Heim > php教程 > php手册 > Hauptteil

POI 读取EXCEL实现程序

WBOY
Freigeben: 2016-06-13 11:27:35
Original
950 Leute haben es durchsucht

 代码如下 复制代码

 

/*
  * 使用POI读取EXCEL文件
  */
 import java.io.File;
 import java.io.FileInputStream;
 import java.util.ArrayList;
 
 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;
 
 /**
  *
  * @author Hanbin
  */
 public class ReadExcel {
 
     /**
      * @param args the command line arguments
      */
     public static void main(String[] args)throws Exception {
         read("d:demo.xls");
     }
    
     public static ArrayList read(String fileName){
         ArrayList list = new ArrayList();
         String sql = "";
         try{
             File f = new File(fileName);
             FileInputStream fis = new FileInputStream(f);
             HSSFWorkbook wbs = new HSSFWorkbook(fis);
             HSSFSheet childSheet = wbs.getSheetAt(0);
             System.out.println("行数:" + childSheet.getLastRowNum());
             for(int i = 4;i                 HSSFRow row = childSheet.getRow(i);
                 System.out.println("列数:" + row.getPhysicalNumberOfCells());
                 if(null != row){
                     for(int k=1;k                         HSSFCell cell;
                         cell = row.getCell((short)k);
                        // System.out.print(getStringCellValue(cell) + "t");
                         list.add(getStringCellValue(cell) + "t");
                     }
                 }
             }
         }catch(Exception e){
             e.printStackTrace();
         }
         return list;
     }
     /**
      * 获取单元格数据内容为字符串类型的数据
      *
      * @param cell Excel单元格
      * @return String 单元格数据内容
      */
     private static String getStringCellValue(HSSFCell cell) {
         String strCell = "";
         switch (cell.getCellType()) {
         case HSSFCell.CELL_TYPE_STRING:
             strCell = cell.getStringCellValue();
             break;
         case HSSFCell.CELL_TYPE_NUMERIC:
             strCell = String.valueOf(cell.getNumericCellValue());
             break;
         case HSSFCell.CELL_TYPE_BOOLEAN:
             strCell = String.valueOf(cell.getBooleanCellValue());
             break;
         case HSSFCell.CELL_TYPE_BLANK:
             strCell = "";
             break;
         default:
             strCell = "";
             break;
         }
         if (strCell.equals("") || strCell == null) {
             return "";
         }
         if (cell == null) {
             return "";
         }
         return strCell;
     }
 }


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!