免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3599 | 回复: 1
打印 上一主题 下一主题

JAVA POI读取Office excel (2003,2007) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-25 15:56 |只看该作者 |倒序浏览
JAVA POI读取Office excel (2003,2007)



所需jar 包 :poi-3.6
  1. poi-3.6-20091214.jar
  2. poi-contrib-3.6-20091214.jar
  3. poi-scratch.6-20091214.jar
  4. geronimo-stax-api_1.0_spec-1.0.jar
  5. xmlbeans-2.3.0.jar
  6. 读取excel 文件的 java 代码:
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.text.DecimalFormat;
  12. import java.text.SimpleDateFormat;
  13. import java.util.LinkedList;
  14. import java.util.List;
  15. import org.apache.poi.hssf.usermodel.HSSFCell;
  16. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  17. import org.apache.poi.hssf.usermodel.HSSFRow;
  18. import org.apache.poi.hssf.usermodel.HSSFSheet;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  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. public class ReadExcel {

  25.      /**
  26.      * 对外提供读取excel 的方法
  27.      * */
  28. public static List<List<Object>> readExcel(File file) throws IOException{
  29.    String fileName = file.getName();
  30.    String extension = fileName.lastIndexOf(".")==-1?"":fileName.substring(fileName.lastIndexOf(".")+1);
  31.    if("xls".equals(extension)){
  32.     return read2003Excel(file);
  33.    }else if("xlsx".equals(extension)){
  34.     return read2007Excel(file);
  35.    }else{
  36.     throw new IOException("不支持的文件类型");
  37.    }
  38. }


  39. /**
  40. * 读取 office 2003 excel
  41. * @throws IOException
  42. * @throws FileNotFoundException */
  43. private static List<List<Object>> read2003Excel(File file) throws IOException{
  44.    List<List<Object>> list = new LinkedList<List<Object>>();
  45.    HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
  46.    HSSFSheet sheet = hwb.getSheetAt(0);
  47.    Object value = null;
  48.    HSSFRow row = null;
  49.    HSSFCell cell = null;
  50.   
  51.    for(int i = sheet.getFirstRowNum();i<= sheet.getPhysicalNumberOfRows();i++){
  52.     row = sheet.getRow(i);
  53.     if (row == null) {
  54.      continue;
  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("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
  64.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
  65.      switch (cell.getCellType()) {
  66.      case XSSFCell.CELL_TYPE_STRING:
  67.       System.out.println(i+"行"+j+" 列 is String type");
  68.       value = cell.getStringCellValue();
  69.       break;
  70.      case XSSFCell.CELL_TYPE_NUMERIC:
  71.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());
  72.       if("@".equals(cell.getCellStyle().getDataFormatString())){
  73.          value = df.format(cell.getNumericCellValue());
  74.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){
  75.          value = nf.format(cell.getNumericCellValue());
  76.       }else{
  77.         value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
  78.       }
  79.       break;
  80.      case XSSFCell.CELL_TYPE_BOOLEAN:
  81.       System.out.println(i+"行"+j+" 列 is Boolean type");
  82.       value = cell.getBooleanCellValue();
  83.       break;
  84.      case XSSFCell.CELL_TYPE_BLANK:
  85.       System.out.println(i+"行"+j+" 列 is Blank type");
  86.       value = "";
  87.       break;
  88.      default:
  89.       System.out.println(i+"行"+j+" 列 is default type");
  90.       value = cell.toString();
  91.      }
  92.      if (value == null || "".equals(value)) {
  93.       continue;
  94.      }
  95.      linked.add(value);
  96.    
  97.    }
  98.     list.add(linked);
  99.    }
  100.   
  101.    return list;
  102. }



  103. /**
  104. * 读取Office 2007 excel
  105. * */
  106. private static List<List<Object>> read2007Excel(File file) throws IOException {
  107.    List<List<Object>> list = new LinkedList<List<Object>>();
  108.    // 构造 XSSFWorkbook 对象,strPath 传入文件路径
  109.    XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
  110.    // 读取第一章表格内容
  111.    XSSFSheet sheet = xwb.getSheetAt(0);
  112.    Object value = null;
  113.    XSSFRow row = null;
  114.    XSSFCell cell = null;
  115.    for (int i = sheet.getFirstRowNum(); i <= sheet
  116.      .getPhysicalNumberOfRows(); i++) {
  117.     row = sheet.getRow(i);
  118.     if (row == null) {
  119.      continue;
  120.     }
  121.     List<Object> linked = new LinkedList<Object>();
  122.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
  123.      cell = row.getCell(j);
  124.      if (cell == null) {
  125.       continue;
  126.      }
  127.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符
  128.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
  129.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
  130.      switch (cell.getCellType()) {
  131.      case XSSFCell.CELL_TYPE_STRING:
  132.       System.out.println(i+"行"+j+" 列 is String type");
  133.       value = cell.getStringCellValue();
  134.       break;
  135.      case XSSFCell.CELL_TYPE_NUMERIC:
  136.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());
  137.       if("@".equals(cell.getCellStyle().getDataFormatString())){
  138.         value = df.format(cell.getNumericCellValue());
  139.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){
  140.         value = nf.format(cell.getNumericCellValue());
  141.       }else{
  142.        value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
  143.       }
  144.       break;
  145.      case XSSFCell.CELL_TYPE_BOOLEAN:
  146.       System.out.println(i+"行"+j+" 列 is Boolean type");
  147.       value = cell.getBooleanCellValue();
  148.       break;
  149.      case XSSFCell.CELL_TYPE_BLANK:
  150.       System.out.println(i+"行"+j+" 列 is Blank type");
  151.       value = "";
  152.       break;
  153.      default:
  154.       System.out.println(i+"行"+j+" 列 is default type");
  155.       value = cell.toString();
  156.      }
  157.      if (value == null || "".equals(value)) {
  158.       continue;
  159.      }
  160.      linked.add(value);
  161.     }
  162.     list.add(linked);
  163.    }
  164.    return list;
  165. }
  166. }
复制代码
说明:该类中共封装了三个方法,对外提供的读取excel文件的方法,两个私有的分别读取excel2003和excel2007的方法。外部使用,只需调用readExcel 方法,传入一个File 参数,程序根据文件扩展名来判断选取那个方法来读取Excel文件。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2011-08-26 12:09 |只看该作者
POI还不错。我用JXL读写的时候总有些意想不到的问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP