Java使用poi读取Excel文件例子

来源:互联网 发布:表情微笑知乎 编辑:程序博客网 时间:2024/04/30 14:28
Java代码  收藏代码
  1.  
  2. package excel;  
  3.   
  4.   
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.IOException;  
  9. import java.text.DecimalFormat;  
  10. import java.text.SimpleDateFormat;  
  11. import java.util.LinkedList;  
  12. import java.util.List;  
  13.   
  14. import org.apache.poi.hssf.usermodel.HSSFCell;  
  15. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
  16. import org.apache.poi.hssf.usermodel.HSSFRow;  
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  19. import org.apache.poi.xssf.usermodel.XSSFCell;  
  20. import org.apache.poi.xssf.usermodel.XSSFRow;  
  21. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  23. /** 
  24.  * 读取excel文件 
  25.  * @author Jiacheng 
  26.  *Poi解析2003时使用的是HSSFCell,而2007的则是  
  27.     XSSFCell,是完全不同的两套API 
  28.     必须先要判断excel的类型,不过 HSSFWorkbook 和 XSSFWorkbook 实现的接口都是一样的Workbook,直接在实例化接口的时候有点区别其他时候没有任何差异。 
  29.  */ 下载
  30. public class ReadExcelUtil {  
  31.     public static List<List<Object>> readExcel(File file) throws IOException {  
  32.         String fileName = file.getName();  
  33.         String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName  
  34.                 .substring(fileName.lastIndexOf(".") + 1);  
  35.         if ("xls".equals(extension)) {  
  36.             return read2003Excel(file);  
  37.         } else if ("xlsx".equals(extension)) {  
  38.             return read2007Excel(file);  
  39.         } else {  
  40.             throw new IOException("不支持的文件类型");  
  41.         }  
  42.     }  
  43.   
  44.     /** 
  45.      * 读取 office 2003 excel 
  46.      *  
  47.      * @throws IOException 
  48.      * @throws FileNotFoundException 
  49.      */  
  50.     private static List<List<Object>> read2003Excel(File file)  
  51.             throws IOException {  
  52.         List<List<Object>> list = new LinkedList<List<Object>>();  
  53.         HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));  
  54.         HSSFSheet sheet = hwb.getSheetAt(0);  
  55.         Object value = null;  
  56.         HSSFRow row = null;  
  57.         HSSFCell cell = null;  
  58.         int counter = 0;  
  59.         for (int i = sheet.getFirstRowNum(); counter < sheet  
  60.                 .getPhysicalNumberOfRows(); i++) {  
  61. //          if(i==0){  
  62. //              //跳过第一行  
  63. //              continue;  
  64. //          }  
  65.             row = sheet.getRow(i);  
  66.             List<Object> linked = new LinkedList<Object>();  
  67.             for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  68.                 cell = row.getCell(j);  
  69.                 if (cell == null) {  
  70.                     value="无";//导入不能为空  
  71.                     linked.add(value);  
  72.                     //System.out.println(value);  
  73.                     continue;  
  74.                 }  
  75.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String  
  76.                                                             // 字符  
  77.                 SimpleDateFormat sdf = new SimpleDateFormat(  
  78.                         "yyyy-MM-dd");// 格式化日期字符串  
  79.                 DecimalFormat nf = new DecimalFormat("0");// 格式化数字  
  80.                 switch (cell.getCellType()) {  
  81.                 case XSSFCell.CELL_TYPE_STRING:  
  82.                     value = cell.getStringCellValue();  
  83.                     //System.out.println(i + "行" + j + " 列 is String type" +"  "+value);  
  84.                     break;  下载
  85.                 case XSSFCell.CELL_TYPE_NUMERIC:  
  86. //                  System.out.println(i + "行" + j  
  87. //                          + " 列 is Number type ; DateFormt:"  
  88. //                          + cell.getCellStyle().getDataFormatString());  
  89.                     if ("@".equals(cell.getCellStyle().getDataFormatString())) {  
  90.                         value = df.format(cell.getNumericCellValue());  
  91.                     } else if ("General".equals(cell.getCellStyle()  
  92.                             .getDataFormatString())) {  
  93.                         value = nf.format(cell.getNumericCellValue());  
  94.                     } else {  
  95.                         value = sdf.format(HSSFDateUtil.getJavaDate(cell  
  96.                                 .getNumericCellValue()));  
  97.                     }  
  98.                     break;  
  99.                 case XSSFCell.CELL_TYPE_BOOLEAN:  
  100.                     value = cell.getBooleanCellValue();  
  101.                     break;  
  102.                 case XSSFCell.CELL_TYPE_BLANK:  
  103.                     value = "";  
  104.                     break;  
  105.                 default:  
  106.                     value = cell.toString();  
  107.                 }  
  108.                 if (value == null || "".equals(value)) {  
  109.                     value="无";//导入不能为空  
  110.                 }  
  111.                 //System.out.println(value);  
  112.                 linked.add(value);  
  113.             }  
  114.             list.add(linked);  
  115.         }  
  116.         return list;  
  117.     }  
  118.   
  119.     /** 
  120.      * 读取Office 2007 excel 
  121.      * */  
  122.     private static List<List<Object>> read2007Excel(File file)  
  123.             throws IOException {  
  124.         List<List<Object>> list = new LinkedList<List<Object>>();  
  125.         // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  126.         XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
  127.         // 读取第一章表格内容  
  128.         XSSFSheet sheet = xwb.getSheetAt(0);  
  129.         Object value = null;  
  130.         XSSFRow row = null;  
  131.         XSSFCell cell = null;  
  132.         int counter = 0;  
  133.         for (int i = sheet.getFirstRowNum(); counter < sheet  
  134.                 .getPhysicalNumberOfRows(); i++) {  
  135.             if(i==0){  
  136.                 //跳过第一行  
  137.                 continue;  
  138.             }  下载
  139.             row = sheet.getRow(i);  
  140.             if (row == null) {  
  141.                  break;  
  142.             }  
  143.             List<Object> linked = new LinkedList<Object>();  
  144.             for (int j = row.getFirstCellNum(); j <row.getLastCellNum(); j++) {  
  145.                 cell = row.getCell(j);  
  146.                 if (cell == null) {  
  147.                     value="无";//导入不能为空  
  148.                     linked.add(value);  
  149.                     //System.out.println(value);  
  150.                     continue;  
  151.                 }  
  152.                 //System.out.println(value);  
  153.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String  
  154.                                                             // 字符  
  155.                 SimpleDateFormat sdf = new SimpleDateFormat(  
  156.                         "yyyy-MM-dd");// 格式化日期字符串  
  157.                 DecimalFormat nf = new DecimalFormat("0");// 格式化数字  
  158.                 switch (cell.getCellType()) {  
  159.                         case XSSFCell.CELL_TYPE_STRING:  
  160.                             //System.out.println(i + "行" + j + " 列 is String type");  
  161.                             value = cell.getStringCellValue();  
  162.                             break;  
  163.                         case XSSFCell.CELL_TYPE_NUMERIC:  
  164.                         //  System.out.println(i + "行" + j  
  165.                                 //  + " 列 is Number type ; DateFormt:"  
  166.                                 //  + cell.getCellStyle().getDataFormatString());  
  167.                             if ("@".equals(cell.getCellStyle().getDataFormatString())) {  
  168.                                 value = df.format(cell.getNumericCellValue());  
  169.                             } else if ("General".equals(cell.getCellStyle()  
  170.                                     .getDataFormatString())) {  
  171.                                 value = nf.format(cell.getNumericCellValue());  
  172.                             } else {  
  173.                                 value = sdf.format(HSSFDateUtil.getJavaDate(cell  
  174.                                         .getNumericCellValue()));  
  175.                             }  
  176.                             break;  
  177.                         case XSSFCell.CELL_TYPE_BOOLEAN:  
  178.                             value = cell.getBooleanCellValue();  
  179.                             break;  
  180.                         case XSSFCell.CELL_TYPE_BLANK://空格,空白  
  181.                             value = "";  
  182.                             break;  
  183.                         default:  
  184.                             value = cell.toString();  
  185.                 }  
  186.                 if (value == null || "".equals(value)) {  
  187.                     value="无";//导入不能为空  
  188.                 }  
  189.                 //System.out.println(value);  
  190.                 linked.add(value);  
  191.             }  
  192.             list.add(linked);  
  193.         }  
  194.         return list;  
  195.     }  
  196.   
  197.     public static void main(String[] args) {  
  198.         try {  
  199.             int count=0;  
  200.             long time1=System.currentTimeMillis();  
  201.             List<List<Object>> list=readExcel(new File("D:\\test.xlsx"));  
  202.             System.out.println("读取花费时间:"+(System.currentTimeMillis()-time1)/1000.0);  
  203.               
  204.             for(List<Object> list2 :list){  
  205.                 count++;  
  206.                 for(int i=0;i<list2.size();i++){  
  207.                     System.out.print(list2.get(i)+"  ");  
  208.                 }  
  209.                 System.out.println("第"+count+"组");  
  210.             }  
  211.             System.out.println("ok!");  
  212.             // readExcel(new File("D:\\test.xls"));  
  213.         } catch (IOException e) {  
  214.             e.printStackTrace();  
  215.         }  
  216.     }  

0 0
原创粉丝点击