【Apache POI】Java 读取Excel文件

àì夳堔傛蜴生んèń 2022-09-30 11:48 367阅读 0赞

Excel内容如下:

Center

封装Excel行列字段的Bean:

  1. public class Employee {
  2. private String name;
  3. private String gender;
  4. private int age;
  5. private String department;
  6. private double salary;
  7. /**
  8. * 注意:读取日期操作要将Excel单元格设为文本格式,然后按字符串读取;写入操作时,直接按字符串写入
  9. */
  10. private String date;
  11. public Employee() {
  12. super();
  13. }
  14. public Employee(String name, String gender, int age, String department,
  15. double salary, String date) {
  16. super();
  17. this.name = name;
  18. this.gender = gender;
  19. this.age = age;
  20. this.department = department;
  21. this.salary = salary;
  22. this.date = date;
  23. }
  24. //省略getter/setter方法
  25. @Override
  26. public String toString() {
  27. return "Employee [name=" + name + ", gender=" + gender + ", age=" + age
  28. + ", department=" + department + ", salary=" + salary
  29. + ", date=" + date + "]";
  30. }
  31. }

读取Excel文件的方法:

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import org.apache.poi.hssf.usermodel.HSSFCell;
  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.openxml4j.exceptions.InvalidFormatException;
  14. import org.apache.poi.ss.usermodel.Cell;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.ss.usermodel.Sheet;
  17. import org.apache.poi.ss.usermodel.Workbook;
  18. import org.apache.poi.ss.usermodel.WorkbookFactory;
  19. import com.lmb.excel.bean.Employee;
  20. /**
  21. * 读取Excel文件的方法
  22. * @author lmb
  23. * @date 2017-3-15
  24. *
  25. */
  26. public class ReadExcel {
  27. private static String xls2003 = "D:\\employee.xls";
  28. private static String xlsx2007 = "D:\\employee.xlsx";
  29. /**
  30. * 读取Excel2003的主表数据 (单个sheet)
  31. * @param filePath
  32. * @return
  33. */
  34. private static List<Employee> readFromXLS2003(String filePath) {
  35. File excelFile = null;// Excel文件对象
  36. InputStream is = null;// 输入流对象
  37. String cellStr = null;// 单元格,最终按字符串处理
  38. List<Employee> employeeList = new ArrayList<Employee>();// 返回封装数据的List
  39. Employee employee = null;// 每一个雇员信息对象
  40. try {
  41. excelFile = new File(filePath);
  42. is = new FileInputStream(excelFile);// 获取文件输入流
  43. HSSFWorkbook workbook2003 = new HSSFWorkbook(is);// 创建Excel2003文件对象
  44. HSSFSheet sheet = workbook2003.getSheetAt(0);// 取出第一个工作表,索引是0
  45. // 开始循环遍历行,表头不处理,从1开始
  46. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  47. HSSFRow row = sheet.getRow(i);// 获取行对象
  48. employee = new Employee();// 实例化Student对象
  49. if (row == null) {// 如果为空,不处理
  50. continue;
  51. }
  52. // 循环遍历单元格
  53. for (int j = 0; j < row.getLastCellNum(); j++) {
  54. HSSFCell cell = row.getCell(j);// 获取单元格对象
  55. if (cell == null) {// 单元格为空设置cellStr为空串
  56. cellStr = "";
  57. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
  58. cellStr = String.valueOf(cell.getBooleanCellValue());
  59. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
  60. cellStr = cell.getNumericCellValue() + "";
  61. } else {// 其余按照字符串处理
  62. cellStr = cell.getStringCellValue();
  63. }
  64. // 下面按照数据出现位置封装到bean中
  65. if (j == 0) {
  66. employee.setName(cellStr);
  67. } else if (j == 1) {
  68. employee.setGender(cellStr);
  69. } else if (j == 2) {
  70. employee.setAge(new Double(cellStr).intValue());
  71. } else if (j == 3) {
  72. employee.setDepartment(cellStr);
  73. } else if(j == 4){
  74. employee.setSalary(new Double(cellStr).intValue());
  75. }else {
  76. employee.setDate(cellStr);
  77. }
  78. }
  79. employeeList.add(employee);// 数据装入List
  80. }
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. } finally {// 关闭文件流
  84. if (is != null) {
  85. try {
  86. is.close();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. return employeeList;
  93. }
  94. /**
  95. * 读取Excel2003的表头
  96. * @param filePath 需要读取的文件路径
  97. * @return
  98. */
  99. public static String[] readHeaderFromXLS2003(String filePath){
  100. String[] excelTitle = null;
  101. FileInputStream is = null;
  102. try{
  103. File excelFile = new File(filePath);
  104. is = new FileInputStream(excelFile);
  105. HSSFWorkbook workbook2003 = new HSSFWorkbook(is);
  106. //循环读取工作表
  107. for (int i = 0; i < workbook2003.getNumberOfSheets(); i++) {
  108. HSSFSheet hssfSheet = workbook2003.getSheetAt(i);
  109. //*************获取表头是start*************
  110. HSSFRow sheetRow = hssfSheet.getRow(i);
  111. excelTitle = new String[sheetRow.getLastCellNum()];
  112. for (int k = 0; k < sheetRow.getLastCellNum(); k++) {
  113. HSSFCell hssfCell = sheetRow.getCell(k);
  114. excelTitle[k] = hssfCell.getStringCellValue();
  115. // System.out.println(excelTitle[k] + " ");
  116. }
  117. //*************获取表头end*************
  118. }
  119. }catch (IOException e) {
  120. e.printStackTrace();
  121. } finally {// 关闭文件流
  122. if (is != null) {
  123. try {
  124. is.close();
  125. } catch (IOException e) {
  126. e.printStackTrace();
  127. }
  128. }
  129. }
  130. return excelTitle;
  131. }
  132. /**
  133. * 读取Excel2007的示例方法 (单个sheet)
  134. * @param filePath
  135. * @return
  136. */
  137. public static List<Employee> readFromXLSX2007(String filePath) {
  138. File excelFile = null;// Excel文件对象
  139. InputStream is = null;// 输入流对象
  140. String cellStr = null;// 单元格,最终按字符串处理
  141. List<Employee> studentList = new ArrayList<Employee>();// 返回封装数据的List
  142. Employee employee = null;// 每一个雇员信息对象
  143. try {
  144. excelFile = new File(filePath);
  145. is = new FileInputStream(excelFile);// 获取文件输入流
  146. // XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2007文件对象
  147. org.apache.poi.ss.usermodel.Workbook workbook2007 = WorkbookFactory.create(is);
  148. // XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0
  149. org.apache.poi.ss.usermodel.Sheet sheet = workbook2007.getSheetAt(0);
  150. // 开始循环遍历行,表头不处理,从1开始
  151. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  152. employee = new Employee();// 实例化Student对象
  153. // HSSFRow row = sheet.getRow(i);// 获取行对象
  154. Row row = sheet.getRow(i);// 获取行对象
  155. if (row == null) {// 如果为空,不处理
  156. continue;
  157. }
  158. // 循环遍历单元格
  159. for (int j = 0; j < row.getLastCellNum(); j++) {
  160. // XSSFCell cell = row.getCell(j);// 获取单元格对象
  161. Cell cell = row.getCell(j);// 获取单元格对象
  162. if (cell == null) {// 单元格为空设置cellStr为空串
  163. cellStr = "";
  164. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
  165. cellStr = String.valueOf(cell.getBooleanCellValue());
  166. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
  167. cellStr = cell.getNumericCellValue() + "";
  168. } else {// 其余按照字符串处理
  169. cellStr = cell.getStringCellValue();
  170. }
  171. // 下面按照数据出现位置封装到bean中
  172. if (j == 0) {
  173. employee.setName(cellStr);
  174. } else if (j == 1) {
  175. employee.setGender(cellStr);
  176. } else if (j == 2) {
  177. employee.setAge(new Double(cellStr).intValue());
  178. } else if (j == 3) {
  179. employee.setDepartment(cellStr);
  180. } else if(j == 4){
  181. employee.setSalary(new Double(cellStr).intValue());
  182. } else {
  183. employee.setDate(cellStr);
  184. }
  185. }
  186. studentList.add(employee);// 数据装入List
  187. }
  188. } catch (IOException e) {
  189. e.printStackTrace();
  190. } catch (InvalidFormatException e) {
  191. // TODO Auto-generated catch block
  192. }finally {// 关闭文件流
  193. if (is != null) {
  194. try {
  195. is.close();
  196. } catch (IOException e) {
  197. e.printStackTrace();
  198. }
  199. }
  200. }
  201. return studentList;
  202. }
  203. /**
  204. * 读取Excel的示例方法 (多个sheet)
  205. * @param filePath
  206. * @return
  207. */
  208. public List<Employee> readMoreSheetFromXLS(String filePath){
  209. List<Employee> employeeList = new ArrayList<Employee>();
  210. String cellStr = null;//单元格,最终按字符串处理
  211. //创建来自excel文件的输入流
  212. try {
  213. FileInputStream is = new FileInputStream(filePath);
  214. //创建WorkBook实例
  215. Workbook workbook = null;
  216. if (filePath.toLowerCase().endsWith("xls")) {//2003
  217. workbook = new HSSFWorkbook(is);
  218. }else if(filePath.toLowerCase().endsWith("xlsx")){//2007
  219. workbook = WorkbookFactory.create(is);
  220. }
  221. //获取excel文件的sheet数量
  222. int numOfSheets = workbook.getNumberOfSheets();
  223. //挨个遍历sheet
  224. for (int i = 0; i < numOfSheets; i++) {
  225. Sheet sheet = workbook.getSheetAt(i);
  226. //挨个遍历sheet的每一行
  227. for (Iterator<Row> iterRow = sheet.iterator();iterRow.hasNext();) {
  228. Row row = iterRow.next();
  229. Employee employee = new Employee();
  230. int j = 0;//标识位,用于标识第几列
  231. //挨个遍历每一行的每一列
  232. for (Iterator<Cell> cellIter = row.cellIterator();cellIter.hasNext();) {
  233. Cell cell = cellIter.next();//获取单元格对象
  234. if (j == 0) {
  235. if (cell == null) {// 单元格为空设置cellStr为空串
  236. cellStr = "";
  237. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
  238. cellStr = String.valueOf(cell.getBooleanCellValue());
  239. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
  240. cellStr = cell.getNumericCellValue() + "";
  241. } else {// 其余按照字符串处理
  242. cellStr = cell.getStringCellValue();
  243. }
  244. employee.setName(cellStr);
  245. j ++;
  246. }
  247. // employee.setGender(cellStr); j == 1
  248. // employee.setAge(new Double(cellStr).intValue()); j == 2
  249. // employee.setDepartment(cellStr); j == 3
  250. // employee.setSalary(new Double(cellStr).intValue()); j == 4
  251. // employee.setDate(cellStr); j == 5
  252. employeeList.add(employee);
  253. }
  254. }
  255. }
  256. } catch (FileNotFoundException e) {
  257. e.printStackTrace();
  258. } catch (IOException e) {
  259. e.printStackTrace();
  260. } catch (InvalidFormatException e) {
  261. e.printStackTrace();
  262. }
  263. return employeeList;
  264. }
  265. /**
  266. * 主函数
  267. *
  268. * @param args
  269. */
  270. public static void main(String[] args) {
  271. System.out.println("************读取Excel2003的主表数据(将Excel单元格设为文本格式)******************");
  272. long start2003 = System.currentTimeMillis();
  273. List<Employee> list2003 = readFromXLS2003(xls2003);
  274. for (Employee employee : list2003) {
  275. System.out.println(employee);
  276. }
  277. long end2003 = System.currentTimeMillis();
  278. System.out.println((end2003 - start2003) + " ms done!");
  279. System.out.println("***********读取Excel2007的主表数据(未将Excel单元格设为文本格式) *********************");
  280. long start2007 = System.currentTimeMillis();
  281. List<Employee> list2007 = readFromXLSX2007(xlsx2007);
  282. for (Employee employee : list2007) {
  283. System.out.println(employee);
  284. }
  285. long end2007 = System.currentTimeMillis();
  286. System.out.println((end2007 - start2007) + " ms done!");
  287. System.out.println("************读取Excel2003的表头**********************");
  288. String[] excelTitle = readHeaderFromXLS2003(xls2003);
  289. StringBuffer title = new StringBuffer();
  290. for (int i = 0; i < excelTitle.length; i++) {
  291. title.append(excelTitle[i] + " ");
  292. }
  293. System.out.println(title.toString());
  294. }
  295. /* 运行结果:http://stackoverflow.com/questions/21992071/org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma
  296. ************读取Excel2003的主表数据(将Excel单元格设为文本格式)******************
  297. Employee [name=Tom, gender=男, age=29, department=信息技术部, salary=6800.0, date=20170316]
  298. Employee [name=Jack, gender=男, age=25, department=质量保障部, salary=6800.0, date=20170316]
  299. Employee [name=May, gender=女, age=26, department=公共关系部, salary=6800.0, date=20170316]
  300. Employee [name=Mary, gender=女, age=27, department=财务部, salary=6800.0, date=20170316]
  301. Employee [name=Sarah, gender=女, age=28, department=电销部, salary=6800.0, date=20170316]
  302. 268 ms done!
  303. ***********读取Excel2007的主表数据 (未将Excel单元格设为文本格式)*********************
  304. Employee [name=Tom, gender=男, age=29, department=信息技术部, salary=6800.0, date=42810.0]
  305. Employee [name=Jack, gender=男, age=25, department=质量保障部, salary=6800.0, date=42810.0]
  306. Employee [name=May, gender=女, age=26, department=公共关系部, salary=6800.0, date=42810.0]
  307. Employee [name=Mary, gender=女, age=27, department=财务部, salary=6800.0, date=42810.0]
  308. Employee [name=Sarah, gender=女, age=28, department=电销部, salary=6800.0, date=42810.0]
  309. 924 ms done!
  310. ************读取Excel2003的表头**********************
  311. 姓名 性别 年龄 部门 薪资 日期
  312. */
  313. }

以上方法分为对Excel正文的读取和对Excel表头的读取,注释写的很详细,我就不在多说什么,大家参看具体实现。

需要特别说明一点:

读取日期操作要将Excel单元格设为文本格式,然后程序中按字符串读取;写入操作时,程序内也直接按字符串写入。否则读取到的日期内容是有问题的,参看上面的日志打印结果。
将Excel单元格设为文本格式的方法如下:

Center 1

Center 2

Center 3

发表评论

表情:
评论列表 (有 0 条评论,367人围观)

还没有评论,来说两句吧...

相关阅读