excel工具类

excel工具类

基于poi实现的excel工具类,功能包括:

功能一:从excel读取数据,数据输入 readExcel(String filePath,String columns[])

功能二:将数据写入excel,数据输出 writeExcel(String title, String[] rowName, List<Object[]> dataList, OutputStream out)

package com.pzc.opendataplatform.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author: pzc
 * @Date: 2020-12
 * @Description: Excel工具类
 */
public class ExcelUtils {
   

    // 导出时excel的扩展名
    public static final String EXTENSION_NAME = ".xlsx";
    // 03版excel扩展名
    private static final String XLS = ".xls";
    // 07版excel扩展名
    private static final String XLSX = ".xlsx";

    private static Logger logger = LoggerFactory.getLogger(ExcelUtils.class);

    public static List<Map<String, String>> readExcel(String filePath,String columns[]) throws FileNotFoundException {
   
        Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List<Map<String,String>> list = null;
        String cellData = null;
        wb = readExcel(filePath);
        if(wb != null){
   
            //用来存放表中数据
            list = new ArrayList<Map<String,String>>();
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            int colnum = row.getPhysicalNumberOfCells();
            for (int i = 1; i<rownum; i++) {
   
                Map<String,String> map = new LinkedHashMap<String,String>();
                row = sheet.getRow(i);
                if(row !=null){
   
                    for (int j=0;j<colnum;j++){
   
                        cellData = (String) getCellFormatValue(row.getCell(j));
                        map.put(columns[j], cellData);
                    }
                }else{
   
                    break;
                }
                list.add(map);
            }
        }
        return list;
    }
    //读取excel
    public static Workbook readExcel(String filePath) throws FileNotFoundException {
   
        Workbook wb = null;
        if(filePath==null){
   
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
   
            is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
   
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
   
                return wb = null;
            }else{
   
                return wb = null;
            }

        } catch (FileNotFoundException e) {
   
            e.printStackTrace();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        return wb;
    }
    public static Object getCellFormatValue(Cell cell){
   
        Object cellValue = null;
        if(cell!=null){
   
            //判断cell类型
            switch(cell.getCellType()){
   
                case  NUMERIC:{
   
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                }
                case FORMULA:{
   
                    //判断cell是否为日期格式
                    if(DateUtil.isCellDateFormatted(cell)){
   
                        //转换为日期格式YYYY-mm-dd
                        cellValue = cell.getDateCellValue();
                    }else{
   
                        //数字
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    }
                    break;
                }
                case STRING:{
   
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                default:
                    cellValue = "";
            }
        }else{
   
            cellValue = "";
        }
        return cellValue;
    }


    /**
     * 导入Excel到数据库
     *
     * @param filePath 导入的excel文件所在的绝对路径
     * @param startRow 开始解析的行数
     * @param startCol 开始解析的列数
     * @param sheetNum 开始解析的sheet序号,如果不指定,默认传值为-1,则会解析所有sheet
     * @return  {Row2-Col1=编号, Row2-Col2=名称, Row2-Col3=地址}
     */
    public static List<List<Map<String, Object>>> readExcel(String filePath, int startRow, int startCol, int sheetNum) {
   
        logger.info
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今夜明珠色

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值