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();
     }
    }
    }
    
    
  • 相关阅读:
    微服务
    JNDI Tomcat
    JNDI
    依赖倒置原则
    mac下为gdb创建证书赋权其调试其它应用
    sed相关
    关于autoconf
    mac相关
    about gnu bash shell
    关于gcc
  • 原文地址:https://www.cnblogs.com/fb010001/p/14814971.html
Copyright © 2011-2022 走看看