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;
}
}
excel导出工具类
最新推荐文章于 2022-11-10 11:42:33 发布