記錄工作中的點點滴滴。。。。。。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tsing0520</groupId> <artifactId>TP_Excel_Project</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <!-- 核心模塊,包括自動配置支持、日志和YAML --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <!-- 引入WEB模塊 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.tsing0520; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackages = "com.tsing0520") public class TPExcelApplication { public static void main(String[] args) { SpringApplication.run(TPExcelApplication.class, args); } }
在使用Java生成excel文件時,單元格中的換行不起作用,需要雙擊時才顯示換行。
解決方案: cell.setCellStyle(cs);
@RequestMapping("/test/export1") public void getExcel1(HttpServletResponse response) throws IOException { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("aaaa"); arrayList.add("bbbb"); arrayList.add("cccc"); arrayList.add("dddd"); String content = String.join("\n", arrayList); XSSFWorkbook workBook = new XSSFWorkbook(); XSSFSheet sheet = workBook.createSheet("測試導出"); XSSFRow row1 = sheet.createRow(1); XSSFCell cell = row1.createCell(0); // 換行==>>自定義單元格內容換行規則 XSSFCellStyle cs = workBook.createCellStyle(); cs.setWrapText(true); // TODO 錯誤的寫法 // row1.setRowStyle(cs); // TODO 正確的寫法 cell.setCellStyle(cs); // 設置要導出的文件的名字 String fileName = "myExport.xlsx"; String[] headers = { "單元格00","單元格01","單元格02" }; XSSFRow titleRow = sheet.createRow(0); // 在excel表中添加表頭 for (int i = 0; i < headers.length; i++) { titleRow.createCell(i).setCellValue(headers[i]); } cell.setCellValue(content); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.flushBuffer(); workBook.write(response.getOutputStream()); }
在使用Java生成excel文件時間,合並單元格。
@RequestMapping("/test/export2") public void getExcel2(HttpServletResponse response) throws IOException { // 內容 ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("aaaa"); arrayList.add("bbbb"); arrayList.add("cccc"); arrayList.add("dddd"); String content = String.join("\n", arrayList); XSSFWorkbook workBook = new XSSFWorkbook(); XSSFSheet sheet = workBook.createSheet("測試導出2"); // 單元格合並(起始行號,終止行號,起始列號,終止列號) sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2)); sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 5)); XSSFRow titleRow = sheet.createRow(0); // 換行==>>自定義單元格內容換行規則 XSSFCellStyle cs = workBook.createCellStyle(); cs.setWrapText(true); XSSFCell cell0 = titleRow.createCell(0); // TODO 錯誤的寫法 // titleRow.setRowStyle(cs); // TODO 正確的寫法 cell0.setCellStyle(cs); // TODO 合並單元格需要設置行高 titleRow.setHeight((short)1000); // 設置要導出的文件的名字 String fileName = "myExport2.xlsx"; cell0.setCellValue(content); titleRow.createCell(3).setCellValue("GGG"); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.flushBuffer(); workBook.write(response.getOutputStream()); }