Spring Boot Excel 数据导出,不用 Easy Excel 怎么写 ?

87 阅读1分钟
	public void excelExport(HttpServletResponse response, List<PutAssetsProject> result) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			//创建工作簿,SXSSFWorkbook 支持大数据量的导出
			SXSSFWorkbook workbook = new SXSSFWorkbook();
			//创建sheet表
			Sheet sheet = workbook.createSheet("项目报表");
			//String[] strings = {"序号", "项目编号", "项目名称", "项目描述", "创建人"};

			//创建标题
			Row rowTitle = sheet.createRow(0);
			Cell cellTitle = null;
			cellTitle = rowTitle.createCell(0);
			cellTitle.setCellValue("序号");
			cellTitle = rowTitle.createCell(1);
			cellTitle.setCellValue("项目编号");
			cellTitle = rowTitle.createCell(2);
			cellTitle.setCellValue("项目名称");
			cellTitle = rowTitle.createCell(3);
			cellTitle.setCellValue("项目描述");
			cellTitle = rowTitle.createCell(4);
			cellTitle.setCellValue("创建人");

			for (int i = 0; i < result.size(); i++) {
				//创建行
				Row row = sheet.createRow(i + 1);
				Cell cell = null; //创建单元格
				cell = row.createCell(0);
				cell.setCellValue(i + 1);
				cell = row.createCell(1);
				cell.setCellValue(result.get(i).getProjectCode());
				cell = row.createCell(2);
				cell.setCellValue(result.get(i).getProjectName());
				cell = row.createCell(3);
				cell.setCellValue(result.get(i).getDescription());
				cell = row.createCell(4);
				cell.setCellValue(result.get(i).getCreateUser());
				cell = row.createCell(5);
				cell.setCellValue(status(result.get(i).getStatus()));
				cell = row.createCell(6);
				cell.setCellValue(result.get(i).getStartTime());
			}

			//输出Excel文件
			OutputStream output=response.getOutputStream();
			response.reset();
			//设置响应头
			final String name = new String("项目报表".getBytes("UTF-8"), "ISO8859-1");
			response.setContentType("application/msexcel");
			response.setHeader("Content-Disposition","attachment;filename="+ name +".xlsx");
			workbook.write(output);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}