Java Struts2讀取Excel 2003/2007/2010例子

Java讀寫Excel的包是Apache POI(項目地址:http://poi.apache.org/),所以須要先獲取POI的jar包,本實驗使用的是POI 3.9穩定版。html

Apache POI 代碼例子地址:http://poi.apache.org/spreadsheet/quick-guide.htmlc++

本例子能夠讀取Microsoft Office Excel 2003/2007/2010,具體代碼及註釋以下:apache

 

讀取「.xls」格式使用  import org.apache.poi.hssf.usermodel.*; 包的內容,例如:HSSFWorkbook
讀取「.xlsx」格式使用 import org.apache.poi.xssf.usermodel.*; 包的內容,例如:XSSFWorkbook
讀取兩種格式使用    import org.apache.poi.ss.usermodel.*    包的內容,例如:Workbook
xss

引入包以下:ide

1 import org.apache.poi.ss.usermodel.Cell;
2 import org.apache.poi.ss.usermodel.Row;
3 import org.apache.poi.ss.usermodel.Sheet;
4 import org.apache.poi.ss.usermodel.Workbook;
5 import org.apache.poi.ss.usermodel.WorkbookFactory;
6 import org.apache.poi.ss.usermodel.DateUtil;
 1      /**
 2      * 讀取Excel測試,兼容 Excel 2003/2007/2010
 3      */
 4     public String readExcel()
 5     {
 6         SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
 7         try {
 8             //同時支持Excel 200三、2007
 9             File excelFile = new File("/home/zht/test.xls"); //建立文件對象
10             FileInputStream is = new FileInputStream(excelFile); //文件流
11             Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是能夠處理的
12             int sheetCount = workbook.getNumberOfSheets();  //Sheet的數量
13             //遍歷每一個Sheet
14             for (int s = 0; s < sheetCount; s++) {
15                 Sheet sheet = workbook.getSheetAt(s);
16                 int rowCount = sheet.getPhysicalNumberOfRows(); //獲取總行數
17                 //遍歷每一行
18                 for (int r = 0; r < rowCount; r++) {
19                     Row row = sheet.getRow(r);
20                     int cellCount = row.getPhysicalNumberOfCells(); //獲取總列數
21                     //遍歷每一列
22                     for (int c = 0; c < cellCount; c++) {
23                         Cell cell = row.getCell(c);
24                         int cellType = cell.getCellType();
25                         String cellValue = null;
26                         switch(cellType) {
27                             case Cell.CELL_TYPE_STRING: //文本
28                                 cellValue = cell.getStringCellValue();
29                                 break;
30                             case Cell.CELL_TYPE_NUMERIC: //數字、日期
31                                 if(DateUtil.isCellDateFormatted(cell)) {
32                                     cellValue = fmt.format(cell.getDateCellValue()); //日期型
33                                 }
34                                 else {
35                                     cellValue = String.valueOf(cell.getNumericCellValue()); //數字
36                                 }
37                                 break;
38                             case Cell.CELL_TYPE_BOOLEAN: //布爾型
39                                 cellValue = String.valueOf(cell.getBooleanCellValue());
40                                 break;
41                             case Cell.CELL_TYPE_BLANK: //空白
42                                 cellValue = cell.getStringCellValue();
43                                 break;
44                             case Cell.CELL_TYPE_ERROR: //錯誤
45                                 cellValue = "錯誤";
46                                 break;
47                             case Cell.CELL_TYPE_FORMULA: //公式
48                                 cellValue = "錯誤";
49                                 break;
50                             default:
51                                 cellValue = "錯誤";
52                         }
53                         System.out.print(cellValue + "    ");
54                     }
55                     System.out.println();
56                 }
57             }
58 
59         }
60         catch (Exception e) {
61             e.printStackTrace();
62         }
63 
64         return Action.SUCCESS;
65     }

轉自 :http://blog.csdn.net/zht666/article/details/11598711測試

相關文章
相關標籤/搜索