Java POI读取Office excel (2003,2007)

 

Java POI读取Office excel (2003,2007)及相关jar包

分类: J2SE   8359人阅读   评论(54)   收藏   举报

目录(?)[+]

poi-3.7-20101029.jar,下载地址:http://download.csdn.net/detail/evangel_z/4107089

geronimo-stax-api_1.0_spec-1.0.jar,下载地址:http://download.csdn.net/detail/evangel_z/4107083

xmlbeans-2.3.0.jar,下载地址:http://download.csdn.net/detail/evangel_z/4107140

poi-ooxml-3.7-20101029.jar,下载地址:http://download.csdn.net/detail/evangel_z/4107145

poi-ooxml-schemas-3.7-20101029.jar,下载地址:http://download.csdn.net/detail/evangel_z/4108997

以上5个jar,就可读取Excel 2003;

----------------------------------------------------------------------------------------------------------------------------------------

读取Excel 2007,请加上dom4j-1.6.1.jar,下载地址:http://download.csdn.net/detail/evangel_z/6739735

----------------------------------------------------------------------------------------------------------------------------------------

poi-3.6-20091214.jar,下载地址:http://download.csdn.net/detail/evangel_z/3895051

poi-contrib-3.6-20091214.jar,下载地址: http://download.csdn.net/detail/evangel_z/4107197

poi-scratch.6-20091214.jar,下载地址:http://download.csdn.net/detail/evangel_z/4107204

