我找不出什麼問題。我嘗試過,但錯誤仍然存在。在java中寫入excel時出現錯誤
我的計劃是要導出到Excel中,我使用Apache POI API。
以下是我的代碼。編輯:
public void exportToExcel(ValueObjectList columnBody, String pageDef, ValueObject vo){
try {
HttpServletResponse response = vo.getResponse();
SimpleDateFormat sd = new SimpleDateFormat("ddMMyy");
Date dt = new Date();
response.setContentType("application/vnd.ms-excel");
if(pageDef == "promo" || pageDef.equals("promo"))
response.setHeader("Content-Disposition", "attachment; filename=PROMO-" + sd.format(dt) + ".xls");
else if(pageDef == "incomplete" || pageDef.equals("incomplete"))
response.setHeader("Content-Disposition", "attachment; filename=INCOM-" + sd.format(dt) + ".xls");
else
response.setHeader("Content-Disposition", "attachment; filename=ST-" + sd.format(dt) + ".xls");
// create a small spreadsheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = null;
HSSFCell cell = null;
//set default font properties
//font family: Arial
//font weight: bold
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short)14);
//Cell Style for header
CellStyle csHeader = wb.createCellStyle();
csHeader.setFont(headerFont);
csHeader.setBorderBottom(csHeader.BORDER_THICK);
csHeader.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
csHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
csHeader.setWrapText(true);
//Cell Style for body
CellStyle csBody = wb.createCellStyle();
csBody.setWrapText(true);
csBody.setAlignment(HSSFCellStyle.ALIGN_CENTER);
String[] columnHeader = ((ValueObject)columnBody.get(0)).toKeyArray();
//System.out.println("Header Length: " + columnHeader.length);
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("No");
cell.setCellStyle(csHeader);
for(int h = 0; h < columnHeader.length; h++){
cell = row.createCell(h+1);
cell.setCellValue(columnHeader[h]);
cell.setCellStyle(csHeader);
sheet.autoSizeColumn(h+1);
//sheet.setColumnWidth(h, 2000);
}
//System.out.println("header key : " + columnHeader[2]);
//System.out.println("header value : " + testobj.get(testobj.toKeyArray()[2]));
for(int i = 0; i < columnBody.size(); i++){
row = sheet.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(i+1);
cell.setCellStyle(csBody);
ValueObject column = (ValueObject)columnBody.get(i);
for(int j = 0; j < column.size(); j++){
cell = row.createCell(j+1);
cell.setCellValue(column.get(column.toKeyArray()[j]));
cell.setCellStyle(csBody);
sheet.autoSizeColumn(j+1);
}
}
/*
// write it as an excel attachment
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte [] outArray = outByteStream.toByteArray();
response.setContentType("application/ms-excel");
response.setContentLength(outArray.length);
response.setHeader("Expires:", "0"); // eliminates browser caching
if(pageDef == "promo" || pageDef.equals("promo"))
response.setHeader("Content-Disposition", "attachment; filename=PROMO-" + sd.format(dt) + ".xls");
else if(pageDef == "incomplete" || pageDef.equals("incomplete"))
response.setHeader("Content-Disposition", "attachment; filename=INCOM-" + sd.format(dt) + ".xls");
else
response.setHeader("Content-Disposition", "attachment; filename=ST-" + sd.format(dt) + ".xls");
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();
*/
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(wb.getBytes().length);
wb.write(outByteStream);
} catch (Exception e) {
log.error(e);
e.printStackTrace();
}
}
我試圖在谷歌衝浪,並試圖解決,但不好。
首先,我在jsp中製成這些代碼。
當我瀏覽谷歌,人們說我已經讓我感動與Servlet,但還是得到了錯誤的servlet來使用。
英語不是我的母語。對不起,如果我輸入錯誤。
在此先感謝。
該錯誤看起來與HSSF和POI無關。 「File」/common/err/errorPage.jsp「找不到」 – 2013-04-04 02:12:36
@JimGarrison,當我包含errorPage.jsp時,它顯示「java.lang.IllegalStateException:getOutputStream()已被調用此響應」 – Raymond 2013-04-04 02:18:02
你是否多次調用'response.getOutputStream()'? – 2013-04-04 02:23:23