java excel 파일 분석 방법

6203 단어 java해석excel
프로젝트를 만들기 전에 POI 패키지를 가져와야 합니다.POI 관련jar 패키지 다운로드 주소:http://poi.apache.org/download.html
1. 해석.xlsx 접미사 이름의 EXCEL 파일:

package com.shuai.hello;  
  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStream;  
  
import org.apache.poi.hssf.usermodel.HSSFCell;  
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 ReadExcel {  
  public static void main(String[] args) throws IOException {  
      
    //File file = new File("C:/Users.xlsx");  
    InputStream stream = new FileInputStream("C:/Users.xlsx");  
  
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(stream);  
    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);  
  
    int rowstart = xssfSheet.getFirstRowNum();  
    int rowEnd = xssfSheet.getLastRowNum();  
    for(int i=rowstart;i<=rowEnd;i++)  
    {  
      XSSFRow row = xssfSheet.getRow(i);  
      if(null == row) continue;  
      int cellStart = row.getFirstCellNum();  
      int cellEnd = row.getLastCellNum();  
  
      for(int k=cellStart;k<=cellEnd;k++)  
      {  
        XSSFCell cell = row.getCell(k);  
        if(null==cell) continue;  
  
  
        switch (cell.getCellType())  
        {  
          case HSSFCell.CELL_TYPE_NUMERIC: //    
            System.out.print(cell.getNumericCellValue()  
                + "\t");  
            break;  
          case HSSFCell.CELL_TYPE_STRING: //    
            System.out.print(cell.getStringCellValue()  
                + "\t");  
            break;  
          case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean  
            System.out.println(cell.getBooleanCellValue()  
                + "\t");  
            break;  
          case HSSFCell.CELL_TYPE_FORMULA: //    
            System.out.print(cell.getCellFormula() + "\t");  
            break;  
          case HSSFCell.CELL_TYPE_BLANK: //    
            System.out.println(" ");  
            break;  
          case HSSFCell.CELL_TYPE_ERROR: //    
            System.out.println(" ");  
            break;  
          default:  
            System.out.print("   ");  
            break;  
        }  
  
      }  
      System.out.print("
"); } } } /*String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length()); InputStream stream = new FileInputStream(filePath); Workbook wb = null; if (fileType.equals("xls")) { wb = new HSSFWorkbook(stream); } else if (fileType.equals("xlsx")) { wb = new XSSFWorkbook(stream); } else { System.out.println(" excel "); }*/
2. 해석 접미사는.xls의 EXCEL 파일:

package com.shuai.hello;  
  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
  
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;  
import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  
public class ReadXls {  
  public static void main(String[] args) throws IOException, IOException {  
    File file = new File("C:/Users/dengta/Desktop/ok1.xls");  
    POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));  
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook(poifsFileSystem);  
    HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);  
  
    int rowstart = hssfSheet.getFirstRowNum();  
    int rowEnd = hssfSheet.getLastRowNum();  
    for(int i=rowstart;i<=rowEnd;i++)  
    {  
      HSSFRow row = hssfSheet.getRow(i);  
      if(null == row) continue;  
      int cellStart = row.getFirstCellNum();  
      int cellEnd = row.getLastCellNum();  
  
      for(int k=cellStart;k<=cellEnd;k++)  
      {  
        HSSFCell cell = row.getCell(k);  
        if(null==cell) continue;  
        //System.out.print("" + k + " ");  
        //System.out.print("type:"+cell.getCellType());  
  
        switch (cell.getCellType())  
        {  
          case HSSFCell.CELL_TYPE_NUMERIC: //    
                  System.out.print(cell.getNumericCellValue()  
                + "  ");  
            break;  
          case HSSFCell.CELL_TYPE_STRING: //    
            System.out.print(cell.getStringCellValue()  
                + "  ");  
            break;  
          case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean  
            System.out.println(cell.getBooleanCellValue()  
                + "  ");  
            break;  
          case HSSFCell.CELL_TYPE_FORMULA: //    
            System.out.print(cell.getCellFormula() + "  ");  
            break;  
          case HSSFCell.CELL_TYPE_BLANK: //    
            System.out.println(" ");  
            break;  
          case HSSFCell.CELL_TYPE_ERROR: //    
            System.out.println(" ");  
            break;  
          default:  
            System.out.print("   ");  
            break;  
        }  
  
      }  
      System.out.print("
"); } } }
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기