SXSSFWorkbook的简单使用

时间:2025-03-20 10:05:47
@Service public class ExportSystemLogService{ @Autowired private SystemLogMapper mapper; public SXSSFWorkbook exportExcel(Map<String, Object> params) throws IOException { SXSSFWorkbook wb = new SXSSFWorkbook(1000); //获取表格列信息 LinkedHashMap<String, List<ExcelColumnInfo>> excelInfo = getExcelInfo(wb); for(String sheetName : ()){ //创建sheet页 Sheet sheet = (sheetName); //获取该sheet页的列信息 List<ExcelColumnInfo> excelColumnInfo = (sheetName); //生成Excel数据 generateExcelData(sheet,excelColumnInfo,params); } return wb; } protected LinkedHashMap<String, List<ExcelColumnInfo>> getExcelInfo(Workbook wb) { LinkedHashMap<String, List<ExcelColumnInfo>> excelInfo = new LinkedHashMap<>(); List<ExcelColumnInfo> columns = new ArrayList<>(); CellStyle percentCellStyle = (); //CellStyle wrapStyle = (); //(true); //设置自动换行 (new ExcelColumnInfo("日志编号", "LOG_ID")); //后面的columnCode与从数据库中查询出来的字段名一致 (new ExcelColumnInfo("操作时间", "CREATE_TIME")); (new ExcelColumnInfo("操作人", "CREATE_USER")); (new ExcelColumnInfo("操作模块", "OPERATION_MODULE")); (new ExcelColumnInfo("操作类型", "OPERATION_TYPE")); (new ExcelColumnInfo("详情", "OPERATION_DETAIL")); (new ExcelColumnInfo("日志级别", "LOG_LEVEL")); (new ExcelColumnInfo("备注", "REMARK")); ("系统日志", columns); return excelInfo; } private void generateExcelData(Sheet sheet,List<ExcelColumnInfo> excelColumnInfo,Map<String, Object> params) { //设置列的宽度,第一个参数为列的序号,从0开始,第二参数为列宽,单位1/256个字节 (0, 12*256); (2, 16*256); (5, 12*256); (6, 26*256); (7, 26*256); //设置开始行和开始列 int rowIndex = 0; int columnIndex = 0; Row row = (rowIndex); //创建表头 for (ExcelColumnInfo column : excelColumnInfo) { ((columnIndex++), ()); } //获取导出数据 List<HashMap<String, Object>> data = (params); rowIndex = 1; for (HashMap<String, Object> tmp : data) { Row row1 = (rowIndex); columnIndex = 0; for(ExcelColumnInfo column : excelColumnInfo){ Cell cell = (columnIndex); //设置单元格样式 if (() != null) { (()); } (cell,(())); columnIndex++; } rowIndex++; } } }