dom4j解析xml字符串|java解析excel文件的方法

时间:2021-06-13  来源:apache  阅读:

建立工程前需要导入POI包。POI相关jar包下载地址:http://poi.apache.org/download.html

1.解析.xlsx后缀名的的EXCEL文件:

 代码如下

packagecom.shuai.hello; 

   

importjava.io.FileInputStream; 

importjava.io.IOException; 

importjava.io.InputStream; 

   

importorg.apache.poi.hssf.usermodel.HSSFCell; 

importorg.apache.poi.xssf.usermodel.XSSFCell; 

importorg.apache.poi.xssf.usermodel.XSSFRow; 

importorg.apache.poi.xssf.usermodel.XSSFSheet; 

importorg.apache.poi.xssf.usermodel.XSSFWorkbook; 

   

publicclassReadExcel { 

  publicstaticvoidmain(String[] args)throwsIOException { 

       

    //File file = new File("C:/Users.xlsx"); 

    InputStream stream =newFileInputStream("C:/Users.xlsx"); 

   

    XSSFWorkbook xssfWorkbook =newXSSFWorkbook(stream); 

    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); 

   

    introwstart = xssfSheet.getFirstRowNum(); 

    introwEnd = xssfSheet.getLastRowNum(); 

    for(inti=rowstart;i<=rowEnd;i++) 

    { 

      XSSFRow row = xssfSheet.getRow(i); 

      if(null== row)continue; 

      intcellStart = row.getFirstCellNum(); 

      intcellEnd = row.getLastCellNum(); 

   

      for(intk=cellStart;k<=cellEnd;k++) 

      { 

        XSSFCell cell = row.getCell(k); 

        if(null==cell)continue; 

   

   

        switch(cell.getCellType()) 

        { 

          caseHSSFCell.CELL_TYPE_NUMERIC:// 数字 

            System.out.print(cell.getNumericCellValue() 

                +"\t"); 

            break; 

          caseHSSFCell.CELL_TYPE_STRING:// 字符串 

            System.out.print(cell.getStringCellValue() 

                +"\t"); 

            break; 

          caseHSSFCell.CELL_TYPE_BOOLEAN:// Boolean 

            System.out.println(cell.getBooleanCellValue() 

                +"\t"); 

            break; 

          caseHSSFCell.CELL_TYPE_FORMULA:// 公式 

            System.out.print(cell.getCellFormula() +"\t"); 

            break; 

          caseHSSFCell.CELL_TYPE_BLANK:// 空值 

            System.out.println(" "); 

            break; 

          caseHSSFCell.CELL_TYPE_ERROR:// 故障 

            System.out.println(" "); 

            break; 

          default: 

            System.out.print("未知类型  "); 

            break; 

        } 

   

      } 

      System.out.print("\n"); 

    } 

  } 

  

/*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文件:

 代码如下

packagecom.shuai.hello; 

   

importjava.io.File; 

importjava.io.FileInputStream; 

importjava.io.IOException; 

   

importorg.apache.poi.hssf.usermodel.HSSFCell; 

importorg.apache.poi.hssf.usermodel.HSSFRow; 

importorg.apache.poi.hssf.usermodel.HSSFSheet; 

importorg.apache.poi.hssf.usermodel.HSSFWorkbook; 

importorg.apache.poi.poifs.filesystem.POIFSFileSystem; 

   

publicclassReadXls { 

  publicstaticvoidmain(String[] args)throwsIOException, IOException { 

    File file =newFile("C:/Users/dengta/Desktop/ok1.xls"); 

    POIFSFileSystem poifsFileSystem =newPOIFSFileSystem(newFileInputStream(file)); 

    HSSFWorkbook hssfWorkbook =newHSSFWorkbook(poifsFileSystem); 

    HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0); 

   

    introwstart = hssfSheet.getFirstRowNum(); 

    introwEnd = hssfSheet.getLastRowNum(); 

    for(inti=rowstart;i<=rowEnd;i++) 

    { 

      HSSFRow row = hssfSheet.getRow(i); 

      if(null== row)continue; 

      intcellStart = row.getFirstCellNum(); 

      intcellEnd = row.getLastCellNum(); 

   

      for(intk=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()) 

        { 

          caseHSSFCell.CELL_TYPE_NUMERIC:// 数字 

                  System.out.print(cell.getNumericCellValue() 

                +"  "); 

            break; 

          caseHSSFCell.CELL_TYPE_STRING:// 字符串 

            System.out.print(cell.getStringCellValue() 

                +"  "); 

            break; 

          caseHSSFCell.CELL_TYPE_BOOLEAN:// Boolean 

            System.out.println(cell.getBooleanCellValue() 

                +"  "); 

            break; 

          caseHSSFCell.CELL_TYPE_FORMULA:// 公式 

            System.out.print(cell.getCellFormula() +"  "); 

            break; 

          caseHSSFCell.CELL_TYPE_BLANK:// 空值 

            System.out.println(" "); 

            break; 

          caseHSSFCell.CELL_TYPE_ERROR:// 故障 

            System.out.println(" "); 

            break; 

          default: 

            System.out.print("未知类型  "); 

            break; 

        } 

   

      } 

      System.out.print("\n"); 

    } 

  } 

}

dom4j解析xml字符串|java解析excel文件的方法

http://m.bbyears.com/jiaocheng/123595.html

推荐访问:js解析json数组对象
相关阅读 猜你喜欢
本类排行 本类最新