1 import java.io.FileInputStream;-
2 import java.io.FileNotFoundException;-
3 import java.io.IOException;-
4 import org.apache.commons.logging.Log;-
5 import org.apache.commons.logging.LogFactory;-
6 import org.apache.poi.hssf.usermodel.HSSFCell;-
7 import org.apache.poi.hssf.usermodel.HSSFRow;-
8 import org.apache.poi.hssf.usermodel.HSSFSheet;-
9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;-
10
11 public class TestExcel {-
12 //記錄類(lèi)的輸出信息-
13 static Log log = LogFactory.getLog(TestExcel.class); -
14 //獲取Excel文檔的路徑-
15 public static String filePath = "D://excel.xls";-
16 public static void main(String[] args) {-
17 try {-
18 // 創(chuàng )建對Excel工作簿文件的引用-
19 HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));-
21 // 在Excel文檔中,第一張工作表的缺省索引是0
22 // 其語(yǔ)句為:HSSFSheet sheet = workbook.getSheetAt(0);-
23 HSSFSheet sheet = wookbook.getSheet("Sheet1");-
25 //獲取到Excel文件中的所有行數-
26 int rows = sheet.getPhysicalNumberOfRows();
28 //遍歷行-
29 for (int i = 0; i < rows; i++) {-
30 // 讀取左上端單元格-
31 HSSFRow row = sheet.getRow(i);-
32 // 行不為空-
33 if (row != null) {-
34 //獲取到Excel文件中的所有的列-
35 int cells = row.getPhysicalNumberOfCells();-
36 String value = ""; -
37 //遍歷列-
38 for (int j = 0; j < cells; j++) {-
39 //獲取到列的值-
40 HSSFCell cell = row.getCell(j);-
41 if (cell != null) {-
42 switch (cell.getCellType()) {-
43 case HSSFCell.CELL_TYPE_FORMULA:-
44 break;-
45 case HSSFCell.CELL_TYPE_NUMERIC:-
46 value += cell.getNumericCellValue() + ","; -
47 break; -
48 case HSSFCell.CELL_TYPE_STRING:-
49 value += cell.getStringCellValue() + ",";-
50 break;-
51 default:-
52 value += "0";-
53 break;-
54 }-
55 }
56 }-
57 // 將數據插入到mysql數據庫中-
58 String[] val = value.split(",");-
59 TestEntity entity = new TestEntity();-
60 entity.setNum1(val[0]);-
61 entity.setNum2(val[1]);-
62 entity.setNum3(val[2]);-
63 entity.setNum4(val[3]);-
64 entity.setNum5(val[4]);-
65 entity.setNum6(val[5]);-
66 TestMethod method = new TestMethod();-
67 method.Add(entity);-
68 }-
69 }-
70 } catch (FileNotFoundException e) {-
71 e.printStackTrace();-
72 } catch (IOException e) {-
73 e.printStackTrace();-
74 }-
75 }-
76 }-
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。