POI 讀取EXCEL實現程式_PHP教程

來源:互聯網
上載者:User
代碼如下 複製代碼

/*
* 使用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;
}
}


http://www.bkjia.com/PHPjc/444639.htmlwww.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...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.