欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java导出多sheet的excel

程序员文章站 2022-07-13 12:58:33
...

csv无法多sheet,JavaPOI导出excel有三种形式:HSSFworkbook,XSSFworkbook,SXSSFworkbook
区别如下:
https://blog.csdn.net/weixin_45631654/article/details/119457460?spm=1001.2014.3001.5502

本次示例是XSSFworkbook,
注意:单元格的限制是32767个字符,超出会报错导出失败。

依赖如下:

   <properties>
      <spring.version>4.3.19.RELEASE</spring.version>
        <poi.version>3.16</poi.version>
    </properties>
    <dependencies>
       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>${poi.version}</version>
        </dependency>
   </dependencies>

代码如下:

package csv;


import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class POITEST {
    public static void main(String[] args) {
        try {
            //excel导出的路径和名称
            OutputStream out = new FileOutputStream("/home/quyanhang/Downloads/test.xlsx");
            //生成两个sheet,不同的数据源和列标题
            List<List<Object>> data1 = new ArrayList<>();
            List<List<Object>> data2 = new ArrayList<>();
            String[] headers1 = {"编号", "年龄", "用户名"};
            String[] headers2 = {"编号", "用户名"};
            HashMap<String, String[]> header = new HashMap<>();
            header.put("表单-1", headers1);
            header.put("表单-2", headers2);
            //注意int等其他类型转换成String类型
            //表单-2的数据内容
            for (int i = 1; i < 5; i++) {
                List rowData = new ArrayList();
                rowData.add(String.valueOf(i));
                rowData.add(String.valueOf(i + 20));
                rowData.add("曲" + i + "号");
                data1.add(rowData);
            }
            //表单-1的数据内容
            for (int i = 1; i < 5; i++) {
                List rowData = new ArrayList();
                rowData.add(String.valueOf(i));
                rowData.add("曲" + i + "号");
                data2.add(rowData);
            }

            XSSFWorkbook workbook = new XSSFWorkbook();
            HashMap<String, List<List<Object>>> map = new HashMap<>();
            map.put("表单-1", data1);
            map.put("表单-2", data2);
            int a = 0;
            for (String key : map.keySet()) {
                System.out.println(key);
                System.out.println(a);
                exportExcel(workbook, a, key, header.get(key), map.get(key), out);
                a = a + 1;
            }
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @param workbook
     * @param sheetNum   sheet的位置,0表示第一个表格中的第一个sheet
     * @param sheetTitle sheet的名称
     * @param headers    标题
     * @param result     内容
     * @param out        输出流
     */
    public static void exportExcel(XSSFWorkbook workbook, int sheetNum,
                                   String sheetTitle, String[] headers, List<List<Object>> result,
                                   OutputStream out) throws Exception {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);

        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据,产生数据行
        if (result != null) {
            int index = 1;
            for (List<Object> m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                for (Object str : m) {
                    XSSFCell cell = row.createCell((short) cellIndex);
                    cell.setCellValue(str.toString());
                    cellIndex++;
                }
                index++;
            }
        }

    }

}

相关标签: 工具 java