CuzDataTable.java
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.xiniunet.util;
import com.xiniunet.framework.util.excel.datatable.Cell;
import com.xiniunet.framework.util.excel.datatable.Column;
import com.xiniunet.framework.util.excel.datatable.DataRow;
import com.xiniunet.framework.util.excel.datatable.DataTable;
import com.xiniunet.framework.util.excel.exception.TemplateNotMatchException;
import com.xiniunet.framework.util.excel.util.ExcelUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Administrator on 2017/4/28.
*/
public class CuzDataTable extends DataTable {
/**
* 构造方法。传入Excel流,不需要指定类型。同时,也不会对数据进行检查。
* @param bytes Excel的字节数组。
* @param headRowIndex 表头开始行下标
* @throws IOException 可能会出现的错误。通常为以下错误:<br/>
* 1.流不能转换为Workbook
*/
public CuzDataTable(byte[] bytes, int headRowIndex) throws IOException,TemplateNotMatchException {
init();
InputStream inputStream = null; //文件输入流
Workbook workbook = null; //导入的文档
boolean flag; //用于判断文件的类型
try {
flag = true;
inputStream = new ByteArrayInputStream(bytes);
workbook = new HSSFWorkbook(inputStream);
} catch (Exception e) {
flag = false;
}
if( !flag) {
try {
flag = true;
inputStream = new ByteArrayInputStream(bytes);
workbook = new XSSFWorkbook(inputStream);
} catch (Exception e) {
flag = false;
}
}
if(inputStream != null) {
inputStream.close();
}
if( !flag ) {
throw new TemplateNotMatchException("不支持的文件类型");
}
/* 创建导入表的对象 */
Sheet sheet = workbook.getSheetAt(0); //默认只处理第一个Sheet表
this.setName(sheet.getSheetName());
/* 读取表头 */
Row headRow = sheet.getRow(headRowIndex);
int columnNum = headRow.getPhysicalNumberOfCells();
for(int i=0; i<columnNum; i++) {
org.apache.poi.ss.usermodel.Cell cell = headRow.getCell(i);
Column column = new Column();
column.setName(ExcelUtil.getValue(cell));
this.addColumn(column);
}
//表头开始之前的数据存放到 headInfo中
String headInfo = "";
for(int i = 0; i < headRowIndex; i++){
Row inputRow = sheet.getRow(i);
if(null != inputRow) {
headInfo += ExcelUtil.getValue(inputRow.getCell(0));
}
}
if (StringUtils.isNotBlank(headInfo)){
this.setHeadInfo(headInfo);
}
/* 逐行读取导入文件的数据,从1开始是因为第0行为表头 */
int rowNumber = sheet.getLastRowNum();
for(int i = headRowIndex + 1; i <= rowNumber; i ++) {
Row inputRow = sheet.getRow(i);
DataRow row = new DataRow();
if(null != inputRow) {
for (int j = 0; j < columnNum; j++) {
org.apache.poi.ss.usermodel.Cell excelCell = inputRow.getCell(j);
if (null != excelCell ) {
row.put(this.getColumns()[j].getName(), new Cell(ExcelUtil.getValue(excelCell)));
}
}
}
this.addRow(row);
}
}
}