主题:[讨论]POI导出excel文件供客户端下载时遇到的异常!!!
最近在时使POI导出excel表格的时候,遇到了ServletOutputStream和PrintWriter冲突问题,我的代码是这样写的:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package BlueToothSystem.action;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class DownLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try{
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;Filename=Report.xls");
response.setCharacterEncoding("utf-8");
ServletOutputStream so = response.getOutputStream();
HSSFWorkbook hwb = new HSSFWorkbook(); //create the excel work book
HSSFSheet sheet = hwb.createSheet(); //create the excel table
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("ProductName");
hwb.write(so);
so.flush();
so.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
不知道如何处理?还请各位大哥,帮帮忙!非常感谢!
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package BlueToothSystem.action;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class DownLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try{
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;Filename=Report.xls");
response.setCharacterEncoding("utf-8");
ServletOutputStream so = response.getOutputStream();
HSSFWorkbook hwb = new HSSFWorkbook(); //create the excel work book
HSSFSheet sheet = hwb.createSheet(); //create the excel table
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("ProductName");
hwb.write(so);
so.flush();
so.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
不知道如何处理?还请各位大哥,帮帮忙!非常感谢!