使用hutool工具類進行導出


引入依賴為:

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.0.6</version>
        </dependency>

代碼為:

    @Test
    public void merTest() {
//測試使用的數據
        TestBean bean1 = new TestBean();
        bean1.setName("張三");
        bean1.setAge(22);
        bean1.setPass(true);
        bean1.setScore(66.30);
        bean1.setExamDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));

        TestBean bean2 = new TestBean();
        bean2.setName("李四");
        bean2.setAge(28);
        bean2.setPass(false);
        bean2.setScore(38.50);
        bean2.setExamDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
//傳入list集合數組
        List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);

        // 通過工具類創建writer
        ExcelWriter writer = ExcelUtil.getBigWriter("E:/data/excel/4.xlsx");

        String[] names = {"name", "age", "score", "isPass", "examDate"};
        String[] names2 = {"姓名", "年齡", "分數", "是否通過", "考試時間"};

        for (int i = 0; i < names.length; i++) {
            writer.addHeaderAlias(names[i], names2[i]);
        }
        //todo 設置自動換行(時間)
        writer.setColumnWidth(4, 25);
// 一次性寫出內容,使用默認樣式,強制輸出標題
        writer.write(rows, true);
// 關閉writer,釋放內存
        writer.close();
    }

將會在指定的文件中生成對應的excel文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM