zoukankan      html  css  js  c++  java
  • JXl常用解析详解

    目的:

    java解析 excel 无非就是apache poi 或者 jxl 两者在使用上其实都差不多,关键还是看你自己熟悉那个,用那个!我也是初次接触jxl 看很多博客说 jxl只适用于处理小数据量 excel,或者说是功能比较单一的,实际上我看了jxl的包,发现其实用

    好了,功能还是很强大的。

    需要了解:支持 Reads data from Excel 95, 97, 2000, XP, and 2003 workbooks

    jxl.read.biff.BiffException: Unable to recognize OLE stream  出现这个错误就是excel 2007格式不符合引起的

    官网:http://jexcelapi.sourceforge.net/

    java doc: http://jxl.sourceforge.net/javadoc/index.html

    依赖管理:

    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.12</version>
    </dependency>

    UML大纲:

    创建简单的excel:

     1 @Test
     2     public void testCreateExcel() {
     3         try {
     4             // 创建xls文件
     5             file.createNewFile();
     6             // 2:创建工作簿
     7             WritableWorkbook workbook = Workbook.createWorkbook(file);
     8 
     9             // 3:创建sheet,设置第二三四..个sheet,依次类推即可
    10             WritableSheet sheet = workbook.createSheet("测试", 0);
    11             // 4:设置titles
    12             String[] titles = { "编号", "账号"};
    13             // 5:给第一行设置列名
    14             for (int i = 0; i < titles.length; i++) {
    15                 sheet.addCell(new Label(i, 0, titles[i]));
    16             }
    17             sheet.setHeader("aa", "cc", "cc");
    18             // 6:模拟数据库导入数据 注意起始行为1
    19             for (int i = 1; i < 100; i++) {
    20                 //添加编号
    21                 sheet.addCell(new Label(0, i, new String("编号"+i)));
    22                 //添加密码
    23                 sheet.addCell(new Label(1, i, new String("编号"+i)));
    24             }
    25             workbook.write();
    26             workbook.close();
    27         } catch (IOException e) {
    28             e.printStackTrace();
    29         } catch (RowsExceededException e) {
    30             e.printStackTrace();
    31         } catch (WriteException e) {
    32             e.printStackTrace();
    33         }
    34     }

     效果:

     

     

    简单读取Excel:

     1 @Test
     2     public void testCreateExcel() {
     3         try {
     4             //1:创建workbook
     5             Workbook workbook = Workbook.getWorkbook(file);
     6             //2:获取第一个工作表sheet
     7             Sheet sheet = workbook.getSheet(0);
     8              //3:读取数据
     9             System.out.println(sheet.getColumns());
    10             System.out.println(sheet.getRows());
    11             //4.自己注意行列关系
    12             for (int i = 0; i < sheet.getRows(); i++) {
    13                 for (int j = 0; j < sheet.getColumns(); j++) {
    14                     Cell cell = sheet.getCell(j, i);
    15                     System.out.println(cell.getContents());
    16                 }
    17             }
    18         } catch (BiffException | IOException e) {
    19             e.printStackTrace();
    20         }
    21     }

  • 相关阅读:
    2021NUAA暑假集训 Day3 题解
    2021NUAA暑假集训 Day2 题解
    2021NUAA暑期模拟赛部分题解
    CodeForces 1038D Slime
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 10689 Yet another Number Sequence
    HDU 4549 M斐波那契数列
    HDU 4990 Reading comprehension
    CodeForces 450B Jzzhu and Sequences
  • 原文地址:https://www.cnblogs.com/dgwblog/p/9368700.html
Copyright © 2011-2022 走看看