JAVA poi导出为excel详细范例(二)

2014-11-24 09:14:50 · 作者: · 浏览: 1
return sheet1;
}
org.apache.poi.ss.usermodel.Sheet xlsSheet = this.wb.getSheet(sheet);
if (null == xlsSheet) {
xlsSheet = this.wb.createSheet(sheet);
}
Sheet obj = new Sheet(this.wb, xlsSheet);
this.sheetMap.put(sheet, obj);
return obj;
}

public void write() throws IOException {
this.wb.write(this.out);
}

public void close() {
this.sheetMap.clear();
IOUtils.closeQuietly(this.out);
}
}

service层处理:

public byte[] findExcelData() throws NetflowServiceException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExcelWriter writer = null;
try {
writer = new ExcelWriter(out);
/*拼装excel头部*/
ExcelWriter.Sheet sheetAct = writer.getOrCreateSheet(informationPublishForm.getOrdertitle());

for(int i=0;i sheetAct.getOrCreateCell(0, i).setCellValue(excelHead.get(i));
}

/*拼装excel内容*/
for(int i=0;i sheetAct.getOrCreateCell((i + 1), j).setCellValue(value);
}
}
writer.write();
} catch(Exception e){
throw new NetflowServiceException(e);
} finally {
if (null != writer) {
writer.close();
}
}
return out.toByteArray();
}


action处理:

public StringdownExcel() {
try {
byte[] fileNameByte = (informationPublishForm.getOrdertitle() + ".xlsx").getBytes("GBK");
String filename = new String(fileNameByte, "ISO8859-1");

byte[] bytes = commonTableReportService.findExcelData(psForm, reportId);
response.setContentType("application/x-msdownload");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.getOutputStream().write(bytes);
log.info("产生excel文件大小(byte):" + bytes.length);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}