读取excel 文件的 java 代码:
[java]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5. import java.text.DecimalFormat;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.LinkedList;  
  8. import java.util.List;  
  9. import org.apache.poi.hssf.usermodel.HSSFCell;  
  10. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
  11. import org.apache.poi.hssf.usermodel.HSSFRow;  
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  14. import org.apache.poi.xssf.usermodel.XSSFCell;  
  15. import org.apache.poi.xssf.usermodel.XSSFRow;  
  16. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  18. public class ReadExcel {  
  19.      /** 
  20.      * 对外提供读取excel 的方法 
  21.      * */  
  22. public static List<List<Object>> readExcel(File file) throws IOException{  
  23.    String fileName = file.getName();  
  24.    String extension = fileName.lastIndexOf(".")==-1?"":fileName.substring(fileName.lastIndexOf(".")+1);  
  25.    if("xls".equals(extension)){  
  26.     return read2003Excel(file);  
  27.    }else if("xlsx".equals(extension)){  
  28.     return read2007Excel(file);  
  29.    }else{  
  30.     throw new IOException("不支持的文件类型");  
  31.    }  
  32. }  
  33. /** 
  34. * 读取 office 2003 excel 
  35. * @throws IOException  
  36. * @throws FileNotFoundException */  
  37. private static List<List<Object>> read2003Excel(File file) throws IOException{  
  38.    List<List<Object>> list = new LinkedList<List<Object>>();  
  39.    HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));  
  40.    HSSFSheet sheet = hwb.getSheetAt(0);  
  41.    Object value = null;  
  42.    HSSFRow row = null;  
  43.    HSSFCell cell = null;   
  44.    for(int i = sheet.getFirstRowNum();i<= sheet.getPhysicalNumberOfRows();i++){  
  45.     row = sheet.getRow(i);  
  46.     if (row == null) {  
  47.      continue;  
  48.     }  
  49.     List<Object> linked = new LinkedList<Object>();  
  50.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  51.      cell = row.getCell(j);  
  52.      if (cell == null) {  
  53.       continue;  
  54.      }  
  55.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符  
  56.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
  57.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字  
  58.      switch (cell.getCellType()) {  
  59.      case XSSFCell.CELL_TYPE_STRING:  
  60.       System.out.println(i+"行"+j+" 列 is String type");  
  61.       value = cell.getStringCellValue();  
  62.       break;  
  63.      case XSSFCell.CELL_TYPE_NUMERIC:  
  64.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());  
  65.       if("@".equals(cell.getCellStyle().getDataFormatString())){  
  66.          value = df.format(cell.getNumericCellValue());  
  67.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){  
  68.          value = nf.format(cell.getNumericCellValue());  
  69.       }else{  
  70.         value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));  
  71.       }  
  72.       break;  
  73.      case XSSFCell.CELL_TYPE_BOOLEAN:  
  74.       System.out.println(i+"行"+j+" 列 is Boolean type");  
  75.       value = cell.getBooleanCellValue();  
  76.       break;  
  77.      case XSSFCell.CELL_TYPE_BLANK:  
  78.       System.out.println(i+"行"+j+" 列 is Blank type");  
  79.       value = "";  
  80.       break;  
  81.      default:  
  82.       System.out.println(i+"行"+j+" 列 is default type");  
  83.       value = cell.toString();  
  84.      }  
  85.      if (value == null || "".equals(value)) {  
  86.       continue;  
  87.      }  
  88.      linked.add(value);    
  89.    }  
  90.     list.add(linked);  
  91.    }  
  92.    return list;  
  93. }  
  94. /** 
  95. * 读取Office 2007 excel 
  96. * */  
  97. private static List<List<Object>> read2007Excel(File file) throws IOException {  
  98.    List<List<Object>> list = new LinkedList<List<Object>>();  
  99.    // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  100.    XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
  101.    // 读取第一章表格内容  
  102.    XSSFSheet sheet = xwb.getSheetAt(0);  
  103.    Object value = null;  
  104.    XSSFRow row = null;  
  105.    XSSFCell cell = null;  
  106.    for (int i = sheet.getFirstRowNum(); i <= sheet  
  107.      .getPhysicalNumberOfRows(); i++) {  
  108.     row = sheet.getRow(i);  
  109.     if (row == null) {  
  110.      continue;  
  111.     }  
  112.     List<Object> linked = new LinkedList<Object>();  
  113.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  114.      cell = row.getCell(j);  
  115.      if (cell == null) {  
  116.       continue;  
  117.      }  
  118.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符  
  119.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
  120.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字  
  121.      switch (cell.getCellType()) {  
  122.      case XSSFCell.CELL_TYPE_STRING:  
  123.       System.out.println(i+"行"+j+" 列 is String type");  
  124.       value = cell.getStringCellValue();  
  125.       break;  
  126.      case XSSFCell.CELL_TYPE_NUMERIC:  
  127.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());  
  128.       if("@".equals(cell.getCellStyle().getDataFormatString())){  
  129.         value = df.format(cell.getNumericCellValue());  
  130.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){  
  131.         value = nf.format(cell.getNumericCellValue());  
  132.       }else{  
  133.        value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));  
  134.       }  
  135.       break;  
  136.      case XSSFCell.CELL_TYPE_BOOLEAN:  
  137.       System.out.println(i+"行"+j+" 列 is Boolean type");  
  138.       value = cell.getBooleanCellValue();  
  139.       break;  
  140.      case XSSFCell.CELL_TYPE_BLANK:  
  141.       System.out.println(i+"行"+j+" 列 is Blank type");  
  142.       value = "";  
  143.       break;  
  144.      default:  
  145.       System.out.println(i+"行"+j+" 列 is default type");  
  146.       value = cell.toString();  
  147.      }  
  148.      if (value == null || "".equals(value)) {  
  149.       continue;  
  150.      }  
  151.      linked.add(value);  
  152.     }  
  153.     list.add(linked);  
  154.    }  
  155.    return list;  
  156. }  
  157. }  

说明:该类中共封装了三个方法,对外提供的读取excel文件的方法,两个私有的分别读取excel2003和excel2007的方法。外部使用,只需调用readExcel 方法,传入一个File 参数,程序根据文件扩展名来判断选取那个方法来读取Excel文件。

附:改进后代码

Q:excel的第1,2行有数据,第3,4行没数据,第5行有数据,第5行数据显示不出来。

