springboot-實現excle檔案匯出的單元格相同內容合併

万笑佛發表於2024-10-24

匯出excle檔案中的單元格有些需要合併如何操作

例如:左邊的表格想合併單元格成右邊的表格更加便於觀看

一、依賴檔案

 <!-- excle操作-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.8</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>

二、程式碼

@RequestMapping("/merge")
public class MergeController {

    @RequestMapping(value = "/excleExport" )
    @ResponseBody
    public void csvExport(HttpServletResponse response){
        //表頭
        List<String> headerList = Arrays.asList("編號", "姓名");
        //透過工具類建立writer,預設建立xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
        //寫入表頭
        writer.writeHeadRow(headerList);
        //組織資料
        // 組織資料
        List<List<Object>> data = new ArrayList<>();

        List list1 = Arrays.asList("001","張三");
        List list2 = Arrays.asList("001","張三");
        List list3 = Arrays.asList("001","張三");
        List list4 = Arrays.asList("002","李四");
        List list5 = Arrays.asList("002","李四");
        List list6 = Arrays.asList("003","王五");
        data.add(list1);
        data.add(list2);
        data.add(list3);
        data.add(list4);
        data.add(list5);
        data.add(list6);

        //一次性寫出內容,使用預設樣式,強制輸出標題
        writer.write(data,true);
        //設定表格寬度自動
        writer.autoSizeColumnAll();

        //獲取底層的Workbook和Sheet物件
        Workbook workbook = writer.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);

        //合併相同編號的單元格
        int startRow = 1; // 資料開始行(跳過表頭)
        for (int i = 1; i < data.size(); i++) {
            if (!data.get(i).get(0).equals(data.get(i - 1).get(0))) {
                System.out.println(data.get(i).get(0));
                System.out.println(data.get(i - 1).get(0));
                if (startRow < i) {
                    // 合併編號列  前兩個引數起始行和結束行  後兩個引數起始列和結束列
                    sheet.addMergedRegion(new CellRangeAddress(startRow, i , 0, 0));
                    // 合併姓名列
                    sheet.addMergedRegion(new CellRangeAddress(startRow, i , 1, 1));
                }
                startRow = i+1;
            }
        }

        //response為HttpServeltReponse物件
        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setHeader("Content-Disposition","attachment;filename=1.xls");
        ServletOutputStream out= null;
        try{
            out = response.getOutputStream();
            writer.flush(out,true);

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //關閉writer,釋放記憶體
            writer.close();
        }
        IoUtil.close(out);
    }
    
}

三、postman呼叫

下載後就是開頭合併單元格後的效果

原始碼獲取方式(免費):
(1)登入-註冊:http://resources.kittytiger.cn/
(2)搜尋:springboot匯出excle+匯出的excle單元格合併

相關文章