1. package util;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.xssf.usermodel.XSSFRow;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16.  
  17. public class ReadExcel {
  18.  
  19. public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
  20. public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
  21. public static final String EMPTY = "";
  22. public static final String POINT = ".";
  23. public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
  24. public static final String PROCESSING = "Processing...";
  25.  
  26. public static List<ArrayList<String>> readExcel(String path,int sheelNum,int startRow,int cols){
  27. if (path == null || EMPTY.equals(path)) {
  28. return null;
  29. } else {
  30. String postfix = getPostfix(path);
  31. if (!EMPTY.equals(postfix)) {
  32. if (OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
  33. return readXls(path,sheelNum,startRow,cols);
  34. } else if (OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
  35. return readXlsx(path,sheelNum,startRow,cols);
  36. }
  37. } else {
  38. System.out.println(path + NOT_EXCEL_FILE);
  39. }
  40. }
  41. return null;
  42. }
  43. /**
  44. * Read the Excel 2003-2007
  45. * @param path the path of the Excel
  46. * @return
  47. * @throws IOException
  48. */
  49. public static List<ArrayList<String>> readXls(String path,int sheelNum,int startRow,int cols) {
  50. System.out.println(PROCESSING + path);
  51. List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
  52. try {
  53. InputStream is = new FileInputStream(path);
  54. HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
  55. HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(sheelNum);
  56. if (hssfSheet == null) {
  57. return list;
  58. }
  59. // Read the Row
  60. for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
  61. HSSFRow hssfRow = hssfSheet.getRow(rowNum);
  62. if (hssfRow != null) {
  63. ArrayList<String> rowList = new ArrayList<String>();
  64. for (int col = 0; col < cols; col++) {
  65. rowList.add(hssfRow.getCell(col).getStringCellValue());
  66. }
  67. list.add(rowList);
  68. }
  69. }
  70. } catch (FileNotFoundException e) {
  71. e.printStackTrace();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. return list;
  76. }
  77.  
  78. /**
  79. * Read the Excel 2010
  80. * @param path the path of the excel file
  81. * @return
  82. * @throws IOException
  83. */
  84. public static List<ArrayList<String>> readXlsx(String path,int sheelNum,int startRow,int cols) {
  85. System.out.println(PROCESSING + path);
  86. List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
  87. InputStream is;
  88. try {
  89. is = new FileInputStream(path);
  90. XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
  91. // Read the Sheet
  92. XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(sheelNum);
  93. if (xssfSheet == null) {
  94. return list;
  95. }
  96. // Read the Row
  97. for (int rowNum = startRow; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
  98. XSSFRow xssfRow = xssfSheet.getRow(rowNum);
  99. if (xssfRow != null) {
  100. ArrayList<String> rowList = new ArrayList<String>();
  101. for (int col = 0; col < cols; col++) {
  102. rowList.add(xssfRow.getCell(col).getStringCellValue());
  103. }
  104. list.add(rowList);
  105. }
  106. }
  107. } catch (FileNotFoundException e) {
  108. e.printStackTrace();
  109. }catch (IOException e) {
  110. e.printStackTrace();
  111. }
  112. return list;
  113. }
  114. /**
  115. * get postfix of the path
  116. * @param path
  117. * @return
  118. */
  119. private static String getPostfix(String path) {
  120. if (path == null || EMPTY.equals(path.trim())) {
  121. return EMPTY;
  122. }
  123. if (path.contains(POINT)) {
  124. return path.substring(path.lastIndexOf(POINT) + 1, path.length());
  125. }
  126. return EMPTY;
  127. }
  128. }

  

POI导入数据的更多相关文章

  1. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  2. Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

    ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...

  3. java使用POI实现Excel批量导入数据

    1.准备工作 1.1 创建模板表头与数据库表字段一一对应,示例如下 1.2将模板放入项目中,如下图所示: 2.前端页面 2.1 使用超链接提供模板下载地址 <html lang="zh ...

  4. java利用poi导出数据到excel

    背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...

  5. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  6. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  7. Excel文件按照指定模板导入数据(用jxl.jar包)

        本文中的方法只适合Excel2003,要读取Excel2007最好使用poi.jar,据说poi.jar还在更新,jxl.jar已经不更新了,处理Excel文件的读写问题最好还是学习poi.j ...

  8. 纳税服务系统【用户模块之使用POI导入excel、导出excel】

    前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...

  9. poi 导入导出的api说明(大全)

    原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...

随机推荐

  1. 分享一个option样式传递给select当前选中样式

    今天遇到一个很是纠结的问题,需求又改了!原生的select给option加样式,结果发现select选中仍是默认样式,如下图:

  2. java 图片 批量 压缩 +所有压缩

    /* oldsrc  : 原图片地址目录 如 'd:/'    newsrc  : 压缩后图片地址目录 如 'e:/'    widthdist,heightdist : 压缩后的宽和高       ...

  3. Bloom Filter 算法具体解释

    Bloom Filter 算法 Bloom filter是由Burton Bloom 在1970年提出的,其后在P2P上得到了广泛的应用.Bloom filter 算法可用来查询某一数据是否在某一数据 ...

  4. python - 面向对象(一)

    python是一门面向对象的编程语言,python中的一切均是对象. 有对象就提到类,对象和类就像是儿子和老子的关系,是不可分的一对. 什么是类     类就是具有一些共同特性的事物的统称.好比人类, ...

  5. asp.net服务器向客户端弹出对话框,但不使页面边白板

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: ...

  6. (转)怎样查看局域网中自己的IP地址和其他电脑的IP地址?

    开始菜单->运行->打cmd,回车->再弹出的黑框里打ipconfig -all,回车显示的IP Address就是你的ip地址看局域网的电脑的ip用软件比较方便,比如p2p终结者, ...

  7. Asp.net实现在线人数统计功能代码实例

    application最经典的一个方法:统计在线人数,这需要借助于我们的全局应用程序类来对登录的用户信息进行统计: 以下是代码片段:    void application_start(object ...

  8. 如何禁用不需要的HTTP方法

    IIS7.0默认开启了不安全的OPTIONS和TRACE方法,建议关闭这两个方法. 以下环境为windows server 2008.IIS7.0 方法(1):web.config 在<conf ...

  9. 查看kafka的group.id

    kafka/config目录下的consumer.properties中可以看到

  10. silverlight visifire控件图表制作——silverlight 后台方法页面事件

    1.返回事件 (1.返回silverlight页面,2.返回web页面) private void button_ClickBack(object sender, RoutedEventArgs e) ...