A:sheet.getPhysicalNumberOfRows()获取有效的行数,多谢热心网友的指正,改进后代码如下:

[java]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5. import java.text.DecimalFormat;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.LinkedList;  
  8. import java.util.List;  
  9. import org.apache.poi.hssf.usermodel.HSSFCell;  
  10. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
  11. import org.apache.poi.hssf.usermodel.HSSFRow;  
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  14. import org.apache.poi.xssf.usermodel.XSSFCell;  
  15. import org.apache.poi.xssf.usermodel.XSSFRow;  
  16. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  18. public class ReadExcel {  
  19.     /** 
  20.      * 对外提供读取excel 的方法 
  21.      * */  
  22.     public static List<List<Object>> readExcel(File file) throws IOException {  
  23.         String fileName = file.getName();  
  24.         String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName  
  25.                 .substring(fileName.lastIndexOf(".") + 1);  
  26.         if ("xls".equals(extension)) {  
  27.             return read2003Excel(file);  
  28.         } else if ("xlsx".equals(extension)) {  
  29.             return read2007Excel(file);  
  30.         } else {  
  31.             throw new IOException("不支持的文件类型");  
  32.         }  
  33.     }  
  34.     /** 
  35.      * 读取 office 2003 excel 
  36.      * @throws IOException 
  37.      * @throws FileNotFoundException 
  38.      */  
  39.     private static List<List<Object>> read2003Excel(File file)  
  40.             throws IOException {  
  41.         List<List<Object>> list = new LinkedList<List<Object>>();  
  42.         HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));  
  43.         HSSFSheet sheet = hwb.getSheetAt(0);  
  44.         Object value = null;  
  45.         HSSFRow row = null;  
  46.         HSSFCell cell = null;  
  47.         int counter = 0;  
  48.         for (int i = sheet.getFirstRowNum(); counter < sheet  
  49.                 .getPhysicalNumberOfRows(); i++) {  
  50.             row = sheet.getRow(i);  
  51.             if (row == null) {  
  52.                 continue;  
  53.             } else {  
  54.                 counter++;  
  55.             }  
  56.             List<Object> linked = new LinkedList<Object>();  
  57.             for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  58.                 cell = row.getCell(j);  
  59.                 if (cell == null) {  
  60.                     continue;  
  61.                 }  
  62.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符  
  63.                 SimpleDateFormat sdf = new SimpleDateFormat(  
  64.                         "yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
  65.                 DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字  
  66.                 switch (cell.getCellType()) {  
  67.                 case XSSFCell.CELL_TYPE_STRING:  
  68.                     System.out.println(i + "行" + j + " 列 is String type");  
  69.                     value = cell.getStringCellValue();  
  70.                     break;  
  71.                 case XSSFCell.CELL_TYPE_NUMERIC:  
  72.                     System.out.println(i + "行" + j  
  73.                             + " 列 is Number type ; DateFormt:"  
  74.                             + cell.getCellStyle().getDataFormatString());  
  75.                     if ("@".equals(cell.getCellStyle().getDataFormatString())) {  
  76.                         value = df.format(cell.getNumericCellValue());  
  77.                     } else if ("General".equals(cell.getCellStyle()  
  78.                             .getDataFormatString())) {  
  79.                         value = nf.format(cell.getNumericCellValue());  
  80.                     } else {  
  81.                         value = sdf.format(HSSFDateUtil.getJavaDate(cell  
  82.                                 .getNumericCellValue()));  
  83.                     }  
  84.                     break;  
  85.                 case XSSFCell.CELL_TYPE_BOOLEAN:  
  86.                     System.out.println(i + "行" + j + " 列 is Boolean type");  
  87.                     value = cell.getBooleanCellValue();  
  88.                     break;  
  89.                 case XSSFCell.CELL_TYPE_BLANK:  
  90.                     System.out.println(i + "行" + j + " 列 is Blank type");  
  91.                     value = "";  
  92.                     break;  
  93.                 default:  
  94.                     System.out.println(i + "行" + j + " 列 is default type");  
  95.                     value = cell.toString();  
  96.                 }  
  97.                 if (value == null || "".equals(value)) {  
  98.                     continue;  
  99.                 }  
  100.                 linked.add(value);  
  101.             }  
  102.             list.add(linked);  
  103.         }  
  104.         return list;  
  105.     }  
  106.     /** 
  107.      * 读取Office 2007 excel 
  108.      * */  
  109.     private static List<List<Object>> read2007Excel(File file)  
  110.             throws IOException {  
  111.         List<List<Object>> list = new LinkedList<List<Object>>();  
  112.         // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  113.         XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
  114.         // 读取第一章表格内容  
  115.         XSSFSheet sheet = xwb.getSheetAt(0);  
  116.         Object value = null;  
  117.         XSSFRow row = null;  
  118.         XSSFCell cell = null;  
  119.         int counter = 0;  
  120.         for (int i = sheet.getFirstRowNum(); counter < sheet  
  121.                 .getPhysicalNumberOfRows(); i++) {  
  122.             row = sheet.getRow(i);  
  123.             if (row == null) {  
  124.                 continue;  
  125.             } else {  
  126.                 counter++;  
  127.             }  
  128.             List<Object> linked = new LinkedList<Object>();  
  129.             for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  130.                 cell = row.getCell(j);  
  131.                 if (cell == null) {  
  132.                     continue;  
  133.                 }  
  134.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符  
  135.                 SimpleDateFormat sdf = new SimpleDateFormat(  
  136.                         "yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
  137.                 DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字  
  138.                 switch (cell.getCellType()) {  
  139.                 case XSSFCell.CELL_TYPE_STRING:  
  140.                     System.out.println(i + "行" + j + " 列 is String type");  
  141.                     value = cell.getStringCellValue();  
  142.                     break;  
  143.                 case XSSFCell.CELL_TYPE_NUMERIC:  
  144.                     System.out.println(i + "行" + j  
  145.                             + " 列 is Number type ; DateFormt:"  
  146.                             + cell.getCellStyle().getDataFormatString());  
  147.                     if ("@".equals(cell.getCellStyle().getDataFormatString())) {  
  148.                         value = df.format(cell.getNumericCellValue());  
  149.                     } else if ("General".equals(cell.getCellStyle()  
  150.                             .getDataFormatString())) {  
  151.                         value = nf.format(cell.getNumericCellValue());  
  152.                     } else {  
  153.                         value = sdf.format(HSSFDateUtil.getJavaDate(cell  
  154.                                 .getNumericCellValue()));  
  155.                     }  
  156.                     break;  
  157.                 case XSSFCell.CELL_TYPE_BOOLEAN:  
  158.                     System.out.println(i + "行" + j + " 列 is Boolean type");  
  159.                     value = cell.getBooleanCellValue();  
  160.                     break;  
  161.                 case XSSFCell.CELL_TYPE_BLANK:  
  162.                     System.out.println(i + "行" + j + " 列 is Blank type");  
  163.                     value = "";  
  164.                     break;  
  165.                 default:  
  166.                     System.out.println(i + "行" + j + " 列 is default type");  
  167.                     value = cell.toString();  
  168.                 }  
  169.                 if (value == null || "".equals(value)) {  
  170.                     continue;  
  171.                 }  
  172.                 linked.add(value);  
  173.             }  
  174.             list.add(linked);  
  175.         }  
  176.         return list;  
  177.     }  
  178.     public static void main(String[] args) {  
  179.         try {  
  180.             readExcel(new File("D:\\test.xlsx"));  
  181.             // readExcel(new File("D:\\test.xls"));  
  182.         } catch (IOException e) {  
  183.             e.printStackTrace();  
  184.         }  
  185.     }  
  186. }  

相关文章&源代码下载地址:

Java POI导出EXCEL经典实现 Java导出Excel弹出下载框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值