使用poi生成excel通常包含一下几个步骤
创建一个工作簿
创建一个sheet
创建一个Row对象
创建一个cell对象(1个row+1个cell构成一个单元格)
设置单元格内容
设置单元格样式. 字体 字体大小 是否加粗
保存
关闭流对象
生成一个工作簿
2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样.
生成2003格式
public void test1() { HSSFWorkbook workbook = new HSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); Font font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); HSSFSheet sheet = workbook.createSheet("Sheet1"); //设置单元格宽度 sheet.setColumnWidth(0, 30 * 256); sheet.setColumnWidth(1, 30 * 256); sheet.setColumnWidth(2, 30 * 256); Row row0 = sheet.createRow(0); Cell cell0 = row0.createCell(0); cell0.setCellValue("序号"); cell0.setCellStyle(cellStyle); Cell cell1 = row0.createCell(1); cell1.setCellValue("姓名"); Cell cell2 = row0.createCell(2); cell2.setCellValue("成绩"); OutputStream os = null; try { os = new FileOutputStream("d:\\测试生成2003.xls"); workbook.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
生成2010以上格式
@Test public void test2() { XSSFWorkbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); Font font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); XSSFSheet sheet = workbook.createSheet("Sheet1"); Row row0 = sheet.createRow(0); Cell cell0 = row0.createCell(0); cell0.setCellValue("序号"); cell0.setCellStyle(cellStyle); Cell cell1 = row0.createCell(1); cell1.setCellValue("姓名"); Cell cell2 = row0.createCell(2); cell2.setCellValue("成绩"); OutputStream os = null; try { os = new FileOutputStream("d:\\测试生成2010.xlsx"); workbook.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
合并单元格
合并单元格在生成excel中算常见的一个场景, 通常先合并单元, 单元格内容居中,并设置单元格边框.
poi合并单元格使用CellRangeAddress类, 构造函数包括4个参数firstRow, lastRow, firstCol, lastCol根据自己需要传入行和列.
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) { }
合并单元格后设置边框poi已提供了RegionUtil静态类, 可直接使用.
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2); sheet.addMergedRegion(region); RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
设置单元格样式
左右居中 上下居中 自动换行
cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setWrapText(true);
使用SpringMVC/SpringBoot导出excel
@Controller @GetMapping("/excel2003") public void excel2003(HttpServletResponse httpServletResponse){ try { //2010格式设置 //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //2003格式设置 response.setContentType("application/vnd.ms-excel"); httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("学生成绩单.xls", "utf-8")); ServletOutputStream outputStream = httpServletResponse.getOutputStream(); HSSFWorkbook workbook = new HSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); Font font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); HSSFSheet sheet = workbook.createSheet("Sheet1"); Row row0 = sheet.createRow(0); Cell cell0 = row0.createCell(0); cell0.setCellValue("序号"); cell0.setCellStyle(cellStyle); Cell cell1 = row0.createCell(1); cell1.setCellValue("姓名"); Cell cell2 = row0.createCell(2); cell2.setCellValue("成绩"); workbook.write(outputStream); } catch (Exception e) { e.printStackTrace(); } }
以上是java怎么使用poi生成excel的详细内容。更多信息请关注PHP中文网其他相关文章!

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Dreamweaver Mac版
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver CS6
视觉化网页开发工具