Excel导出换行的问题

 

记录工作中的点点滴滴。。。。。。

 

 

<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());
    }

 

 

 

 


 

posted @ 2020-12-02 09:27  tsing0520  阅读(2162)  评论(1编辑  收藏  举报