利用hutool导出Excel

1、导入依赖

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

<!--		excel导出依赖-->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.2</version>
		</dependency>

2、代码

Apache POI既可以将数据写入Excel文件,也可以读取Excel文件中的数据,

2.1 基于POI写入excel文件

 public static void write() throws IOException {
        // 在内存重创建一个excel对象
        XSSFWorkbook excel = new XSSFWorkbook();
        // 创建sheet页
        XSSFSheet sheet = excel.createSheet("itcast");
        // 在sheet页中创建行,0表示第1行
        XSSFRow row1 = sheet.createRow(0);
        // 创建单元格,row1.createCell()
        // 设置单元格值 setCellValue("姓名")
        row1.createCell(1).setCellValue("姓名");
        row1.createCell(2).setCellValue("城市");

// 输出工作簿到文件或输出流
        FileOutputStream out = new FileOutputStream(new File("D:\\itcast.xlsx"));
        //通过输出流将内存中的Excel文件写入到磁盘上
        excel.write(out);

        out.flush();
        out.close();
        excel.close();
    }

2.2 读取excel文件

public static void read() throws IOException {
        FileInputStream input = new FileInputStream(new File("D:\\itcast.xlsx"));
        XSSFWorkbook excel = new XSSFWorkbook(input);
        XSSFSheet sheet = excel.getSheetAt(0);

        int lastRowNum = sheet.getLastRowNum();

        for (int i=0;i<=lastRowNum;i++){
            XSSFRow row = sheet.getRow(i);  // 获取行

            XSSFCell cell1 = row.getCell(1); // 获取行的第2个单元格
            String cellValue1 = cell1.getStringCellValue();//获取单元格的文本内容
            XSSFCell cell2 = row.getCell(2);
            String cellValue2 = cell2.getStringCellValue();


            System.out.println(cellValue1+" "+cellValue2);

            input.close();
            excel.close();


        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XSSFWorkbook是Apache POI库中的一个类,用于创建和操作Excel文件(.xlsx格式)。它是基于XML的Excel文件格式,提供了一组Java API来读取、写入和修改Excel文档。 使用XSSFWorkbook,你可以创建新的工作簿、工作表,设置单元格的值、格式、样式等。它还提供了各种方法来处理Excel文件,如读取和写入数据、合并单元格、设置超链接、创建图表等。 要使用XSSFWorkbook,你需要先导入Apache POI库,并在代码中创建一个XSSFWorkbook对象来表示一个Excel文件。然后,你可以使用该对象来访问工作表和单元格,并执行所需的操作。 下面是一个简单的示例代码,演示如何使用XSSFWorkbook创建一个Excel文件并写入数据: ```java import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.ss.usermodel.*; public class ExcelWriter { public static void main(String[] args) { try { // 创建新的工作簿 Workbook workbook = new XSSFWorkbook(); // 创建新的工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建行和单元格,并设置值 Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello, Excel!"); // 保存Excel文件 FileOutputStream fileOut = new FileOutputStream("example.xlsx"); workbook.write(fileOut); fileOut.close(); System.out.println("Excel文件已成功创建!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,这只是一个简单的示例,你可以根据需要进行更复杂的操作,如读取数据、设置样式等。希望这能帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值