//控制层
// 清除buffer缓存
response.reset();
// 指定下载的文件名
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmss”);
String time = sdf.format(date);
time=“导出文件字典信息Excel-”+time;
response.setHeader(“Content-Disposition”, “attachment;filename=”+
new String(time.getBytes(“utf-8”),“iso-8859-1”)+ “.xlsx”);
response.setContentType(“application/vnd.ms-excel;charset=UTF-8”);
response.setHeader(“Pragma”, “no-cache”);
response.setHeader(“Cache-Control”, “no-cache”);
response.setDateHeader(“Expires”, 0);
XSSFWorkbook workbook = null;
List list = iAbsFiledictService.SelectAllFiledictImportExcel(core_id,file_source);
workbook = ExcelExport.AbsFiledictExportContacts(list);
OutputStream output;
output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
workbook.write(bufferedOutPut);
bufferedOutPut.close();
return null;
生成Excel文档工具类
public class ExcelExport {
//文件字典列表
public static XSSFWorkbook AbsFiledictExportContacts(List<AbsFiledict> list) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
XSSFWorkbook xssfWorkbook = null;
String sheetName = "文件字典信息表";
xssfWorkbook = AbsFiledictExportContactsFile(list, sheetName);
return xssfWorkbook;
}
//已认证数据生成EXCEL
public static XSSFWorkbook AbsFiledictExportContactsFile(List<AbsFiledict> list, String sheetName) throws IllegalArgumentException {
// 创建新的Excel工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFRow row = sheet.createRow(0);
CellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//表头粗体显示
style.setFont(font);
XSSFCell cell = row.createCell(0);
cell.setCellStyle(style);sheet.setColumnWidth((short) 0, (short) 5000);
cell.setCellValue("文件字典ID");
cell = row.createCell(1);
cell.setCellStyle(style);sheet.setColumnWidth((short) 1, (short) 5000);
cell.setCellValue("核心企业ID");
cell = row.createCell(2);
cell.setCellStyle(style);sheet.setColumnWidth((short) 2, (short) 5000);
cell.setCellValue("文件名称");
cell = row.createCell(3);
cell.setCellStyle(style);sheet.setColumnWidth((short) 3, (short) 5000);
cell.setCellValue("文件编号");
cell = row.createCell(4);
cell.setCellStyle(style);sheet.setColumnWidth((short) 4, (short) 5000);
cell.setCellValue("文件来源");
cell = row.createCell(5);
cell.setCellStyle(style);sheet.setColumnWidth((short) 5, (short) 5000);
cell.setCellValue("文件类型");
cell = row.createCell(6);
cell.setCellStyle(style);sheet.setColumnWidth((short) 6, (short) 5000);
cell.setCellValue("文件要求");
cell = row.createCell(7);
cell.setCellStyle(style);sheet.setColumnWidth((short) 7, (short) 5000);
cell.setCellValue("盖章要求");
cell = row.createCell(8);
cell.setCellStyle(style);sheet.setColumnWidth((short) 8, (short) 5000);
cell.setCellValue("文件份数");
cell = row.createCell(9);
cell.setCellStyle(style);sheet.setColumnWidth((short) 9, (short) 5000);
cell.setCellValue("文件模板");
cell = row.createCell(10);
cell.setCellStyle(style);sheet.setColumnWidth((short) 10, (short) 5000);
cell.setCellValue("审核要点");
cell = row.createCell(11);
cell.setCellStyle(style);sheet.setColumnWidth((short) 11, (short) 5000);
cell.setCellValue("文件备注");
//放入数据及列自适应宽
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(list.get(i).getFiledictId());
row.createCell(1).setCellValue(list.get(i).getCore_enterp_id());
row.createCell(2).setCellValue(list.get(i).getFiledictName());
row.createCell(3).setCellValue(list.get(i).getFiledictNo());
row.createCell(4).setCellValue(list.get(i).getFiledictSource());
row.createCell(5).setCellValue(list.get(i).getFiledictType());
row.createCell(6).setCellValue(list.get(i).getFiledictRequire());
row.createCell(7).setCellValue(list.get(i).getFiledictSeal());
row.createCell(8).setCellValue(list.get(i).getFiledictNumber());
row.createCell(9).setCellValue(list.get(i).getFiledictUrl());
row.createCell(10).setCellValue(list.get(i).getFiledictAuditpoint());
row.createCell(11).setCellValue(list.get(i).getFiledictDesc());
}
return workbook;
}
}