Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error reading zip file containing an excel file

843842Jul 15 2008 — edited Jul 31 2008
Hi all,

Using servlets I am able to zip an excel file using java.util.zip package. The excel file is created using POI API.

While I save and extract the zip file on my machine and then open the file in MS Excel. Excel pops a message saying "MS Office Excel has encountered a problem and need to close" and it gives me an option to "Recover my work and restart MS Office Excel". I select the option and then MS Excel does a document recovery and I am able to view my data. I get an excel repair log file saying as follows
"Microsoft Office Excel File Repair Log
Errors were detected in file 'C:\Documents and Settings\JohnDoe\Desktop\excel\test.xls'
The following is a list of repairs:
Damage to the file was so extensive that repairs were not possible. Excel attempted to recover your formulas and values, but some data may have been lost or corrupted.
"

I have attached my servlet code down below I suspect there is a problem with PrintWriter to output or do I need to use OutputStream to write excel data.


Below is my servlet code:

import java.io.*;
import javax.servlet.*;
import org.apache.log4j.Logger;
import java.util.*;
import java.util.zip.*;
import java.net.*;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.util.*;

public class ZipServlet extends HttpServlet {


/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
makeZip(request, response, "GET");
}

/**
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
makeZip(request, response, "POST");
}

public void makeZip(HttpServletRequest request, HttpServletResponse response, String methodGetPost) {
Logger logger = Logger.getLogger(ZipServlet.class);
try
{
int id=1;
ServletContext sc = getServletContext();
HttpSession session = request.getSession();
ConnectionPoolManager cpm = (ConnectionPoolManager)sc.getAttribute("CONNECTION_POOL_MANAGER");
ConnectionPool cp = cpm.getConnectionPool(id);

createTestExcelZip(request,response,methodGetPost,session);
}

} catch (Exception e2) {

}
}

public void createTestExcelZip(HttpServletRequest request, HttpServletResponse response,
String methodGetPost,HttpSession session
)throws ServletException, IOException
{
Logger logger = Logger.getLogger(ZipServlet.class);
try
{ //Create an Excel Workbook and placing value "Test" in the first cell
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCell cell=null;
HSSFRow row = sheet.createRow((short) 0);
short column = 0;

cell = row.createCell(column);
cell.setCellValue(new HSSFRichTextString("Test"));
HSSFCellStyle fontStyle = wb.createCellStyle();
HSSFFont f = wb.createFont();
f.setFontHeight((short) 200);
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontStyle.setFont(f);
cell.setCellStyle(fontStyle);

response.setContentType("application/zip");
response.setHeader("Content-Disposition","attachment; filename=zipExcelRecordFiles.zip;");

int BUFFER = 2048;
byte buf[]=new byte[300000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream( baos );
ByteArrayInputStream is = null;
BufferedInputStream origin = null;
byte[] b = null;
String fileName = null;

b = wb.getBytes();
fileName = "testExcelRecords.xls";

try
{
is = new ByteArrayInputStream(b);
origin = new BufferedInputStream(is, BUFFER);
zos.putNextEntry(new ZipEntry(fileName)); // Add ZIP entry to output stream.

int count;
while((count = origin.read(buf, 0, BUFFER)) != -1)
{
zos.write(buf, 0, count);
}

zos.closeEntry(); // Complete the entry
is.close();



}catch(Exception e)
{
logger.error(e);
}


zos.close();
PrintWriter pr = response.getWriter();
pr.write(baos.toString("ISO-8859-1"));
pr.close();

}
catch(Exception e)
{
logger.error(e);
}

}

/**
* @see javax.servlet.GenericServlet#destroy()
*/
public void destroy() {
}
}

Any help would be appreciated.

Regards
jdcunha
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 28 2008
Added on Jul 15 2008
3 comments
1,204 views