Chinaunix首页 | 论坛 | 博客
  • 博客访问: 314912
  • 博文数量: 280
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2905
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(280)

文章存档

2022年(78)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-28 13:25:21


点击(此处)折叠或打开


  1. package com.fh.util;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;

  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFRow;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  10. /**
  11.  * 说明:从EXCEL导入到数据库
  12.  * 作者:FH Admin
  13.  * from:fhadmin.cn
  14.  */
  15. public class ObjectExcelRead {

  16.     /**
  17.      * @param filepath //文件路径
  18.      * @param filename //文件名
  19.      * @param startrow //开始行号
  20.      * @param startcol //开始列号
  21.      * @param sheetnum //sheet
  22.      * @return list
  23.      */
  24.     public static List<Object> readExcel(String filepath, String filename, int startrow, int startcol, int sheetnum) {
  25.         List<Object> varList = new ArrayList<Object>();

  26.         try {
  27.             File target = new File(filepath, filename);
  28.             FileInputStream fi = new FileInputStream(target);
  29.             HSSFWorkbook wb = new HSSFWorkbook(fi);
  30.             HSSFSheet sheet = wb.getSheetAt(sheetnum);                     //sheet 从0开始
  31.             int rowNum = sheet.getLastRowNum() + 1;                     //取得最后一行的行号

  32.             for (int i = startrow; i < rowNum; i++) {                    //行循环开始
  33.                 
  34.                 PageData varpd = new PageData();
  35.                 HSSFRow row = sheet.getRow(i);                             //行
  36.                 int cellNum = row.getLastCellNum();                     //每行的最后一个单元格位置

  37.                 for (int j = startcol; j < cellNum; j++) {                //列循环开始
  38.                     
  39.                     HSSFCell cell = row.getCell(Short.parseShort(j + ""));
  40.                     String cellValue = null;
  41.                     if (null != cell) {
  42.                         switch (cell.getCellType()) {                     // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
  43.                         case 0:
  44.                             cellValue = String.valueOf((int) cell.getNumericCellValue());
  45.                             break;
  46.                         case 1:
  47.                             cellValue = cell.getStringCellValue();
  48.                             break;
  49.                         case 2:
  50.                             cellValue = cell.getNumericCellValue() + "";
  51.                             // cellValue = String.valueOf(cell.getDateCellValue());
  52.                             break;
  53.                         case 3:
  54.                             cellValue = "";
  55.                             break;
  56.                         case 4:
  57.                             cellValue = String.valueOf(cell.getBooleanCellValue());
  58.                             break;
  59.                         case 5:
  60.                             cellValue = String.valueOf(cell.getErrorCellValue());
  61.                             break;
  62.                         }
  63.                     } else {
  64.                         cellValue = "";
  65.                     }
  66.                     
  67.                     varpd.put("var"+j, cellValue);
  68.                     
  69.                 }
  70.                 varList.add(varpd);
  71.             }

  72.         } catch (Exception e) {
  73.             System.out.println(e);
  74.         }
  75.         
  76.         return varList;
  77.     }
  78. }


阅读(394) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~