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

 

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

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

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 代码:
  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. * 读取 office 2003 excel
  34. * @throws IOException
  35. * @throws FileNotFoundException */ 
  36. private static List<List<Object>> read2003Excel(File file) throws IOException{ 
  37.    List<List<Object>> list = new LinkedList<List<Object>>(); 
  38.    HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file)); 
  39.    HSSFSheet sheet = hwb.getSheetAt(0); 
  40.    Object value = null
  41.    HSSFRow row = null
  42.    HSSFCell cell = null;  
  43.    for(int i = sheet.getFirstRowNum();i<= sheet.getPhysicalNumberOfRows();i++){ 
  44.     row = sheet.getRow(i); 
  45.     if (row == null) { 
  46.      continue
  47.     } 
  48.     List<Object> linked = new LinkedList<Object>(); 
  49.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { 
  50.      cell = row.getCell(j); 
  51.      if (cell == null) { 
  52.       continue
  53.      } 
  54.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符 
  55.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串 
  56.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字 
  57.      switch (cell.getCellType()) { 
  58.      case XSSFCell.CELL_TYPE_STRING: 
  59.       System.out.println(i+"行"+j+" 列 is String type"); 
  60.       value = cell.getStringCellValue(); 
  61.       break
  62.      case XSSFCell.CELL_TYPE_NUMERIC: 
  63.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString()); 
  64.       if("@".equals(cell.getCellStyle().getDataFormatString())){ 
  65.          value = df.format(cell.getNumericCellValue()); 
  66.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){ 
  67.          value = nf.format(cell.getNumericCellValue()); 
  68.       }else
  69.         value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); 
  70.       } 
  71.       break
  72.      case XSSFCell.CELL_TYPE_BOOLEAN: 
  73.       System.out.println(i+"行"+j+" 列 is Boolean type"); 
  74.       value = cell.getBooleanCellValue(); 
  75.       break
  76.      case XSSFCell.CELL_TYPE_BLANK: 
  77.       System.out.println(i+"行"+j+" 列 is Blank type"); 
  78.       value = ""
  79.       break
  80.      default
  81.       System.out.println(i+"行"+j+" 列 is default type"); 
  82.       value = cell.toString(); 
  83.      } 
  84.      if (value == null || "".equals(value)) { 
  85.       continue
  86.      } 
  87.      linked.add(value);   
  88.    } 
  89.     list.add(linked); 
  90.    } 
  91.    return list; 
  92. /**
  93. * 读取Office 2007 excel
  94. * */ 
  95. private static List<List<Object>> read2007Excel(File file) throws IOException { 
  96.    List<List<Object>> list = new LinkedList<List<Object>>(); 
  97.    // 构造 XSSFWorkbook 对象,strPath 传入文件路径 
  98.    XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file)); 
  99.    // 读取第一章表格内容 
  100.    XSSFSheet sheet = xwb.getSheetAt(0); 
  101.    Object value = null
  102.    XSSFRow row = null
  103.    XSSFCell cell = null
  104.    for (int i = sheet.getFirstRowNum(); i <= sheet 
  105.      .getPhysicalNumberOfRows(); i++) { 
  106.     row = sheet.getRow(i); 
  107.     if (row == null) { 
  108.      continue
  109.     } 
  110.     List<Object> linked = new LinkedList<Object>(); 
  111.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { 
  112.      cell = row.getCell(j); 
  113.      if (cell == null) { 
  114.       continue
  115.      } 
  116.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符 
  117.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串 
  118.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字 
  119.      switch (cell.getCellType()) { 
  120.      case XSSFCell.CELL_TYPE_STRING: 
  121.       System.out.println(i+"行"+j+" 列 is String type"); 
  122.       value = cell.getStringCellValue(); 
  123.       break
  124.      case XSSFCell.CELL_TYPE_NUMERIC: 
  125.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString()); 
  126.       if("@".equals(cell.getCellStyle().getDataFormatString())){ 
  127.         value = df.format(cell.getNumericCellValue()); 
  128.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){ 
  129.         value = nf.format(cell.getNumericCellValue()); 
  130.       }else
  131.        value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); 
  132.       } 
  133.       break
  134.      case XSSFCell.CELL_TYPE_BOOLEAN: 
  135.       System.out.println(i+"行"+j+" 列 is Boolean type"); 
  136.       value = cell.getBooleanCellValue(); 
  137.       break
  138.      case XSSFCell.CELL_TYPE_BLANK: 
  139.       System.out.println(i+"行"+j+" 列 is Blank type"); 
  140.       value = ""
  141.       break
  142.      default
  143.       System.out.println(i+"行"+j+" 列 is default type"); 
  144.       value = cell.toString(); 
  145.      } 
  146.      if (value == null || "".equals(value)) { 
  147.       continue
  148.      } 
  149.      linked.add(value); 
  150.     } 
  151.     list.add(linked); 
  152.    } 
  153.    return list; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值