标签:excel
看了下同事写的导出excel 很简单 记下来备用
1、加入必须的jar包 poi-3.10-FINAL-20140208.jar
2、新建文件名 “XXXX.xlsx”
3、Workbook wb = null;
4、Workbook wb = new XSSFWorkbook();
5、Sheet sheet = wb.createSheet(fileName);
6、String[] title = {"合同编号"}
7、Row titleRow = sheet.createRow(0);
8、初始化表头
private void initialiseRow(Row row,String[] title){
for (int i = 0; i < title.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(title[i]);
}
}
9、填充数据
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i+1);
row.createCell((short) 0).setCellValue(list.get(i).getCode());
}
10、响应请求
OutputStream os = null;
try {
os = new BufferedOutputStream(response.getOutputStream());// 取得输出流
response.reset();// 清空输出流
response.addHeader("Content-disposition","attachment;filename="
+ new String( fileName.getBytes("gbk"), "ISO8859-1" ));
response.setContentType("application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
wb.write(os);
os.flush();
os.close();
} catch (IOException e) {
logger.error("导出购保情况的Excel出错: ", e);
}
标签:excel
原文地址:http://blog.csdn.net/majun_guang/article/details/45694799