Home  >  Article  >  Backend Development  >  POI reading EXCEL implementation program_PHP tutorial

POI reading EXCEL implementation program_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:461253browse

The code is as follows Copy code

/*
  * 使用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;
     }
     /**
* Get cell data content as string type data
* *
* @param cell Excel cell
* @return String cell data content
*/
     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;
     }
 }


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444639.htmlTechArticle代码如下 复制代码 /* * 使用POI读取EXCEL文件 */ import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import org.apache.poi.hssf.usermodel.H...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn