Hutool工具类ExcelUtil --- 简单表格的创建、数据导入、字体、样式

2 篇文章 0 订阅

1.所需依赖

注意:hutool、poi版本不匹配会导致异常

 <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.3.12.RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
 </parent>

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

2.Excel文件创建、内容导入

@Data
public class User {

    private String id;

    private String name;

    private Integer age;

    private Date birthday;

    public User() {
    }

    public User(String id, String name, Integer age, Date birthday) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    
}

 #控制列顺序,需要使用LinkedHashMap,不能使用HashMap
 Map<String, String> headers = new LinkedHashMap<>();
 headers.put("id","编号");
 headers.put("name","姓名");
 headers.put("age","年龄");
 headers.put("birthday","出生日期");

 ExcelWriter writer = ExcelUtil.getWriter("d:/excelTest.xls");
 #设置表格头部别名
 writer.setHeaderAlias(headers);
 writer.setOnlyAlias(true);
 List<User> userList = new ArrayList<>();
 userList.add(new User("1","小明",20,new Date()));
 userList.add(new User("2","小华",20,new Date()));
 writer.write(userList,true);

 #必须使用close或flush文件才能被创建、数据才能被导入
 writer.close();

3.表格样式设置

Workbook workbook = writer.getWorkbook();
#设置全局样式
StyleSet styleSet = new StyleSet(workbook);

#设置字体样式
Font font = workbook.createFont();
#是否加粗
font.setBold(true);
#颜色
font.setColor(Font.COLOR_RED);
#字体
font.setFontName("微软雅黑");
#斜体
font.setItalic(true);
styleSet.setFont(font,true);

#设置全局行样式
CellStyle cellStyle = styleSet.getCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
cellStyle.setAlignment(HorizontalAlignment.LEFT);
writer.setStyleSet(styleSet);


#头部样式
CellStyle headCellStyle = styleSet.getHeadCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
cellStyle.setAlignment(HorizontalAlignment.LEFT);
headCellStyle.setFont(font);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以按照以下步骤使用HutoolExcelUtil工具类来读取Excel文件并生成SQL语句: 1. 首先,确保您已经导入Hutool的相关依赖。 2. 使用ExcelUtil类的getReader方法获取ExcelReader对象,代码示例: ```java ExcelReader reader = ExcelUtil.getReader("path/to/your/excel/file.xlsx"); ``` 这里的"path/to/your/excel/file.xlsx"是您要读取的Excel文件的路径。 3. 使用ExcelReader对象的read方法读取Excel文件中的数据,代码示例: ```java List<List<Object>> dataList = reader.read(); ``` 这里的dataList是一个二维列表,包含了Excel文件中所有行和列的数据。 4. 遍历dataList,生成SQL语句。您可以根据Excel文件的结构和需求自定义生成SQL语句的逻辑。以下是一个简单的示例: ```java StringBuilder sqlBuilder = new StringBuilder(); for (List<Object> row : dataList) { String column1 = row.get(0).toString(); String column2 = row.get(1).toString(); String column3 = row.get(2).toString(); String sql = String.format("INSERT INTO table_name (column1, column2, column3) VALUES ('%s', '%s', '%s');", column1, column2, column3); sqlBuilder.append(sql).append("\n"); } String sqlStatements = sqlBuilder.toString(); ``` 这里的table_name是您要插入数据的表名,column1、column2、column3是Excel文件中对应的列名。 5. 最后,您可以将生成的SQL语句保存到文件或执行其他操作。 请注意,上述示例仅为生成SQL语句的简单示例,您可能需要根据实际情况进行适当的修改和扩展。 希望能帮到您!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值