导入excel

/**
 * 
 */
package com.ssh.file.util.export;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import com.ssh.framework.exception.ServiceException;

/**
 * @author spring
 * 
 */
public class ImportExcelUtil {

   private Logger logger = LoggerFactory.getLogger(ImportExcelUtil.class);

   public List<List<CellVo>> checkExcel(MultipartFile file, String columns[]) throws ServiceException {
      Workbook workbook = null;
      InputStream fis = null;
      String fileName = file.getOriginalFilename();
      try { // 解决Excel 03版本后兼容性问题
         fis = file.getInputStream();
         if (fileName.endsWith("xls")) {
            workbook = new HSSFWorkbook(fis);
         } else {
            workbook = new XSSFWorkbook(fis);
         }

         logger.debug(this.getClass().getSimpleName() + ".ImportExcel()");
         int sheetCount = workbook.getNumberOfSheets(); // 获得工作薄(Workbook)中工作表(Sheet)的个数
         List<List<CellVo>> list = new ArrayList<List<CellVo>>();
         for (int i = 0; i < sheetCount; i++) {
            Sheet sheet = workbook.getSheetAt(i); // 获取第i张sheet表
            int rownum = sheet.getLastRowNum(); // 获得最后一行的行数
            for (int j = 1; j <= rownum; j++) {
               Row row = sheet.getRow(j); // 获取第i行
               Iterator<Cell> it = row.iterator();
               List<CellVo> cells = new ArrayList<CellVo>();
               int l = 0;
               while (it.hasNext()) {
                  Cell cell = it.next();
                  CellVo cellData = new CellVo();
                  if (cell != null) {
                     cell.setCellType(Cell.CELL_TYPE_STRING);
                     cellData.setValue(cell.getStringCellValue().trim());
                  } else {
                     cellData.setValue("");
                  }
                  cellData.setItem(columns[l++]); // 设置字段名称
                  cells.add(cellData);
               }
               if (list != null)
                  list.add(cells);
            }
         }

         return list;
      } catch (Exception ex) {
         throw new ServiceException("打开Excel文件异常!", ex);
      } finally {
         if (fis != null) {
            try {
               fis.close();
            } catch (Exception e) {
               logger.warn("文件关闭异常!", e);
            }
         }
      }

   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值