SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

понедельник, 14 марта 2011 г.

Servlet to save MSWord and MSExcel

Hi,
if you need to save (open) the MS Word or MS Excel file directly from the browser, for example some report. You should add the following lines to your servlet:


response.setContentType("application/vnd.msword");
response.setHeader("Content-Disposition", "attachment;filename=Report.doc");
OutputStream out = response.getOutputStream();





//CREATE YOUR File f INSTANCE AND DO SOMETHING WITH IT (ADD SOME INFORMATION)
//YOU COULD EASYLY ADD HTML CODE HERE AND MAKE THE RESOLUTION .DOC ;)
//THEN OPEN THIS FILE AND ADD TOU OUTPUT

FileInputStream fistream = new FileInputStream(f);
BufferedInputStream bistream = new BufferedInputStream(fistream);
int readBytes = 0;
while ((readBytes = bistream.read()) != -1)
out.write(readBytes);




So... you can open this Servlet from browser and get your file.
In the case of Excel file you can do the same, but if you use ApachePOI library, you can make it simple:


  bookRead.write(sos);
  sos.flush();
  sos.close();

variables:

OutputStream sos = response.getOutputStream();
HSSFWorkbook bookRead = new HSSFWorkbook();
So, be happy guys, this is a little things but make us happy sometimes :) .

Комментариев нет:

Отправить комментарий