zoukankan      html  css  js  c++  java
  • java读取excel

    java读取excel

    使用 jxl读取

    依赖
        
         <dependency>
                <groupId>net.sourceforge.jexcelapi</groupId>
                <artifactId>jxl</artifactId>
                <version>2.6.12</version>
            </dependency>
        
    注意: 读取的excel为 2003版本 ,新版本会报错 Unable to recognize OLE stream
     public static void main(String[] args) {
            File file = new File("C:" + File.separator+ "Users" + File.separator+ "fangbin" + File.separator + "Desktop" + File.separator + "20210527开发需求" + File.separator + "allhosts2003.xls");
            try {
                Workbook workbook = Workbook.getWorkbook(file);
                Sheet sheet = workbook.getSheet(0);
                for (int i = 0; i < sheet.getRows(); i++) {
                    System.out.println(i +" "+ sheet.getRow(i)[0].getContents() + " " + sheet.getRow(i)[1].getContents() );
                    if (StringUtils.isBlank(sheet.getRow(i)[0].getContents())) {
                        break;
                    }
                    /*for (int j = 0 ; j < sheet.getColumns(); j++){
                        Cell cell = sheet.getCell(j,i);
                        System.out.println(cell.getContents() + );
                    }*/
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    HSSFWorkbook

    package com.ij34.util;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DateUtil;
    import org.apache.poi.ss.usermodel.Row;
    
    public class Test02 {
     public static void main(String[] args) throws FileNotFoundException, IOException {
     File excelFile = new File("table01.xls");
     HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
     HSSFSheet sheet = wb.getSheetAt(0);
     
     for (Row row : sheet) {
      for (Cell cell : row) {
      switch (cell.getCellType()) {
      case Cell.CELL_TYPE_STRING://字符串
       System.out.print(cell.getRichStringCellValue().getString());
       System.out.print(" ");
       break;
      case Cell.CELL_TYPE_NUMERIC://数值与日期
       if (DateUtil.isCellDateFormatted(cell)) {
       System.out.print(String.valueOf(cell.getDateCellValue()));
       } else {
       System.out.print(cell.getNumericCellValue());
       }
       System.out.print(" ");
       break;
      case Cell.CELL_TYPE_BOOLEAN://boolean类型
       System.out.print(cell.getBooleanCellValue());
       System.out.print(" ");
       break;
      default:
      }
      }
      System.out.println();
     }
    }
    }
    
    
  • 相关阅读:
    vim 常用操作
    Zookeeper 一种简单的原子操作机制:
    用习惯的vimrc配置,在这里记录一下,以后可以继续完善使用
    static_cast, dynamic_cast, const_cast探讨【转】
    常用CSS标签使用
    Java基础
    Hibernate的第一个程序
    Hibernate的优缺点
    python基础语法
    ansible-role安装nginx,keepalived,tomcat
  • 原文地址:https://www.cnblogs.com/fb010001/p/14814971.html
Copyright © 2011-2022 走看看