public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
//一、从后台拿数据
if (null == request || null == response)
{
return;
}
List<BoxBatch> list = null;
String batchNo = request.getParameter("batchNo");
list = this.boxBatchNoService.getDownloadList(batchNo);
//二、 数据转成excel
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String fileName = "导入结果详情.xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 第一步:定义一个新的工作簿
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步:创建一个Sheet页
XSSFSheet sheet = wb.createSheet("startTimeendTime");
sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
sheet.setColumnWidth(0, 4000);//设置列宽
sheet.setColumnWidth(1,5500);
sheet.setColumnWidth(2,5500);
sheet.setColumnWidth(3,5500);
sheet.setColumnWidth(11,3000);
sheet.setColumnWidth(12,3000);
sheet.setColumnWidth(13,3000);
XSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("串号 ");
cell = row.createCell(1);
cell.setCellValue("成员状态 ");
cell = row.createCell(2);
cell.setCellValue("同步状态");
cell = row.createCell(3);
cell.setCellValue("同步结果描述");
XSSFRow rows;
XSSFCell cells;
for (int i = 0; i < list.size(); i++) {
// 第三步:在这个sheet页里创建一行
rows = sheet.createRow(i+1);
// 第四步:在该行创建一个单元格
cells = rows.createCell(0);
// 第五步:在该单元格里设置值
cells.setCellValue(list.get(i).getSerialNum());
cells = rows.createCell(1);
if ("01".equals(list.get(i).getStatus())) {
cells.setCellValue("新增");
} else {
cells.setCellValue("删除");
}
cells = rows.createCell(2);
if("01".equals(list.get(i).getSyncStatus())){
cells.setCellValue("未同步");
}else{
cells.setCellValue("已同步");
}
cells = rows.createCell(3);
cells.setCellValue(list.get(i).getSyncDescribe());
}
try {
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
下载excel
最新推荐文章于 2022-11-25 13:23:19 发布