Blame view

src/com/fh/util/ObjectExcelRead.java 2.25 KB
ad5081d3   孙向锦   初始化项目
1
2
3
4
5
6
7
8
9
10
11
12
  package com.fh.util;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.util.ArrayList;
  import java.util.List;
  
  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;
  
053a45fd   孙向锦   解析excel bug,去掉空行
13
14
15
  /**
   * @version
   */
ad5081d3   孙向锦   初始化项目
16
17
18
  public class ObjectExcelRead {
  
  	/**
053a45fd   孙向锦   解析excel bug,去掉空行
19
20
21
22
23
24
25
26
27
28
  	 * @param filepath
  	 *            //文件路径
  	 * @param filename
  	 *            //文件名
  	 * @param startrow
  	 *            //开始行号
  	 * @param startcol
  	 *            //开始列号
  	 * @param sheetnum
  	 *            //sheet
ad5081d3   孙向锦   初始化项目
29
30
  	 * @return list
  	 */
053a45fd   孙向锦   解析excel bug,去掉空行
31
32
  	public static List<Object> readExcel(String filepath, String filename,
  			int startrow, int startcol, int sheetnum) {
ad5081d3   孙向锦   初始化项目
33
34
35
36
37
38
  		List<Object> varList = new ArrayList<Object>();
  
  		try {
  			File target = new File(filepath, filename);
  			FileInputStream fi = new FileInputStream(target);
  			HSSFWorkbook wb = new HSSFWorkbook(fi);
053a45fd   孙向锦   解析excel bug,去掉空行
39
40
41
  			HSSFSheet sheet = wb.getSheetAt(sheetnum); // sheet 从0开始
  			int rowNum = sheet.getLastRowNum() + 1; // 取得最后一行的行号
  			for (int i = startrow; i < rowNum; i++) { // 行循环开始
ad5081d3   孙向锦   初始化项目
42
  				PageData varpd = new PageData();
053a45fd   孙向锦   解析excel bug,去掉空行
43
44
45
  				HSSFRow row = sheet.getRow(i); // 行
  				int cellNum = row.getLastCellNum(); // 每行的最后一个单元格位置
  				for (int j = startcol; j < cellNum; j++) { // 列循环开始
ad5081d3   孙向锦   初始化项目
46
47
48
  					HSSFCell cell = row.getCell(Short.parseShort(j + ""));
  					String cellValue = null;
  					if (null != cell) {
053a45fd   孙向锦   解析excel bug,去掉空行
49
50
51
52
53
54
  						int type = cell.getCellType();
  						if (type == 0) {
  							cellValue = String.valueOf((long) cell
  									.getNumericCellValue());
  						}
  						if (type == 1) {
ad5081d3   孙向锦   初始化项目
55
  							cellValue = cell.getStringCellValue();
053a45fd   孙向锦   解析excel bug,去掉空行
56
57
  						}
  						if (type == 2) {
ad5081d3   孙向锦   初始化项目
58
  							cellValue = cell.getNumericCellValue() + "";
053a45fd   孙向锦   解析excel bug,去掉空行
59
60
  						}
  						if (type == 3) {
ad5081d3   孙向锦   初始化项目
61
  							cellValue = "";
053a45fd   孙向锦   解析excel bug,去掉空行
62
63
64
65
66
67
68
69
  						}
  						if (type == 4) {
  							cellValue = String.valueOf(cell
  									.getBooleanCellValue());
  						}
  						if (type == 5) {
  							cellValue = String
  									.valueOf(cell.getErrorCellValue());
ad5081d3   孙向锦   初始化项目
70
71
72
73
  						}
  					} else {
  						cellValue = "";
  					}
053a45fd   孙向锦   解析excel bug,去掉空行
74
75
  					varpd.put("var" + j, cellValue);
  					System.out.println(cellValue);
ad5081d3   孙向锦   初始化项目
76
77
78
79
80
81
82
  				}
  				varList.add(varpd);
  			}
  
  		} catch (Exception e) {
  			System.out.println(e);
  		}
053a45fd   孙向锦   解析excel bug,去掉空行
83
  
ad5081d3   孙向锦   初始化项目
84
85
86
  		return varList;
  	}
  }