excel导出工具类

package com.md.cpmis.utils;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;


public class ReadCellFormula {

    /**
     * 判断Excel的版本,获取Workbook
     */
    public static Workbook getWorkbook(InputStream is, String fileType) throws IOException {
        Workbook wb = null;
        if (fileType.toUpperCase().endsWith("XLS")) {     //Excel 2003
            wb = new HSSFWorkbook(is);
        } else if (fileType.toUpperCase().endsWith("XLSX")) {    // Excel 2007/2010
            wb = new XSSFWorkbook(is);
        }
        return wb;
    }
    /**
     * //判断从cell列的数据类型 并转换成String类型
    * @param Cell cell
     * @return
     * @author houshixin
     * @date 2020/6/30
     */
    public static String getCellValue(Cell cell) {
        String value = "";
        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    value = cell.getRichStringCellValue().getString();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {  //判断日期类型
                        value = sdf.format(HSSFDateUtil.getJavaDate(cell.
                                getNumericCellValue())).toString();
                    }
                    else {  //否
                        value = new DecimalFormat("#.######").format(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    value = String.valueOf(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_BLANK:
                    value = "";
                    break;
                default:
                    value = cell.toString();
                    break;
            }
        } catch (Exception ex) {
            return value;
        }
        return value;
    }
}

展开阅读全文
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值