private void buildExcelData(HttpServletResponse response,List<ScheduleInfo> todoList,String title) {
ByteArrayOutputStream byteArrayOutputStream = null;
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet();
//创建样式
HSSFCellStyle style1 = HSSFUtil.getStyle(book, HSSFCellStyle.ALIGN_CENTER,1);
HSSFCellStyle style2 = HSSFUtil.getStyle(book, HSSFCellStyle.ALIGN_CENTER,2);
HSSFCellStyle style3 = HSSFUtil.getStyle(book, HSSFCellStyle.ALIGN_CENTER,3);
int rows = 0;
int cells = 0;
HSSFRow row = sheet.createRow(rows);
row.setHeight((short)800);
HSSFCell cell = row.createCell(cells);
cell.setCellValue(title);
cell.setCellStyle(style1);
sheet.setColumnWidth(cells, (15*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (30*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (50*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
/*cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));*/
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (35*256));
cells++;
cell = row.createCell(cells);
sheet.setColumnWidth(cells, (25*256));
rows++;
sheet.addMergedRegion(new CellRangeAddress(0,0,0,cells-2));
cells = 0;
row = sheet.createRow(rows);
row.setHeight((short)500);
cell = row.createCell(cells);
cell.setCellValue("序号");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("姓名");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("日程主题");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("日程内容");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("开始时间");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("结束时间");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("创建人");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("创建时间");
cell.setCellStyle(style3);
/*cells++;
cell = row.createCell(cells);
cell.setCellValue("办理时间(开始/结束)");
cell.setCellStyle(style3);
cells++;
cell = row.createCell(cells);
cell.setCellValue("办文状态");
cell.setCellStyle(style3);*/
rows++;
for (int i = 0; i < todoList.size(); i++) {
ScheduleInfo todo = (ScheduleInfo) todoList.get(i);
row = sheet.createRow(rows);
row.setHeight((short)600);
cells = 0;
cell = row.createCell(cells);
cell.setCellValue(String.valueOf(i+1));//序号
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
// cell.setCellValue(todo.getFormName());//姓名
cell.setCellValue(todo.getPerson());
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
cell.setCellValue(todo.getTheme());//日程主题
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
cell.setCellValue(todo.getContent());
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
cell.setCellValue(sdf.format(todo.getStartime()));//==null?"":sdf.format(todo.getStartime())
//cell.setCellValue("");
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
cell.setCellValue(sdf.format(todo.getEndtime()));
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
cell.setCellValue(todo.getCreateperson());
cell.setCellStyle(style2);
cells++;
cell = row.createCell(cells);
cell.setCellValue(sdf.format(todo.getCreatetime()));
cell.setCellStyle(style2);
rows++;
}
//导出excel名字
String excelName = "日程列表.xls";
byteArrayOutputStream = new ByteArrayOutputStream();
book.write(byteArrayOutputStream);
DownloadResponse downloadResponse = new DownloadResponse(response);
downloadResponse.downloadBytes(byteArrayOutputStream.toByteArray(), StringUtil.toUtf8String(excelName));
}catch(Exception e){
LogUtil.errorLog(this.getClass(), "日程列表导出Excel出错", e);
}finally{
if(byteArrayOutputStream != null){
try{
byteArrayOutputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
java实现Excel数据导出
最新推荐文章于 2024-08-25 16:29:44 发布