poi批量导入excel

左手的ㄟ右手 2022-03-26 15:14 485阅读 0赞

1.先下载poi依赖的包,复制张贴代码一定没有错误

2.直接上代码,如果导入有错误,可能是别人给你发的excel模版有错误,可以自己创建一个excel测试

  1. public static void getDataFromExcel(String filePath) throws Exception
  2. \{
  3. Workbook wookbook = null;
  4. //判断是否为excel类型文件
  5. if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx"))
  6. \{
  7. System.out.println("文件不是excel类型");
  8. \}
  9. FileInputStream fis =null;
  10. try
  11. \{
  12. //获取一个绝对地址的流
  13. fis = new FileInputStream(filePath);
  14. \}
  15. catch(Exception e)
  16. \{
  17. e.printStackTrace();
  18. \}
  19. try
  20. \{
  21. //2003版本的excel,用.xls结尾
  22. wookbook = new HSSFWorkbook(fis);//得到工作簿
  23. \}
  24. catch (Exception ex)
  25. \{
  26. //ex.printStackTrace();
  27. try
  28. \{
  29. //2007版本的excel,用.xlsx结尾
  30. wookbook = new XSSFWorkbook(fis);//得到工作簿
  31. \} catch (IOException e)
  32. \{
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. \}
  36. \}
  37. //得到一个工作表
  38. Sheet sheet = wookbook.getSheetAt(0);
  39. //获得数据的总行数
  40. int totalRowNum = sheet.getLastRowNum();
  41. String code="";
  42. String name="";
  43. ImportExcelUtil importExcelUtil = new ImportExcelUtil();
  44. for (int i = 1; i <= totalRowNum; i++) \{
  45. // 获得第i行对象
  46. Row row = sheet.getRow(i);
  47. // 获得获得第i行第0列的 String类型对象
  48. Cell cell = row.getCell((short) 0);
  49. code=importExcelUtil.getCellValue(cell).toString();
  50. cell = row.getCell((short) 1);
  51. name=importExcelUtil.getCellValue(cell).toString();
  52. System.out.println(code+"---"+name);
  53. \}
  54. \}

3.测试方法

public static void main(String[] args) {
try {
getDataFromExcel(“E:\\test.xls”);
} catch (Exception e) {
e.printStackTrace();
}
}

4.ImportExcelUtil工具类

public class ImportExcelUtil {
private final static String excel2003L =”.xls”; //2003- 版本的excel
private final static String excel2007U =”.xlsx”; //2007+ 版本的excel

  1. /\*\*
  2. \* 描述:获取IO流中的数据,组装成List<List<Object>>对象
  3. \* @param in,fileName
  4. \* @throws IOException
  5. \*//\*
  6. public List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception\{
  7. List<List<Object>> list = null;
  8. POIFSFileSystem fs = new POIFSFileSystem(in);
  9. HSSFWorkbook work = new HSSFWorkbook(fs);
  10. HSSFSheet sheet= null;
  11. HSSFRow row=null;
  12. Cell cell = null;
  13. //户主 电话 身份证 有无房屋 房屋缴费类型 房屋面积(m²) 房屋单价:m²/元 有无车位 车位费 车位缴费类型 单元(栋)
  14. list = new ArrayList<List<Object>>();
  15. //遍历Excel中所有的sheet
  16. for (int i = 0; i < work.getNumberOfSheets(); i++) \{
  17. sheet = work.getSheetAt(i);
  18. if(sheet==null)\{continue;\}
  19. //遍历当前sheet中的所有行 从第6行开始遍历
  20. for (int j = 6; j <=sheet.getLastRowNum(); j++)\{
  21. row = sheet.getRow(j);
  22. //遍历所有的列 row.getFirstCellNum()
  23. List<Object> li = new ArrayList<Object>();
  24. for (int y =0 ; y < row.getLastCellNum(); y++) \{
  25. if(y>=15) break;
  26. cell = row.getCell(y);
  27. if(cell!=null)
  28. \{
  29. cell.setCellType(Cell.CELL\_TYPE\_STRING);//设置列值类型
  30. if(StringUtils.isEmpty(cell.getStringCellValue().trim().replaceAll(" ", "")))
  31. \{
  32. cell.setCellValue("0");
  33. \}else
  34. \{
  35. cell.setCellValue(cell.getStringCellValue());
  36. \}
  37. li.add(this.getCellValue(cell));
  38. \}
  39. cell=null;
  40. \}
  41. if(li.size()>=15)
  42. \{
  43. list.add(li);
  44. \}
  45. \}
  46. \}
  47. //work.close();
  48. return list;
  49. \} \*/
  50. /\*\*
  51. \* 描述:根据文件后缀,自适应上传文件的版本
  52. \* @param inStr,fileName
  53. \* @return
  54. \* @throws Exception
  55. \*/
  56. public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception\{
  57. Workbook wb = null;
  58. String fileType = fileName.substring(fileName.lastIndexOf("."));
  59. if(excel2003L.equals(fileType))\{
  60. wb = new HSSFWorkbook(inStr); //2003-
  61. \}else if(excel2007U.equals(fileType))\{
  62. wb = new XSSFWorkbook(inStr); //2007+
  63. \}else\{
  64. throw new Exception("解析的文件格式有误!");
  65. \}
  66. return wb;
  67. \}
  68. /\*\*
  69. \* 描述:对表格中数值进行格式化
  70. \* @param cell
  71. \* @return
  72. \*/
  73. public Object getCellValue(Cell cell)\{
  74. Object value = null;
  75. DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
  76. SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
  77. DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字
  78. switch (cell.getCellType()) \{
  79. case Cell.CELL\_TYPE\_STRING:
  80. value = cell.getRichStringCellValue().getString();
  81. break;
  82. case Cell.CELL\_TYPE\_NUMERIC:
  83. if("General".equals(cell.getCellStyle().getDataFormatString()))\{
  84. value = df.format(cell.getNumericCellValue());
  85. \}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString()))\{
  86. value = sdf.format(cell.getDateCellValue());
  87. \}else\{
  88. value = df2.format(cell.getNumericCellValue());
  89. \}
  90. break;
  91. case Cell.CELL\_TYPE\_BOOLEAN:
  92. value = cell.getBooleanCellValue();
  93. break;
  94. case Cell.CELL\_TYPE\_BLANK:
  95. value = "";
  96. break;
  97. default:
  98. break;
  99. \}
  100. return value;
  101. \}
  102. public static void main(String\[\] args) \{
  103. \}

}

5.excel模版,我这个只读取了第一列和第二列的数据

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hleHVfYmxvZw_size_16_color_FFFFFF_t_70

发表评论

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

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

相关阅读

    相关 poi批量导入excel

    1.先下载poi依赖的包,复制张贴代码一定没有错误 2.直接上代码,如果导入有错误,可能是别人给你发的excel模版有错误,可以自己创建一个excel测试