HSSF 是Horrible SpreadSheet Format的缩写,也即“讨厌的电子表格格式”。 也许HSSF的名字有点滑稽,就本质而言它是一个非常严肃、正规的API。通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。
1 创建对象
| HSSFWorkbook workbook = new HSSFWorkbook(); |
2 样式
2.1 基础样式
| |
| HSSFCellStyle cellStyle = workbook.createCellStyle(); |
| |
| cellStyle.setAlignment(HorizontalAlignment.CENTER); |
| |
| cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| |
| cellStyle.setWrapText(true); |
| |
| HSSFFont cellFont = workbook.createFont(); |
| cellFont.setFontName("宋体"); |
| cellStyle.setFont(cellFont); |
2.2 背景颜色
| |
| HSSFCellStyle colorStyle = workbook.createCellStyle(); |
| colorStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| |
| colorStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); |
| |
| HSSFPalette customPalette = workbook.getCustomPalette(); |
| customPalette.setColorAtIndex(IndexedColors.LIGHT_TURQUOISE.getIndex(), (byte) 255,(byte) 244, (byte) 144); |
| colorStyle.setAlignment(HorizontalAlignment.CENTER); |
| colorStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| |
| colorStyle.setBorderBottom(BorderStyle.THIN); |
| colorStyle.setBorderLeft(BorderStyle.THIN); |
| colorStyle.setBorderTop(BorderStyle.THIN); |
| colorStyle.setBorderRight(BorderStyle.THIN); |
| |
| colorStyle.setBottomBorderColor(IndexedColors.CORAL.getIndex()); |
| colorStyle.setTopBorderColor(IndexedColors.CORAL.getIndex()); |
| colorStyle.setLeftBorderColor(IndexedColors.CORAL.getIndex()); |
| colorStyle.setRightBorderColor(IndexedColors.CORAL.getIndex()); |
| |
| HSSFPalette customPalette2 = workbook.getCustomPalette(); |
| customPalette2.setColorAtIndex(IndexedColors.CORAL.getIndex(), (byte) 208,(byte) 214, (byte) 228); |
| |
2.3 文字颜色样式
| HSSFCellStyle redFont = workbook.createCellStyle(); |
| |
| redFont.setVerticalAlignment(VerticalAlignment.CENTER); |
| HSSFFont font = workbook.createFont(); |
| |
| font.setFontHeightInPoints((short)12); |
| |
| font.setFontName("宋体"); |
| |
| font.setBold(true); |
| |
| font.setColor(Font.COLOR_RED); |
| redFont.setFont(font); |
| redFont.setFillBackgroundColor(Font.COLOR_NORMAL); |
| redFont.setFont(font); |
| redFont.setWrapText(true); |
3 创建单元格并输入数据
| |
| HSSFSheet sheet = workbook.createSheet("我是sheet名"); |
| sheet.setDefaultColumnWidth((short)16); |
| sheet.setDefaultRowHeight((short)450); |
| |
| |
| HSSFRow row = sheet.createRow(0); |
| |
| row.setHeightInPoints(52); |
| |
| |
| |
| sheet.addMergedRegion(new CellRangeAddress(0,0,0,7)); |
| |
| |
| HSSFCell cell = row.createCell(0); |
| cell.setCellValue("我是标题"); |
| cell.setCellStyle(cellStyle); |
| |
| |
| row = sheet.createRow(1); |
| |
| cell = row.createCell(0); |
| |
| cell.setCellValue("单元格1"); |
| |
| cell.setCellStyle(cellStyle); |
| |
| cell = row.createCell(1); |
| cell.setCellValue("单元格2"); |
| cell.setCellStyle(cellStyle); |
| |
| cell = row.createCell(2); |
| cell.setCellValue("单元格3"); |
| cell.setCellStyle(cellStyle); |
| |
| |
| sheet.createFreezePane(0,2); |
| |
| |
4 为单元格设置下拉框
| DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); |
| |
| CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(3,400,3,3); |
| |
| DataValidationConstraint explicitListConstraint = dataValidationHelper.createExplicitListConstraint(new String[]{"重要","普通"}); |
| |
| DataValidation validation = dataValidationHelper.createValidation(explicitListConstraint, cellRangeAddressList); |
| sheet.addValidationData(validation); |
| |
| CellRangeAddressList cellRangeAddressList2 = new CellRangeAddressList(3,400,4,4); |
| DataValidationConstraint explicitListConstraint2 = dataValidationHelper.createExplicitListConstraint(new String[]{"111","222","333"}); |
| DataValidation validation2 = dataValidationHelper.createValidation(explicitListConstraint2, cellRangeAddressList2); |
| sheet.addValidationData(validation2); |
5 为单元格设置时间格式
| HSSFCellStyle dateStyle = workbook.createCellStyle(); |
| HSSFDataFormat dataFormat = workbook.createDataFormat(); |
| dateStyle.setDataFormat(dataFormat.getFormat("yyyy-mm-dd")); |
| |
| for (int i = 3; i <400; i++) { |
| row = sheet.createRow(i); |
| for (int j = 9; j <=11; j++) { |
| cell = row.createCell(j); |
| cell.setCellStyle(dateStyle); |
| } |
| } |
| |
| |
6 生成后返回给前端
6.1 后端发送
| HSSFWorkbook workbook = testService.getExportXls(); |
| OutputStream osOut = null; |
| try{ |
| osOut = response.getOutputStream(); |
| response.setHeader("Content-Disposition", "attachment;"); |
| response.setContentType("application/vnd.ms-excel;charset=utf-8"); |
| response.setCharacterEncoding("utf-8"); |
| workbook.write(osOut); |
| osOut.flush(); |
| }catch (IOException e){ |
| e.printStackTrace(); |
| }finally { |
| try { |
| if(osOut != null){ |
| osOut.close(); |
| } |
| workbook.close(); |
| }catch (IOException e){ |
| e.printStackTrace(); |
| } |
| } |
6.2 前端接收
| |
| downloadXls(){ |
| |
| request({ |
| url: `/后端的请求路径`, |
| method: "get", |
| responseType: 'blob' |
| }).then(res=>{ |
| let url = window.URL.createObjectURL(new Blob([res], { type: 'application/vnd.ms-excel' })) |
| let link = document.createElement('a') |
| link.style.display = 'none' |
| link.href = url |
| link.setAttribute('download', '文件名称.xls') |
| document.body.appendChild(link) |
| link.click() |
| document.body.removeChild(link) |
| window.URL.revokeObjectURL(url) |
| }) |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)