JAVA使用hutool poi工具讀取Excel模板並寫值輸出文件


1.pom依賴配置

        <!-- huTool工具箱 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.19</version>
        </dependency>

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

 

2.測試代碼

package com.hdwang.exceltest;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        File templateFile = new File("C:\\Users\\hdwang\\Desktop\\test.xlsx");
        File outputFile = new File("C:\\Users\\hdwang\\Desktop\\test2.xlsx");
        produceExcelByTemplate(templateFile, outputFile);
    }


    /**
     * 根據模板文件產生Excel文件
     *
     * @param templateFile 模板文件
     * @param outputFile   輸出文件
     */
    private static void produceExcelByTemplate(File templateFile, File outputFile) {
        //===========================讀取測試=====================================
        ExcelReader excelReader = ExcelUtil.getReader(templateFile, 0);

        //讀取數據,按照行列方式讀取所有數據
        List<List<Object>> rowObjects = excelReader.read();
        System.out.println(rowObjects);

        //讀取數據,指定標題行和起始數據行
        List<Map<String, Object>> rowMaps = excelReader.read(1, 2, Integer.MAX_VALUE);
        System.out.println(rowMaps);

        //讀取數據,指定標題行和起始數據行,轉換為對象
        excelReader.addHeaderAlias("名稱", "name");
        excelReader.addHeaderAlias("數值", "value");
        List<ZhenquanReport> reports = excelReader.read(1, 2, Integer.MAX_VALUE, ZhenquanReport.class);
        System.out.println(reports);

        //讀取指定單元格
        String value = String.valueOf(excelReader.readCellValue(2,2));
        System.out.println(value);

        //關閉
        excelReader.close();

        //===========================寫入測試=====================================
        ExcelWriter excelWriter = new ExcelWriter(templateFile);
        excelWriter.writeCellValue(3, 2, "error"); //寫入值,x=列、y=行號

        //設置單元格樣式
        CellStyle cellStyle = excelWriter.createCellStyle(3, 2);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //設置背景色
        cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        cellStyle.setBorderBottom(BorderStyle.DASHED); //設置邊框線條與顏色
        cellStyle.setBottomBorderColor(IndexedColors.PINK.getIndex());
        Font font = excelWriter.createFont();
        font.setColor(IndexedColors.RED.getIndex());
        cellStyle.setFont(font); //設置字體

        //設置輸出文件路徑
        excelWriter.setDestFile(outputFile);
        excelWriter.close();
    }
}

 

 

 

3.讀取的內容

[[證券月報, 證券月報, 證券月報, 證券月報, ], [null, 名稱, 數值], [資產, 凈資產, 10000], [資產, 市值, 20000], [null, 標題], [利潤, 凈利潤, 1000]]
[{A=資產, 名稱=凈資產, 數值=10000}, {A=資產, 名稱=市值, 數值=20000}, {A=null, 名稱=標題, 數值=null}, {A=利潤, 名稱=凈利潤, 數值=1000}]
[ZhenquanReport{name='凈資產', value='10000'}, ZhenquanReport{name='市值', value='20000'}, ZhenquanReport{name='標題', value='null'}, ZhenquanReport{name='凈利潤', value='1000'}]
10000

 

4.文件格式

C:\\Users\\hdwang\\Desktop\\test.xlsx

 

 

C:\\Users\\hdwang\\Desktop\\test2.xlsx

 

5.附錄

最新源碼文件:https://github.com/hdwang123/exceltest

 


免責聲明!

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



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