jxls读取excel中所有的数据(最基础的读取)

jxls读取excel中所有的数据

代码很简单,但是io的异常问题比较多,方便阅读就直接抛出异常
下面是maven依赖(也就是jxls读取的依赖,多加了个lombok)
        <dependency>
            <groupId>net.sf.jxls</groupId>
            <artifactId>jxls-reader</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.0.13</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-jexcel</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
测试的实体类Student(忽略了getter setter 构造等 推荐使用lombok)
public class Student {
    private int id;
    private String name;
    private int age;
    private int math;
    private int chinese;
}
这个是我测试的表

在这里插入图片描述

最为关键的xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<workbook>
    <worksheet name="Sheet1">
        <!-- 没有loop包着的section是直接读取准确的行,这里为了不读取上面的标题,所以没有用mapping赋值参数 -->
        <section startRow="0" endRow="0"/>
        <loop items="studentList" var="student" varType="com.example.demo.gong.jxls.Student">
            <!-- 有loop包着的是循环获取,从当前行开始 -->
            <section startRow="1" endRow="1">
                <mapping row="1" col="0">student.id</mapping>
                <mapping row="1" col="1">student.name</mapping>
                <mapping row="1" col="2">student.age</mapping>
                <mapping row="1" col="3">student.math</mapping>
                <mapping row="1" col="4">student.chinese</mapping>
            </section>
            <loopbreakcondition>
                <rowcheck offset="0">
                    <cellcheck offset="0"></cellcheck>
                </rowcheck>
            </loopbreakcondition>
        </loop>
    </worksheet>
</workbook>
xml文件是最关键的,可以自定义读取准确的单元数据,也可以循环读取,可以多尝试

代码

public class JXLSGetXls {
    public static void main(String[] args) throws IOException, SAXException, InvalidFormatException {
        //需要导出的参数的xls文件路径
        String path = "D:\\mission\\src\\main\\java\\com\\example\\demo\\gong\\jxls\\student.xls";
        //配置文件路径
        String xmlPath = "D:\\mission\\src\\main\\resources\\student.xml";
        InputStream inputXML = new BufferedInputStream(new FileInputStream(xmlPath));
        InputStream inputXLS = new BufferedInputStream(new FileInputStream(new File(path)));
        XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
        //这个参数对应xml配置文件的参数
        List<Student> studentList = new ArrayList<>();
        Map<String, Object> beans = new HashMap<>();
        beans.put("studentList", studentList);
        //read是读取数据
        XLSReadStatus readStatus = mainReader.read(inputXLS, beans);
        //是否读取成功
        boolean statusOK = readStatus.isStatusOK();
        System.out.println("读取结果:" + statusOK);
        //测试结果
        for (Student stu : studentList) {
            System.out.println(stu);
        }
    }
}

简单说一些小坑

	配置文件里面有两种读取,一个被loop包着还有一个是没有被loop包着的,像我这个表格第一行是属性名称,不是具体的参数,就可以用不被loop包着的section,然后starerow跟endrow都是0,相当于读取掉了这行数据,然后再开始就是读下面的
	如果直接是全部数据的话,就不需要这样了

小白记录一下,不对之处欢迎留言

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jxls是一个开源的Java工具,可以根据Excel模板文件生成Excel文件。jxls支持复杂的Excel模板,可以在模板包含多个工作表、多个单元格样式、公式等。 下面是使用jxls导出Excel的步骤: 1. 创建Excel模板文件,可以使用Excel或者其他电子表格软件创建,也可以使用jxls提供的Excel模板文件样例。 2. 在Java代码使用jxls API读取Excel模板文件,并将要填充到Excel文件数据传递给jxls。 3. 在Excel模板文件,使用jxls提供的标记语言标记待填充的单元格或区域。 4. 使用jxls API将填充好数据Excel文件输出到指定位置。 下面是一个简单的示例: 1. 创建Excel模板文件,假设文件名为template.xlsx,包含两个工作表Sheet1和Sheet2,每个工作表包含一个表格,表格包含两个单元格A1和B1,A1单元格填充姓名,B1单元格填充年龄。 2. 在Java代码,使用jxls API读取Excel模板文件,准备要填充到Excel文件数据: ```java InputStream is = new FileInputStream(new File("template.xlsx")); OutputStream os = new FileOutputStream(new File("output.xlsx")); Map<String, Object> model = new HashMap<String, Object>(); List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Alice", 25)); persons.add(new Person("Bob", 30)); model.put("persons", persons); ``` 3. 在Excel模板文件,使用jxls提供的标记语言标记待填充的单元格或区域。在A1单元格插入${person.name},在B1单元格插入${person.age},表示在Excel文件填充persons集合的每个Person对象的name和age属性。 4. 使用jxls API将填充好数据Excel文件输出到指定位置: ```java XLSTransformer transformer = new XLSTransformer(); Workbook workbook = transformer.transformXLS(is, model); workbook.write(os); os.flush(); os.close(); is.close(); ``` 这样,就可以根据复杂模板导出Excel文件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值