java使用poi读取excel数据

java使用poi读取excel数据(xlsx)

首先要下载poi的jar包:下载地址

之后在项目中需要把poi下载目录里的所有jar包导入。

然后我自己封装了一个xlsx读取类,输入文件地址,要获取的列。输出二维数组,第一维代表一行数据,第二维代表每一行的单元格数据。


 
 
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.ArrayList;
  6. import java.util.Properties;
  7. import org.apache.poi.hssf.usermodel.HSSFCell;
  8. import org.apache.poi.xssf.usermodel.XSSFCell;
  9. import org.apache.poi.xssf.usermodel.XSSFRow;
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. public class Excel_reader {
  13. //*************xlsx文件读取函数************************
  14. //excel_name为文件名,arg为需要查询的列号
  15. //返回二维字符串数组
  16. @SuppressWarnings({ "resource", "unused" })
  17. public ArrayList<ArrayList<String>> xlsx_reader(String excel_url, int ... args) throws IOException {
  18. //读取xlsx文件
  19. XSSFWorkbook xssfWorkbook = null;
  20. //寻找目录读取文件
  21. File excelFile = new File(excel_url );
  22. InputStream is = new FileInputStream(excelFile);
  23. xssfWorkbook = new XSSFWorkbook(is);
  24. if(xssfWorkbook== null){
  25. System.out.println( "未读取到内容,请检查路径!");
  26. return null;
  27. }
  28. ArrayList<ArrayList<String>> ans= new ArrayList<ArrayList<String>>();
  29. //遍历xlsx中的sheet
  30. for ( int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
  31. XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
  32. if (xssfSheet == null) {
  33. continue;
  34. }
  35. // 对于每个sheet,读取其中的每一行
  36. for ( int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
  37. XSSFRow xssfRow = xssfSheet.getRow(rowNum);
  38. if (xssfRow == null) continue;
  39. ArrayList<String> curarr= new ArrayList<String>();
  40. for( int columnNum = 0 ; columnNum<args.length ; columnNum++){
  41. XSSFCell cell = xssfRow.getCell(args[columnNum]);
  42. curarr.add( Trim_str( getValue(cell) ) );
  43. }
  44. ans.add(curarr);
  45. }
  46. }
  47. return ans;
  48. }
  49. //判断后缀为xlsx的excel文件的数据类
  50. @SuppressWarnings( "deprecation")
  51. private static String getValue(XSSFCell xssfRow) {
  52. if(xssfRow== null){
  53. return "---";
  54. }
  55. if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
  56. return String.valueOf(xssfRow.getBooleanCellValue());
  57. } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
  58. double cur=xssfRow.getNumericCellValue();
  59. long longVal = Math.round(cur);
  60. Object inputValue = null;
  61. if(Double.parseDouble(longVal + ".0") == cur)
  62. inputValue = longVal;
  63. else
  64. inputValue = cur;
  65. return String.valueOf(inputValue);
  66. } else if(xssfRow.getCellType() == xssfRow.CELL_TYPE_BLANK || xssfRow.getCellType() == xssfRow.CELL_TYPE_ERROR){
  67. return "---";
  68. }
  69. else {
  70. return String.valueOf(xssfRow.getStringCellValue());
  71. }
  72. }
  73. //判断后缀为xls的excel文件的数据类型
  74. @SuppressWarnings( "deprecation")
  75. private static String getValue(HSSFCell hssfCell) {
  76. if(hssfCell== null){
  77. return "---";
  78. }
  79. if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
  80. return String.valueOf(hssfCell.getBooleanCellValue());
  81. } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
  82. double cur=hssfCell.getNumericCellValue();
  83. long longVal = Math.round(cur);
  84. Object inputValue = null;
  85. if(Double.parseDouble(longVal + ".0") == cur)
  86. inputValue = longVal;
  87. else
  88. inputValue = cur;
  89. return String.valueOf(inputValue);
  90. } else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BLANK || hssfCell.getCellType() == hssfCell.CELL_TYPE_ERROR){
  91. return "---";
  92. }
  93. else {
  94. return String.valueOf(hssfCell.getStringCellValue());
  95. }
  96. }
  97. //字符串修剪 去除所有空白符号 , 问号 , 中文空格
  98. static private String Trim_str(String str){
  99. if(str== null)
  100. return null;
  101. return str.replaceAll( "[\\s\\?]", "").replace( " ", "");
  102. }
  103. }

下面是主方法中对该类的调用:


 
 
  1. import java.io.IOException;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. public class Main {
  12. public static void main(String[] args) throws IOException {
  13. Excel_reader test= new Excel_reader();
  14. ArrayList<ArrayList<String>> arr=test.xlsx_reader( "/....../filename.xlsx", 0, 1, 2, 3, 4, 5); //后面的参数代表需要输出哪些列,参数个数可以任意
  15. for( int i= 0;i<arr.size();i++){
  16. ArrayList<String> row=arr.get(i);
  17. for( int j= 0;j<row.size();j++){
  18. System.out.print(row.get(j)+ " ");
  19. }
  20. System.out.println( "");
  21. }
  22. }
  23. }




阅读更多
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值