我无法弄清楚是什么问题。我试过了,但错误仍然存在。
我的程序是导出到 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 但仍然出现错误。
英语不是我的母语。对不起,如果我输入错误。
提前致谢。