I have a problem with Spring MVC and Excel. I store excel file in database as lob. Like this part of my entity database is PostgreSQL
@Lob
@Column(name = "Exel")
private String exel;
Next I would like to get it from DB and user can download it from web page this is the controller
@RequestMapping(value = "/downloadExelTemplate.xls", method = RequestMethod.GET)
public void downloadExelTemplate(HttpServletResponse response)
throws IOException {
response.setContentType("application/x-msexcel");
ExelDTO exel = service.getExel(new Long(1));
InputStream is = new ByteArrayInputStream(exel.getExel().getBytes());
BufferedWriter outex = new BufferedWriter(new FileWriter("out.xls"));
outex.write(exel.getExel());
outex.close();
ServletOutputStream out = response.getOutputStream();
out.write(exel.getExel().getBytes());
is.close();
out.close();
}
And I get not proper xls file with the bugs.
Please help me. What is wrong? When I get file from stream is the same effect.
So I resolve my problem, I change the type of data in hibernate entity to
and everything work well. When there was only text file there was no problem to mapping the @lob to the string but connected with ms office formats it fails. So I learned how to keep data file correctly.