java Excel读取

package util;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelOperateUtil {

    /**
     * Excel后缀为.xlsx的版本调用此方法
     *
     * @param tempFile
     * @return
     */
    public static List<Map<String, Object>> readDataByExcelXLSX(File tempFile, boolean flag) {
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();// 号码集合
        try {
            // 创建对Excel工作簿文件的引用
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(tempFile));
            XSSFSheet sheet = workbook.getSheetAt(0);
            int rows = sheet.getPhysicalNumberOfRows();
            Map<String, Object> resultMap = null;
            String[] keys = new String[]{"column1","column2"};
            Pattern pattern = Pattern.compile("[0-9]*"); 
            for (int i = 1; i < rows; i++) {

                resultMap = new HashMap<String, Object>();

                Row row = sheet.getRow(i);
                if (row != null) {
                    int cells = row.getPhysicalNumberOfCells()+1;

                    for (int j = 0; j < cells; j++) {
                        Cell cell = row.getCell(j);
                        if (cell != null) {
                            if(flag){
                                if(HSSFCell.CELL_TYPE_STRING == cell.getCellType() && pattern.matcher(cell.getStringCellValue()).matches()){
                                    resultMap.put(keys[0], cell.getStringCellValue());
                                }else if(HSSFCell.CELL_TYPE_STRING == cell.getCellType()){
                                    resultMap.put(keys[1], cell.getStringCellValue());
                                }
                            }else{
                                if(HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType() && DateUtil.isCellDateFormatted(cell)){
                                    resultMap.put(keys[0], new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()));
                                }else if(HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()){
                                    resultMap.put(keys[1], cell.getNumericCellValue());
                                }
                            }
                        }
                    }

                    resultList.add(resultMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }

    /**
     * 读取Excel后缀为.xls之下的版本调用此方法
     *
     * @param tempFile
     * @return
     */
//  public static List<String> readDataByExcelXLS(File tempFile) {
//      List<String> phoneList = new ArrayList<String>();// 号码集合
//      try {
//          // 创建对Excel工作簿文件的引用
//          HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(tempFile));
//          HSSFSheet sheet = workbook.getSheetAt(0);
//          int rows = sheet.getPhysicalNumberOfRows();
//          for (int i = 0; i < rows; i++) {
//              HSSFRow row = sheet.getRow(i);
//              if (row != null) {
//                  int cells = row.getPhysicalNumberOfCells()+1;
//
//                  for (int j = 0; j < cells; j++) {
//                      String value = "";
//                      HSSFCell cell = row.getCell(j);
//                      if (cell != null) {
//                          switch (cell.getCellType()) {
//                          case HSSFCell.CELL_TYPE_NUMERIC:
//                              value = new DecimalFormat("0").format(cell.getNumericCellValue());
//                              phoneList.add(value);
//                              break;
//                          default:
//                              break;
//                          }
//                      }
//                  }
//              }
//          }
//      } catch (Exception e) {
//          e.printStackTrace();
//      }
//      return phoneList;
//  }

//  public static void main(String[] args) {
//      
        File file = new File("C:\\Users\\dzd-000\\Desktop\\111.xls"); 
        List<String> readDataByExcelXLS = readDataByExcelXLS(file);
//      
//      File file1 = new File("C:\\Users\\dzd-000\\Desktop\\000.xlsx"); 
//      List<Map<String, Object>> readDataByExcelXLSX = readDataByExcelXLSX(file1);
//      System.out.println(readDataByExcelXLSX.size());
//  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值