0

I am working in a project which is already developed. Now I have a task to generate excel file using Apache poi API.

My problem is I have PrinWriter object available instead of OutputStream.

workbook.write(outputStream);

How to tackle this situation?

Himanshu
  • 378
  • 8
  • 27
SSS4
  • 1
  • Possibly duplicate of http://stackoverflow.com/questions/4268353/is-there-a-simple-and-safe-way-to-convert-a-printwriter-into-a-printstream – Shoaib Chikate Jan 20 '14 at 12:44
  • You would need some sort of `OutputStream` that decorates a `Writer`. There's [one in the Commons IO library](http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/WriterOutputStream.html). – Boris the Spider Jan 20 '14 at 12:45

2 Answers2

0
 File f=new File("c://newfolder//lijo.xls");
    PrintWriter out=new PrintWriter(f);
   FileOutputStream fout=new FileOutputStream(f);

for converting from one output stream to another just pass the reference value example to convert file to fileoutput stream

File file=new File("c://lijo.xls");
FileOutputStream outputStream=new FileOutputStream(file);
Lijo
  • 6,498
  • 5
  • 49
  • 60
  • Where are you seeing a constructor on `FileOutputStream` that takes a `Writer` of any kind? There isn't one in the [JavaDoc](http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html). – Boris the Spider Jan 20 '14 at 18:00
0

If you work on web project you may use ServletOutputStream and doing something like this

Workbook wb = new HSSFWorkbook();
ServletOutputStream out = response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();

Or you can use FileInputStream

Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));
Aram
  • 163
  • 4
  • 11