POI读取excel的工具类

package com.tmg.commons.excel;

import org.apache.poi.hssf.usermodel.HSSFCell;
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil
{
     private int totalRows = 0;

     private int totalCells = 0;

     private String errorInfo;

     public ExcelUtil()
     {

     }


     public int getTotalRows()
     {
          return totalRows;
     }

     public int getTotalCells()
     {
          return totalCells;

     }

     public String getErrorInfo()
     {
          return errorInfo;
     }

     public boolean validateExcel(String filePath)
     {

          if (filePath == null
                  || !(isExcel2003(filePath) || isExcel2007(filePath)))
          {
               errorInfo = "";
               return false;
          }

          File file = new File(filePath);
          if (!file.exists())
          {
               errorInfo = "";
               return false;
          }

          return true;

     }

     public List<List<String>> read(String filePath) throws Exception
     {

          List<List<String>> dataLst = new ArrayList<List<String>>();

          InputStream is = null;

          try
          {

               if (!validateExcel(filePath))
               {
                    throw new Exception( "excel读取失败");
               }

               boolean isExcel2003 = true;
               if (isExcel2007(filePath))
               {
                    isExcel2003 = false;
               }
               File file = new File(filePath);
               is = new FileInputStream(file);
               dataLst = read(is, isExcel2003);
               is.close();

          }
          catch (Exception ex)
          {
               throw new Exception( "excel读取失败");
          }
          finally
          {
               if (is != null)
               {
                    try
                    {
                         is.close();
                    }
                    catch (IOException e)
                    {
                         is = null;
                    }
               }

          }
          return dataLst;
     }

     public List<List<String>> read(File file) throws Exception
     {
          if (file == null)
          {
               return null;
          }
          String filePath = file.getAbsolutePath();

          List<List<String>> dataLst = new ArrayList<List<String>>();

          InputStream is = null;

          try
          {
               if (!validateExcel(filePath))
               {
                    throw new Exception( "excel读取失败");
               }
               boolean isExcel2003 = true;
               if (isExcel2007(filePath))
               {
                    isExcel2003 = false;
               }
               is = new FileInputStream(file);
               dataLst = read(is, isExcel2003);
               is.close();

          }
          catch (Exception ex)
          {
               ex.printStackTrace();
               throw new Exception( "excel读取失败");
          }
          finally
          {
               if (is != null)
               {
                    try
                    {
                         is.close();
                    }
                    catch (IOException e)
                    {
                         is = null;
                    }
               }

          }
          return dataLst;
     }

     public List<List<String>> read(InputStream inputStream, boolean isExcel2003)
             throws Exception
     {
          List<List<String>> dataLst = null;
          try
          {
               Workbook wb = null;
               if (isExcel2003)
               {
                    wb = new HSSFWorkbook(inputStream);
               }
               else
               {
                    wb = new XSSFWorkbook(inputStream);
               }
               dataLst = read(wb);
          }
          catch (IOException e)
          {
               throw new Exception( "excel读取失败");
          }
          catch (Exception e)
          {
               throw new Exception( "excel读取失败");
          }

          return dataLst;

     }

     private List<List<String>> read(Workbook wb)
     {

          List<List<String>> dataLst = new ArrayList<List<String>>();

          Sheet sheet = wb.getSheetAt(0);

          this.totalRows = sheet.getPhysicalNumberOfRows();

          if (this.totalRows >= 1 && sheet.getRow(0) != null)
          {
               this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
          }
          for (int r = 0; r < this.totalRows; r++)
          {
               Row row = sheet.getRow(r);

               if (row == null)
               {
                    continue;
               }
               List<String> rowLst = new ArrayList<String>();

               for (int c = 0; c < this.getTotalCells(); c++)
               {
                    Cell cell = row.getCell(c);
                    String cellValue = "";
                    if (null != cell)
                    {

                         switch (cell.getCellType())
                         {
                              case HSSFCell.CELL_TYPE_NUMERIC:
                                   BigDecimal bd = new BigDecimal(
                                           cell.getNumericCellValue());
                                   cellValue = bd.toPlainString();
                                   break;
                              case HSSFCell.CELL_TYPE_STRING:
                                   cellValue = cell.getStringCellValue();
                                   break;
                              case HSSFCell.CELL_TYPE_BOOLEAN:
                                   cellValue = String.valueOf(cell.getBooleanCellValue());
                                   break;
                              case HSSFCell.CELL_TYPE_FORMULA:
                                   cellValue = cell.getCellFormula();
                                   break;
                              case HSSFCell.CELL_TYPE_BLANK:
                                   cellValue = "";
                                   break;
                              case HSSFCell.CELL_TYPE_ERROR:
                                   cellValue = "";
                                   break;
                              default:
                                   cellValue = "";
                                   break;
                         }
                    }
                    rowLst.add(cellValue);
               }
               dataLst.add(rowLst);
          }
          return dataLst;

     }

     public boolean isExcel2003(String filePath)
     {
          return filePath.matches("^.+\\.(?i)(xls)$");
     }

     public boolean isExcel2007(String filePath)
     {
          return filePath.matches("^.+\\.(?i)(xlsx)$");
     }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值