POI 读取EXCEL实现程序 - php类库
来源:自学PHP网
时间:2014-11-30 12:53 作者:
阅读:次
[导读] /**使用POI读取EXCEL文件*/importjava.io.File;importjava.io.FileInputStream;importjava.util.ArrayList;importorg.apache.poi.hssf.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;
-
-
-
-
-
- public class ReadExcel {
-
-
-
-
- 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<childSheet.getLastRowNum();i++){
- HSSFRow row = childSheet.getRow(i);
- System.out.println("列数:" + row.getPhysicalNumberOfCells());
- if(null != row){
- for(int k=1;k<row.getPhysicalNumberOfCells();k++){
- HSSFCell cell;
- cell = row.getCell((short)k);
-
- list.add(getStringCellValue(cell) + "t");
- }
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return list;
- }
-
-
-
-
-
-
- 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;
- }
- }
|