使用poi解析Excel

使用poi来解析Excel的xls和xlsx。

解析xls:
Java代码 使用poi解析Excel 使用poi解析Excel使用poi解析Excel
  1. package xls;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import org.apache.poi.hssf.usermodel.HSSFCell;  
  7. import org.apache.poi.hssf.usermodel.HSSFRow;  
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10.   
  11. public class XlsMain {  
  12.   
  13.   public static void main( String[] args) throws IOException {  
  14.     XlsMain xlsMain = new XlsMain();  
  15.       
  16.     xlsMain.readXls();  
  17.   }  
  18.   
  19.   private void readXls() throws IOException{  
  20.     InputStream is = new FileInputStream( "D:\\excel\\xls_test2.xls");  
  21.     HSSFWorkbook hssfWorkbook = new HSSFWorkbook( is);   
  22.       
  23.     // 循环工作表Sheet  
  24.     for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){  
  25.       HSSFSheet hssfSheet = hssfWorkbook.getSheetAt( numSheet);  
  26.       if(hssfSheet == null){  
  27.         continue;  
  28.       }  
  29.         
  30.       // 循环行Row   
  31.       for(int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++){  
  32.         HSSFRow hssfRow = hssfSheet.getRow( rowNum);  
  33.         if(hssfRow == null){  
  34.           continue;  
  35.         }  
  36.           
  37.         // 循环列Cell    
  38.         for(int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++){  
  39.           HSSFCell hssfCell = hssfRow.getCell( cellNum);  
  40.           if(hssfCell == null){  
  41.             continue;  
  42.           }  
  43.             
  44.           System.out.print("    " + getValue( hssfCell));  
  45.         }  
  46.         System.out.println();  
  47.       }  
  48.     }  
  49.   }  
  50.     
  51.   @SuppressWarnings("static-access")  
  52.   private String getValue(HSSFCell hssfCell){  
  53.     if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){  
  54.       return String.valueOf( hssfCell.getBooleanCellValue());  
  55.     }else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){  
  56.       return String.valueOf( hssfCell.getNumericCellValue());  
  57.     }else{  
  58.       return String.valueOf( hssfCell.getStringCellValue());  
  59.     }  
  60.   }  
  61.     
  62. }  
package xls;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class XlsMain {

  public static void main( String[] args) throws IOException {
    XlsMain xlsMain = new XlsMain();
    
    xlsMain.readXls();
  }

  private void readXls() throws IOException{
    InputStream is = new FileInputStream( "D:\\excel\\xls_test2.xls");
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook( is); 
    
    // 循环工作表Sheet
    for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
      HSSFSheet hssfSheet = hssfWorkbook.getSheetAt( numSheet);
      if(hssfSheet == null){
        continue;
      }
      
      // 循环行Row 
      for(int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
        HSSFRow hssfRow = hssfSheet.getRow( rowNum);
        if(hssfRow == null){
          continue;
        }
        
        // 循环列Cell  
        for(int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++){
          HSSFCell hssfCell = hssfRow.getCell( cellNum);
          if(hssfCell == null){
            continue;
          }
          
          System.out.print("    " + getValue( hssfCell));
        }
        System.out.println();
      }
    }
  }
  
  @SuppressWarnings("static-access")
  private String getValue(HSSFCell hssfCell){
    if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){
      return String.valueOf( hssfCell.getBooleanCellValue());
    }else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){
      return String.valueOf( hssfCell.getNumericCellValue());
    }else{
      return String.valueOf( hssfCell.getStringCellValue());
    }
  }
  
}



解析xlsx:
Java代码 使用poi解析Excel 使用poi解析Excel使用poi解析Excel
  1. package xlsx;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.poi.xssf.usermodel.XSSFCell;  
  5. import org.apache.poi.xssf.usermodel.XSSFRow;  
  6. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  7. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  8.   
  9. public class XlsxMain {  
  10.   
  11.   public static void main( String[] args) throws IOException {  
  12.     XlsxMain xlsxMain = new XlsxMain();  
  13.       
  14.     xlsxMain.readXlsx();  
  15.   }  
  16.   
  17.   private void readXlsx() throws IOException{  
  18.     String fileName = "D:\\excel\\xlsx_test.xlsx";  
  19.     XSSFWorkbook xssfWorkbook = new XSSFWorkbook( fileName);  
  20.       
  21.     // 循环工作表Sheet  
  22.     for(int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++){  
  23.       XSSFSheet xssfSheet = xssfWorkbook.getSheetAt( numSheet);  
  24.       if(xssfSheet == null){  
  25.         continue;  
  26.       }  
  27.         
  28.       // 循环行Row   
  29.       for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++ ){  
  30.         XSSFRow xssfRow = xssfSheet.getRow( rowNum);  
  31.         if(xssfRow == null){  
  32.           continue;  
  33.         }  
  34.           
  35.         // 循环列Cell     
  36.         for(int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++){  
  37.           XSSFCell xssfCell = xssfRow.getCell( cellNum);  
  38.           if(xssfCell == null){  
  39.             continue;  
  40.           }  
  41.           System.out.print("   "+getValue(xssfCell));  
  42.         }  
  43.         System.out.println();  
  44.       }  
  45.     }  
  46.   }  
  47.     
  48.   @SuppressWarnings("static-access")  
  49.   private String getValue(XSSFCell xssfCell){  
  50.     if(xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN){  
  51.       return String.valueOf( xssfCell.getBooleanCellValue());  
  52.     }else if(xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC){  
  53.       return String.valueOf( xssfCell.getNumericCellValue());  
  54.     }else{  
  55.       return String.valueOf( xssfCell.getStringCellValue());  
  56.     }  
  57.   }  
  58.     
  59. }  
package xlsx;

import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XlsxMain {

  public static void main( String[] args) throws IOException {
    XlsxMain xlsxMain = new XlsxMain();
    
    xlsxMain.readXlsx();
  }

  private void readXlsx() throws IOException{
    String fileName = "D:\\excel\\xlsx_test.xlsx";
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook( fileName);
    
    // 循环工作表Sheet
    for(int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++){
      XSSFSheet xssfSheet = xssfWorkbook.getSheetAt( numSheet);
      if(xssfSheet == null){
        continue;
      }
      
      // 循环行Row 
      for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++ ){
        XSSFRow xssfRow = xssfSheet.getRow( rowNum);
        if(xssfRow == null){
          continue;
        }
        
        // 循环列Cell   
        for(int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++){
          XSSFCell xssfCell = xssfRow.getCell( cellNum);
          if(xssfCell == null){
            continue;
          }
          System.out.print("   "+getValue(xssfCell));
        }
        System.out.println();
      }
    }
  }
  
  @SuppressWarnings("static-access")
  private String getValue(XSSFCell xssfCell){
    if(xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN){
      return String.valueOf( xssfCell.getBooleanCellValue());
    }else if(xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC){
      return String.valueOf( xssfCell.getNumericCellValue());
    }else{
      return String.valueOf( xssfCell.getStringCellValue());
    }
  }
  
}

  • lib.rar (7.2 MB)
  • 下载次数: 1707
上一篇:HTTP 头部字段 Cache Control max-age = 0 和 no-cache 的区别


下一篇:Spring环境中支持多数据库源连接的配置及使用方法