Java 导入excel 两个sheet

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ReadExcel {

    public static void main(String[] args) {
        try {
            // 创建工作簿
            FileInputStream file = new FileInputStream(new File("path/to/excel/file.xlsx"));
            Workbook workbook = WorkbookFactory.create(file);

            // 读取第一个Sheet
            Sheet sheet1 = workbook.getSheetAt(0);
            System.out.println("Sheet 1:");
            for (Row row : sheet1) {
                for (Cell cell : row) {
                    System.out.print(cell.getStringCellValue() + "\t");
                }
                System.out.println();
            }

            // 读取第二个Sheet
            Sheet sheet2 = workbook.getSheetAt(1);
            System.out.println("Sheet 2:");
            for (Row row : sheet2) {
                for (Cell cell : row) {
                    System.out.print(cell.getStringCellValue() + "\t");
                }
                System.out.println();
            }

            // 关闭工作簿和文件流
            workbook.close();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于您的问题,我可以为您提供一些Java导入导出Excel的操作方法。 1. 使用Apache POI库进行Excel文件的读写操作。您需要添加相关的依赖,然后可以使用POI API进行Excel文件的读写操作。下面是读取Excel文件的示例代码: ``` FileInputStream file = new FileInputStream(new File("excel文件的路径")); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { switch (cell.getCellType()) { case STRING: System.out.print(cell.getStringCellValue() + "\t"); break; case NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); break; default: break; } } System.out.println(); } workbook.close(); file.close(); ``` 2. 使用EasyPOI库进行Excel文件的读写操作。EasyPOI是基于POI封装的Excel文件操作库,提供了更加简单方便的API,可以快速地进行Excel文件的读写操作。下面是一个简单的示例代码: ``` // 导入Excel文件 List<ExcelModel> list = ExcelImportUtil.importExcel( new File("excel文件的路径"), ExcelModel.class, new ImportParams()); for (ExcelModel data : list) { System.out.println(data.toString()); } // 导出Excel文件 List<ExcelModel> list = new ArrayList<>(); // 添加数据到list中 ExcelExportUtil.exportExcel(new ExportParams("sheet1", "测试"), ExcelModel.class, list, new FileOutputStream("excel文件的路径")); ``` 以上是两种常见的Java操作Excel文件的方法。希望能够帮助到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值