|
@@ -9,7 +9,7 @@ import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
-import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.CellType;
|
|
|
import org.apache.poi.ss.usermodel.CellValue;
|
|
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
@@ -20,17 +20,19 @@ import java.lang.reflect.Method;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
+ * @version V1.0
|
|
|
* @ClassName XLSApp
|
|
|
* @Description 2003Excel导入
|
|
|
- * @version V1.0
|
|
|
*/
|
|
|
public class XLSApp implements ExcelApp {
|
|
|
- /** 公式求值器 */
|
|
|
+ /**
|
|
|
+ * 公式求值器
|
|
|
+ */
|
|
|
private FormulaEvaluator evaluator;
|
|
|
private String cofSheetName = "flagSheet";
|
|
|
|
|
|
@Override
|
|
|
- public List<Object> readAllRow (ExcelAppBean bean) throws Exception {
|
|
|
+ public List<Object> readAllRow(ExcelAppBean bean) throws Exception {
|
|
|
List<Object> result = new ArrayList<Object>();
|
|
|
HSSFWorkbook book = null;
|
|
|
try {
|
|
@@ -60,7 +62,7 @@ public class XLSApp implements ExcelApp {
|
|
|
int colNum;
|
|
|
// 读取行, 实际情况:有可能有多行表头,因此开始行号根据配置所得
|
|
|
for (int rowNum = bean.getStartRow() - 1; rowNum < sheet.getLastRowNum() - tailRow + 1; rowNum++) {
|
|
|
- obj = c.newInstance();
|
|
|
+ obj = c.newInstance();
|
|
|
row = sheet.getRow(rowNum);
|
|
|
if (row == null) {
|
|
|
return result;
|
|
@@ -101,8 +103,8 @@ public class XLSApp implements ExcelApp {
|
|
|
/**
|
|
|
* 判断指定的单元格是否是合并单元格
|
|
|
*
|
|
|
- * @param sheet 数据sheet
|
|
|
- * @param row 行下标
|
|
|
+ * @param sheet 数据sheet
|
|
|
+ * @param row 行下标
|
|
|
* @param column 列下标
|
|
|
* @return boolean
|
|
|
*/
|
|
@@ -161,7 +163,7 @@ public class XLSApp implements ExcelApp {
|
|
|
HSSFCell cell = null;
|
|
|
int idx;
|
|
|
while (it.hasNext()) {
|
|
|
- obj = c.newInstance();
|
|
|
+ obj = c.newInstance();
|
|
|
rowNum = it.next();
|
|
|
cols = rowCols.get(rowNum);
|
|
|
row = sheet.getRow(rowNum - 1);
|
|
@@ -172,7 +174,7 @@ public class XLSApp implements ExcelApp {
|
|
|
for (int i = 0; i < cols.size(); i++) {
|
|
|
idx = cols.get(i);
|
|
|
cell = row.getCell(colNums.get(idx) - 1);
|
|
|
- if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
|
|
|
+ if (cell == null || cell.getCellType() == CellType.BLANK) {
|
|
|
continue;
|
|
|
}
|
|
|
readCell(c, obj, cell, titles.get(idx));
|
|
@@ -188,6 +190,7 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public List<Object> readByColumnCof(ExcelAppBean bean) throws Exception {
|
|
|
List<Object> result = new ArrayList<Object>();
|
|
@@ -217,7 +220,7 @@ public class XLSApp implements ExcelApp {
|
|
|
boolean readOk = true;
|
|
|
// 每一行都从起始列开始
|
|
|
col0 = startColNum;
|
|
|
- obj = c.newInstance();
|
|
|
+ obj = c.newInstance();
|
|
|
row = sheet.getRow(i);
|
|
|
if (row == null) {
|
|
|
return result;
|
|
@@ -231,7 +234,7 @@ public class XLSApp implements ExcelApp {
|
|
|
return result;
|
|
|
}
|
|
|
// 如果第一个要读取的单元格为空,则读取下一行
|
|
|
- if (j == 0 && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
|
|
|
+ if (j == 0 && cell.getCellType() == CellType.BLANK) {
|
|
|
readOk = false;
|
|
|
break;
|
|
|
}
|
|
@@ -253,6 +256,7 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public void write(Object obj, ExcelAppBean bean) throws Exception {
|
|
|
OutputStream out = null;
|
|
@@ -307,6 +311,7 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public ByteArrayOutputStream download(Object obj, ExcelAppBean bean) throws Exception {
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
@@ -363,14 +368,15 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
return out;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * @Title: getBook
|
|
|
- * @Description: 获取Excel实例
|
|
|
* @param bean bean
|
|
|
* @return HSSFWorkbook
|
|
|
- * @exception Exception e
|
|
|
+ * @throws Exception e
|
|
|
+ * @Title: getBook
|
|
|
+ * @Description: 获取Excel实例
|
|
|
*/
|
|
|
- private HSSFWorkbook getBook (ExcelAppBean bean) throws Exception{
|
|
|
+ private HSSFWorkbook getBook(ExcelAppBean bean) throws Exception {
|
|
|
InputStream is = null;
|
|
|
HSSFWorkbook book = null;
|
|
|
try {
|
|
@@ -394,13 +400,14 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * @Title: getSheet
|
|
|
- * @Description: 获取Sheet, 如果没给定sheetName, 则返回第一个Sheet
|
|
|
* @param book book
|
|
|
* @param bean bean
|
|
|
* @return HSSFSheet
|
|
|
- * @exception Exception e
|
|
|
+ * @throws Exception e
|
|
|
+ * @Title: getSheet
|
|
|
+ * @Description: 获取Sheet, 如果没给定sheetName, 则返回第一个Sheet
|
|
|
*/
|
|
|
private HSSFSheet getSheet(HSSFWorkbook book, ExcelAppBean bean) throws Exception {
|
|
|
String sheetName = bean.getSourceSheetName();
|
|
@@ -424,31 +431,32 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
return sheet;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * @Title: getValue
|
|
|
- * @Description: 根据单元格类型获取值
|
|
|
* @param cell 单元格对象
|
|
|
* @return String
|
|
|
+ * @Title: getValue
|
|
|
+ * @Description: 根据单元格类型获取值
|
|
|
*/
|
|
|
private Object getCellValue(HSSFCell cell) {
|
|
|
Object value = null;
|
|
|
try {
|
|
|
CellValue cellValue = this.evaluator.evaluate(cell);
|
|
|
switch (cellValue.getCellType()) {
|
|
|
- case Cell.CELL_TYPE_BOOLEAN:
|
|
|
+ case BOOLEAN:
|
|
|
value = cellValue.getBooleanValue();
|
|
|
break;
|
|
|
- case Cell.CELL_TYPE_NUMERIC:
|
|
|
+ case NUMERIC:
|
|
|
value = cellValue.getNumberValue();
|
|
|
break;
|
|
|
- case Cell.CELL_TYPE_STRING:
|
|
|
+ case STRING:
|
|
|
value = cellValue.getStringValue();
|
|
|
break;
|
|
|
- case Cell.CELL_TYPE_BLANK:
|
|
|
+ case BLANK:
|
|
|
break;
|
|
|
- case Cell.CELL_TYPE_ERROR:
|
|
|
+ case ERROR:
|
|
|
break;
|
|
|
- case Cell.CELL_TYPE_FORMULA:
|
|
|
+ case FORMULA:
|
|
|
break;
|
|
|
default:
|
|
|
value = cellValue.getStringValue();
|
|
@@ -458,11 +466,12 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
return value;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
+ * @param cell 单元格对象
|
|
|
+ * @param objValue 要写入的值
|
|
|
* @Title: getValue
|
|
|
* @Description: 填写单元格
|
|
|
- * @param cell 单元格对象
|
|
|
- * @param objValue 要写入的值
|
|
|
*/
|
|
|
private void setCellValue(HSSFCell cell, Object objValue) {
|
|
|
Object orgValue = this.getCellValue(cell);
|
|
@@ -486,13 +495,14 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
+ * @param row 第一行
|
|
|
+ * @param columnName 列名
|
|
|
+ * @return Map<String, Integer>
|
|
|
+ * @throws Exception e
|
|
|
* @Title: getColumnNumber
|
|
|
* @Description: 根据列名获取列索引号
|
|
|
- * @param row 第一行
|
|
|
- * @param columnName 列名
|
|
|
- * @return Map<String,Integer>
|
|
|
- * @exception Exception e
|
|
|
*/
|
|
|
private Integer getColumnNumber(HSSFRow row, String columnName) throws Exception {
|
|
|
Integer colNum = null;
|
|
@@ -515,14 +525,15 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
return colNum;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
+ * @param c 类模板
|
|
|
+ * @param obj 实体对象
|
|
|
+ * @param cell 单元格
|
|
|
+ * @param key 字段
|
|
|
+ * @throws Exception e
|
|
|
* @Title: setValue
|
|
|
* @Description: 调用实体对象的get方法获取值,并为单元给设置值
|
|
|
- * @param c 类模板
|
|
|
- * @param obj 实体对象
|
|
|
- * @param cell 单元格
|
|
|
- * @param key 字段
|
|
|
- * @exception Exception e
|
|
|
*/
|
|
|
private void writeCell(Class<?> c, Object obj, HSSFCell cell, String key) throws Exception {
|
|
|
try {
|
|
@@ -545,14 +556,15 @@ public class XLSApp implements ExcelApp {
|
|
|
throw e;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
+ * @param c 类模板
|
|
|
+ * @param obj 实体对象
|
|
|
+ * @param cell 单元格
|
|
|
+ * @param key void
|
|
|
+ * @throws Exception e
|
|
|
* @Title: getValue
|
|
|
* @Description: 获取单元格值,并调用实体对象的Set方法
|
|
|
- * @param c 类模板
|
|
|
- * @param obj 实体对象
|
|
|
- * @param cell 单元格
|
|
|
- * @param key void
|
|
|
- * @exception Exception e
|
|
|
*/
|
|
|
private void readCell(Class<?> c, Object obj, HSSFCell cell, String key) throws Exception {
|
|
|
try {
|
|
@@ -589,14 +601,14 @@ public class XLSApp implements ExcelApp {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * @param c 类模板
|
|
|
+ * @param obj 实体对象
|
|
|
+ * @param cell 单元格
|
|
|
+ * @param key 实体对象方法
|
|
|
+ * @param sheet sheet
|
|
|
+ * @param row void
|
|
|
* @Title: readMergeCell
|
|
|
* @Description: 获取单元格值,并调用实体对象的Set方法
|
|
|
- * @param c 类模板
|
|
|
- * @param obj 实体对象
|
|
|
- * @param cell 单元格
|
|
|
- * @param key 实体对象方法
|
|
|
- * @param sheet sheet
|
|
|
- * @param row void
|
|
|
*/
|
|
|
private void readMergeCell(Class<?> c, Object obj, HSSFCell cell, String key, HSSFSheet sheet, HSSFRow row) throws Exception {
|
|
|
try {
|
|
@@ -625,8 +637,8 @@ public class XLSApp implements ExcelApp {
|
|
|
/**
|
|
|
* 获取合并单元格的值
|
|
|
*
|
|
|
- * @param sheet 数据sheet
|
|
|
- * @param row 数据当前行
|
|
|
+ * @param sheet 数据sheet
|
|
|
+ * @param row 数据当前行
|
|
|
* @param column 当前列
|
|
|
* @return String
|
|
|
*/
|
|
@@ -654,12 +666,13 @@ public class XLSApp implements ExcelApp {
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
+ * @param sheet sheet
|
|
|
+ * @param bean bean
|
|
|
+ * @throws Exception e
|
|
|
* @Title: validation
|
|
|
* @Description: 判断是否执行过宏
|
|
|
- * @param sheet sheet
|
|
|
- * @param bean bean
|
|
|
- * @exception Exception e
|
|
|
*/
|
|
|
private void validation(HSSFSheet sheet, ExcelAppBean bean) throws Exception {
|
|
|
boolean vaFlag = bean.isValidateFlag();
|