package com.icexls;
/**
* 导出Excel的工具类
*
* @author iceWater
* @date 2017-04-08
* @version 1.0
*/
public class IceExcel {
private String excelFileName;
private String sheet;
private ParserType parserType = ParserType.AUTO;
private ExcelParser excelParser;
private NumberType numberType = NumberType.STRING;
/**
* 创建一个Excel操作对象
*
* @param excelFileName
* 操作的Excel对应的文件路径
* @since 1.0
*/
public IceExcel(String excelFileName) {
this.excelFileName = excelFileName;
}
/**
* 创建一个Excel操作对象
*
* @param excelFileName
* 操作的Excel对应的文件路径
* @param sheetName
* Excel对应的Sheet名称
* @since 1.0
*/
public IceExcel(String excelFileName, String sheetName) {
this.excelFileName = excelFileName;
this.sheet = sheetName;
}
void setParserType(ParserType parserType) {
if (!this.parserType.equals(parserType)) {
this.parserType = parserType;
excelParser = null;
init(this.sheet);
}
}
/**
* 读取Excel为String数组
*
* @return 从Excel中读入的数据
* @since 1.0
*/
public String[][] getData() {
init(null);
return excelParser.getData();
}
/**
* 将String二维数组导出Excel
*
* @param data
* 需要导出Excel的数据
* @since 1.0
*/
public void setData(String[][] data) {
init("第一页");
excelParser.setData(data);
}
private void init(String sheetName) {
if (excelParser == null) {
if (ParserType.AUTO.equals(parserType)) {
if (ExcelClassUtil.hasClass("jxl.Cell")) {
excelParser = new JxlExcelParser();
} else {
try {
excelParser = new PoiExcelParser();
} catch (NoClassDefFoundError e) {
if ("org/apache/poi/ss/usermodel/Cell".equals(e.getMessage().trim())) {
throw new RuntimeException(
"没有引入poi-x.x.x.jar,你可以从下面的地址下载:http://central.maven.org/maven2/org/apache/poi/poi/3.17/poi-3.17.jar");
} else {
e.printStackTrace();
}
}
}
} else if (ParserType.JXL.equals(parserType)) {
try {
excelParser = new JxlExcelParser();
} catch (NoClassDefFoundError e) {
if ("jxl/read/biff/BiffException".equals(e.getMessage().trim())) {
throw new RuntimeException(
"没有引入jxl-x.x.x.jar,你可以从下面的地址下载:http://central.maven.org/maven2/net/sourceforge/jexcelapi/jxl/2.6.12/jxl-2.6.12.jar");
} else {
e.printStackTrace();
}
}
} else if (ParserType.POI.equals(parserType)) {
try {
excelParser = new PoiExcelParser();
} catch (NoClassDefFoundError e) {
if ("org/apache/poi/ss/usermodel/Cell".equals(e.getMessage().trim())) {
throw new RuntimeException(
"没有引入poi-x.x.x.jar,你可以从下面的地址下载:http://central.maven.org/maven2/org/apache/poi/poi/3.17/poi-3.17.jar");
} else {
e.printStackTrace();
}
}
} else {
throw new RuntimeException("不存在的Excel实现:" + parserType);
}
}
AbstractExcelParser abstractExcelParser = (AbstractExcelParser) excelParser;
if (sheet == null) {
sheet = sheetName;
}
abstractExcelParser.setExcelFileName(excelFileName);
abstractExcelParser.setSheet(sheet);
abstractExcelParser.setNumberType(numberType);
}
void setSheetName(String sheet) {
init(sheet);
this.sheet = sheet;
}
void setNumberType(NumberType numberType) {
this.numberType = numberType;
}
}
最近下载更多
东风hank LV13
2024年9月22日
1358849392 LV21
2023年10月10日
rookie_58 LV2
2022年5月27日
jinandfei LV12
2021年12月8日
zhanghna799 LV3
2021年11月18日
sl0018 LV13
2021年8月27日
aaaahao LV13
2021年7月25日
liangxiang LV6
2020年10月15日
13188866605 LV12
2020年8月21日
wjh12345654321 LV14
2020年7月17日

最近浏览