原文链接:http://blog.csdn.net/qq_37936542/article/details/79024847

最近项目需要实现一个将excell中的数据导入数据库,在网上找到这篇文章,原文地址:点击打开链接

一:导入maven依赖或相关jar包

poi-3.17.jar  下载地址:点击打开链接
poi-ooxml-3.17.jar  下载地址:点击打开链接
poi-ooxml-schemas-3.17.jar  下载地址:点击打开链接

二:编写ImportExcellUtils

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

三:测试

  1. package com.debo.utils;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.List;
  5. public class Test {
  6. public static void main(String[] args) throws IOException {
  7. File file = new File("D:/2.xlsx");
  8. List<List<Object>> readExcel = ImportExcellUtils.readExcel(file);
  9. for (int i = 0; i < readExcel.size(); i++) {
  10. System.out.println(readExcel.get(i));
  11. }
  12. }
  13. }

文末福利:

福利一:前端,Java,产品经理,微信小程序,Python等8G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880

福利二:微信小程序入门与实战全套详细视频教程

领取方式:
如果需要学习视频,欢迎关注 【编程微刊】微信公众号,回复【领取资源】一键领取以下所有干货资源,获取更多有用技术干货、文档资料。所有文档会持续更新,欢迎关注一起成长!

最新文章

  1. Linux内核 TCP/IP、Socket参数调优
  2. EF – 问题集锦
  3. html5.js
  4. Looksery Cup 2015 B. Looksery Party 暴力
  5. freshStartTail 第一次启动时 抛弃旧的日志
  6. [Java Concurrent] 并发访问共享资源的简单案例
  7. Android常用的物理按键及其触发事件
  8. Zookper
  9. PowerMock mock私有方法
  10. MySQL监听数据库存储过程出现异常
  11. 4.npm模块安装和使用(axios异步请求,lodash工具库)
  12. c++基础学习
  13. 海瑞菌的web前端学习直播间
  14. GitHub 新手教程 四,Git GUI 新手教程(1),OpenSSH Public Key
  15. java web程序 String的valueOf方法总集
  16. 【字符串】Simplify Path(栈)
  17. lemon批量蒯
  18. Android 利用cursor来进行排序(转至http://blog.csdn.net/yangzongquan/article/details/6547860)
  19. 04——wepy框架搭建
  20. equals()重写

热门文章

  1. CentOS 7 网络配置、远程访问
  2. Python基础教程之第1章 基础知识
  3. ORACLE10g R2【RAC+ASM→RAC+ASM】
  4. ArcGIS教程:地理处理服务演示样例(河流网络)(三)
  5. 删除dataGridview中选中的一行或多行
  6. html 代码
  7. MWPhotoBrowser 属性详解 和代理解释
  8. 大战C100K之-Linux内核调优篇--转载
  9. swift项目第三天:手写代码搭建主框架
  10. js进阶 12-7 如何知道你是从哪个元素移动到当前元素与事件调用时如何添加额外数据