1. 程式人生 > 其它 >Hutool工具類的使用(一)之實現快速生成表格

Hutool工具類的使用(一)之實現快速生成表格

  1. 先匯入依賴

<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.19</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version

>5.0.0</version>
</dependency>
</dependencies>

  1. 例項程式碼

public class ExcelDemo {
public static void main(String[] args) {
List<String> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd");
List<String> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1");
List<String> row3 = CollUtil.newArrayList

("aa2", "bb2", "cc2", "dd2");
List<String> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3");
List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4");

List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
//通過工具類建立writer
ExcelWriter writer = ExcelUtil.getWriter
("C:\\Users\\MI\\Desktop\\xm\\Hutool\\src\\main\\resources/writeTest.xlsx");
//通過構造方法建立writer
//ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");
/*修改背景*/
// 定義單元格背景色
StyleSet style = writer.getStyleSet();
// 第二個引數表示是否也設定頭部單元格背景
style.setBackgroundColor(IndexedColors.BLUE, false);

//設定內容字型
Font font = writer.createFont();
font.setBold(true);
font.setColor(Font.COLOR_RED);
font.setItalic(true);
//第二個引數表示是否忽略頭部樣式
writer.getStyleSet().setFont(font, true);


//跳過當前行,既第一行,非必須,在此演示用
writer.passCurrentRow();

//合併單元格後的標題行,使用預設標題樣式
writer.merge(row1.size() - 1, "測試標題");
//一次性寫出內容,強制輸出標題
writer.write(rows, true);
//關閉writer,釋放記憶體
writer.close();

}

本文來自部落格園,作者:我是一個小倉鼠,轉載請註明原文連結:https://www.cnblogs.com/yongyuankuaile/p/15806817.html