Repository navigation
Expand file tree
/
Copy pathExcelHelper.java
More file actions
170 lines (158 loc) · 4.35 KB
/
Copy pathExcelHelper.java
File metadata and controls
170 lines (158 loc) · 4.35 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package zjr.amy.excel;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.poifs.filesystem.POIFSFileSystem;
/**
* Excel帮助类
*
* @author zhujinrong
*
*/
public class ExcelHelper {
/**
* 读取Excel内容
*
* @param fileName
* 文件名和路径
* @param ignoreRows
* 忽略读取的行数
* @return 数据
* @throws FileNotFoundException
* 找不到文件
* @throws IOException
* 输入输出异常
*/
public static String[][] getData(String fileName, int ignoreRows)
throws FileNotFoundException, IOException {
File file = new File(fileName);
List<String[]> result = new ArrayList<String[]>();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
// 可能有多个sheet
HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
for (int rowIndex = ignoreRows; rowIndex < sheet.getLastRowNum(); rowIndex++) {
HSSFRow row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
// 获取行数
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (int collumnIndex = 0; collumnIndex <= row.getLastCellNum(); collumnIndex++) {
String value = "";
cell = row.getCell(collumnIndex);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
double d = cell.getNumericCellValue();
value = d + "";
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
if (collumnIndex == 0 && value.trim().equals("")) {
break;
}
values[collumnIndex] = value.trim();
hasValue = true;
}
}
if (hasValue) {
result.add(values);
}
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}
/**
* 写Excel数据
*
* @param fileName
* 文件名
* @param data
* 数据
* @param sheetName
* sheet名字
* @throws IOException
* 输入输出异常
*/
public static void setData(String fileName, String[][] data,
String sheetName) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
wb.setSheetName(0, sheetName);
if (data != null && data.length > 0) {
for (int i = 0; i < data.length; i++) {
// 创建第i行
HSSFRow row = s.createRow(i);
for (int j = 0; j < data[i].length; j++) {
// 第i行第j列的数据
HSSFCell cell = row.createCell(j);
cell.setCellValue(data[i][j]);
}
}
}
// 写入工作薄
wb.write(fos);
// 关闭输出流
fos.close();
}
}