Hutool工具类的使用(一)之实现快速生成表格(Use of hutool tool class (I) implementation of rapid table generation)

  • 先导入依赖

<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>

  • 实例代码

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();    }

————————
  • Import dependency first

<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>

  • Example code

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>& gt; rows = CollUtil. newArrayList(row1, row2, row3, row4, row5); / / create writer through tool class. Excelwriter = excelutil getWriter( “C:\\Users\\MI\\Desktop\\xm\\Hutool\\src\\main\\resources/writeTest.xlsx” ); / / create writer through construction method / / excelwriter writer writer = new excelwriter (“D: / writetest. XLS”); / * modify background * / / / define the cell background color, * styleset style = writer getStyleSet(); / / the second parameter indicates whether to also set the header cell background style setBackgroundColor(IndexedColors.BLUE, false); / / set content font} font = writer createFont();         font. setBold(true);         font. setColor(Font.COLOR_RED);         font. setItalic(true); / / the second parameter indicates whether to ignore the header style} writer getStyleSet(). setFont(font, true); / / skip the current line, i.e. the first line, which is not required. In this demonstration, use} writer passCurrentRow(); / / the title line after merging cells, using the default title style} writer Merge (row1. Size () – 1, < strong > “< / strong > < strong > test title < / strong >” < / strong >); / / write out the content at one time and force the output of the title @ writer write(rows, true); / / close the writer to free up memory close();     }