使用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中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器