1. /**     
  2.  * 文件名:ExcelReadTest.java     
  3.  * 版本信息:     
  4.  * 日期:2013-5-2     
  5.  * Copyright 足下 Corporation 2013      
  6.  * 版权所有     
  7.  *     
  8.  */ 
  9. package com.august.xgame.server.test; 
  10.  
  11. import java.io.File; 
  12. import java.io.FileInputStream; 
  13. import java.io.IOException; 
  14. import java.text.DecimalFormat; 
  15. import java.text.SimpleDateFormat; 
  16. import java.util.LinkedList; 
  17. import java.util.List; 
  18.  
  19. import org.apache.poi.hssf.usermodel.HSSFDateUtil; 
  20. import org.apache.poi.xssf.usermodel.XSSFCell; 
  21. import org.apache.poi.xssf.usermodel.XSSFRow; 
  22. import org.apache.poi.xssf.usermodel.XSSFSheet; 
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
  24.  
  25. /** 
  26.  * 创建时间:2013-5-2 下午2:09:06 
  27.  */ 
  28. public class ExcelReadTest 
  29.     public static void main(String[] args) throws Exception 
  30.     { 
  31.         readTest(new File("d:/career.xlsx")); 
  32.     } 
  33.  
  34.     // 读取Office 2007excel 
  35.     public static List<List<Object>> readTest(File file) throws Exception 
  36.     { 
  37.         List<List<Object>> list = new LinkedList<List<Object>>(); 
  38.         // 构造XSSFWorkbook对象,strPath传入文件路径 
  39.         XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file)); 
  40.         // 读取第一张表格的内容 
  41.         XSSFSheet sheet = xwb.getSheetAt(0); 
  42.         Object value = null
  43.         XSSFRow row = null
  44.         XSSFCell cell = null
  45.         System.out.println("读取2007excel内容如下:"); 
  46.         for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) 
  47.         { 
  48.             row = sheet.getRow(i); 
  49.             if (row == null
  50.             { 
  51.                 continue
  52.             } 
  53.             List<Object> linked = new LinkedList<Object>(); 
  54.             for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) 
  55.             { 
  56.                 cell = row.getCell(j); 
  57.                 if (cell == null
  58.                 { 
  59.                     continue
  60.                 } 
  61.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number string 
  62.                 // 字符 
  63.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//  格式化日期字符串  
  64.                 DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字 
  65.                 switch (cell.getCellType()) 
  66.                 { 
  67.                 case XSSFCell.CELL_TYPE_STRING: 
  68.                     // System.out.println(i+"行"+j+"列 is String type"); 
  69.                     value = cell.getStringCellValue(); 
  70.                     System.out.println(" " + value + " "); 
  71.                     break
  72.                 case XSSFCell.CELL_TYPE_NUMERIC: 
  73.                     if ("@".equals(cell.getCellStyle().getDataFormatString())) 
  74.                     { 
  75.                         value = df.format(cell.getNumericCellValue()); 
  76.                     } else if ("General".equals(cell.getCellStyle().getDataFormatString())) 
  77.                     { 
  78.                         value = nf.format(cell.getNumericCellValue()); 
  79.                     } else 
  80.                     { 
  81.                         value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); 
  82.                     } 
  83.                     System.out.println(" " + value + " "); 
  84.                     break
  85.                 case XSSFCell.CELL_TYPE_BOOLEAN: 
  86.                     value = cell.getBooleanCellValue(); 
  87.                     System.out.println(" " +value + " "); 
  88.                     break
  89.                 case XSSFCell.CELL_TYPE_BLANK: 
  90.                     value = ""
  91.                     System.out.println(value); 
  92.                     break
  93.                 default
  94.                     value = cell.toString(); 
  95.                     System.out.println(" " + value + " "); 
  96.                     break
  97.                 } 
  98.                 if (value == null || "".equals(value)) 
  99.                 { 
  100.                     continue
  101.                 } 
  102.                 linked.add(value); 
  103.             } 
  104.             System.out.println(""); 
  105.             list.add(linked); 
  106.         } 
  107.         return list; 
  108.     }