[Java] java读取excel文件 →→→→→进入此内容的聊天室

来自 , 2021-03-23, 写在 Java, 查看 132 次.
URL http://www.code666.cn/view/abec16f4
  1. /**
  2.  *ParseExcel.java
  3.  *下午02:40:40
  4.  */
  5. package com.zsmj.utilit;
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import org.apache.poi.hssf.usermodel.*;
  10. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  11. /**
  12.  * @author la
  13.  *POI工具读取Excel文件
  14.  */
  15. public class ParseExcel {
  16.        
  17.         private void parseExcel(String excelFile)throws IOException {
  18.                 POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(excelFile));//打开Excel文件
  19.                 HSSFWorkbook wbHssfWorkbook=new HSSFWorkbook(fs);//打开工作薄
  20.                 HSSFSheet sheet=wbHssfWorkbook.getSheetAt(0);//打开工作表
  21.                
  22.                 HSSFRow row=null;
  23.                 String data=null;
  24.                 for (int i = 0; i <=sheet.getLastRowNum(); i++) {//循环读取每一行
  25.                         row =sheet.getRow(i);
  26.                         for (int j = 0; j <= row.getLastCellNum(); j++) {//循环读取每一列
  27.                                 switch (row.getCell((short)j).getCellType()) {//判断单元格的数据类型
  28.                                 case HSSFCell.CELL_TYPE_BLANK:
  29.                                         data="";
  30.                                         break;
  31.                                 case HSSFCell.CELL_TYPE_NUMERIC:
  32.                                         data=(long)row.getCell((short)j).getNumericCellValue()+"";
  33.                                         break;
  34.                                 default:
  35.                                         data=row.getCell((short)j).getStringCellValue();
  36.                                         break;
  37.                                 }
  38.                                 System.out.print(data+"\t");
  39.                         }
  40.                         System.out.println();
  41.                 }
  42.         }
  43.         /**
  44.          * @param args
  45.          *2012-10-23
  46.          *void
  47.          */
  48.         public static void main(String[] args)throws IOException {
  49.                 new ParseExcel().parseExcel(args[0]);
  50.         }
  51.  
  52. }
  53.  
  54. //java/5532

回复 "java读取excel文件"

这儿你可以回复上面这条便签

captcha