java poi 导入excel

来源:互联网 发布:皮皮跑胡子 软件您 编辑:程序博客网 时间:2024/05/17 02:37
最近项目需要导入excel,网上有很多例子,自己整合记录下,兼容2003和2007,暂时没有添加图片处理功能。

所需jar包    poi

?
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package example.poi;
import java.io.*;
import java.text.DecimalFormat;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class ImportExcel {
  
 private Workbook wb = null;
 private Sheet sheet = null;
 private Row row = null;
 private int sheetNum = 0;
 private int rowNum = 0;
 private FileInputStream fis = null;
 private File file = null;
 private DecimalFormat df = new DecimalFormat("0");
 public ImportExcel() {
  super();
 }
 public void setSheetNum(int sheetNum) {
  this.sheetNum = sheetNum;
 }
 public void setRowNum(int rowNum) {
  this.rowNum = rowNum;
 }
 public void setFile(File file) {
  this.file = file;
 }
 /**
  * 读取excel文件获得HSSFWorkbook对象
  * @throws IOException
  */
 public void open(String filePath) throws IOException {
  if(validateExcel(filePath)) {
   file = new File(filePath);
   fis = new FileInputStream(file);
   if(isExcel2003(filePath)) {
    wb = new HSSFWorkbook(fis);
   else {
    wb = new XSSFWorkbook(fis);
   }
   fis.close();
  }
 }
  
 /**
  * 获取sheet表数目
  * @return sheet表数目
  */
 public int getSheetCount() {
  int sheetCount = -1;
  sheetCount = wb.getNumberOfSheets();
  return sheetCount;
 }
  
 /**
  * 获取sheetNum下的记录行数
  * @return 记录行数
  */
 public int getRowCount() {
  if(wb == null) {
   System.err.println("----------->WorkBook为空");
  }
  Sheet sheet = wb.getSheetAt(this.sheetNum);
  int rowCount = -1;
  rowCount = sheet.getLastRowNum();
  return rowCount;
 }
  
 /**
  * 获取指定sheetNum的记录行数
  * @param sheetNum 表编号
  * @return 记录行数
  */
 public int getRowCount(int sheetNum) {
  Sheet sheet = wb.getSheetAt(sheetNum);
  int rowCount = -1;
  rowCount = sheet.getLastRowNum();
  return rowCount;
 }
  
 /**
  * 得到指定行的内容
  * @param lineNum 行数
  * @return 内容
  */
 public String[] readExcelLine(int lineNum) {
  return readExcelLine(this.sheetNum, lineNum);
 }
  
 /**
  * 获取指定工作表和行数的内容
  * @param sheetNum 表编号
  * @param lineNum 行数
  * @return 内容
  */
 public String[] readExcelLine(int sheetNum, int lineNum) {
  if(sheetNum < 0 || lineNum < 0) {
   return null;
  }
  String[] strExcelLine = null;
  try {
   sheet = wb.getSheetAt(sheetNum);
   row = sheet.getRow(lineNum);
    
   int cellCount = row.getLastCellNum();
   strExcelLine = new String[cellCount + 1];
   for(int i = 0; i <= cellCount; i++) {
    strExcelLine[i] = readStringExcelCell(lineNum, i);
   }
  catch (Exception e) {
   e.printStackTrace();
  }
  return strExcelLine;
 }
  
 /**
  * 获取指定列的内容
  * @param cellNum 列编号
  * @return 内容
  */
 public String readStringExcelCell(int cellNum) {
  return readStringExcelCell(this.rowNum, cellNum);
 }
  
 /**
  * 获取指定行和列编号的内容
  * @param rowNum 行编号
  * @param cellNum 列编号
  * @return 内容
  */
 public String readStringExcelCell(int rowNum, int cellNum) {
  return readStringExcelCell(this.sheetNum, rowNum, cellNum);
 }
  
 /**
  * 获取指定工作表、行、列的内容
  * @param sheetNum 表编号
  * @param rowNum 行编号
  * @param cellNum 列编号
  * @return 内容
  */
 public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
  if(sheetNum < 0 || rowNum < 0) {
   return "";
  }
  String strExcelCell = "";
  try {
   sheet = wb.getSheetAt(sheetNum);
   row = sheet.getRow(rowNum);
    
   if(row.getCell(cellNum) != null) {
    switch(row.getCell(cellNum).getCellType()) {
    case HSSFCell.CELL_TYPE_FORMULA:
     strExcelCell = "FORMULA";
     break;
    case HSSFCell.CELL_TYPE_NUMERIC:
     //strExcelCell = String.valueOf(row.getCell(cellNum).getNumericCellValue());
     //防止科学计数,不需要的话可以用上一行
     strExcelCell = decimalFormat(row.getCell(cellNum).getNumericCellValue());
     break;
    case HSSFCell.CELL_TYPE_STRING:
     strExcelCell = row.getCell(cellNum).getStringCellValue();
     break;
    case HSSFCell.CELL_TYPE_BLANK:
     strExcelCell = "";
     break;
    default:
     strExcelCell = "";
     break;
    }
   }
  catch (Exception e) {
   e.printStackTrace();
  }
  return strExcelCell;
 }
  
 /**
  * 测试
  * @param args
  */
 public static void main(String[] args) {
  String filePath = "C:\\Users\\Administrator\\Desktop\\工作簿1.xlsx";
  ImportExcel ie = new ImportExcel();
  try {
   ie.open(filePath);
  catch (IOException e) {
   e.printStackTrace();
  }
  ie.setSheetNum(0);
  int count = ie.getRowCount();
  for (int i = 0; i <= count; i++) {
   String[] rows = ie.readExcelLine(i);
   for (int j = 0; j < rows.length; j++) {
    System.out.print(rows[j] + " ");
   }
   System.out.print("\n");
  }
 }
  
 /**
  * 验证文件是否存在以及是否是excel文件
  * @param filePath
  * @return 结果
  */
 public boolean validateExcel(String filePath) {
  if(filePath == null || ! (isExcel2003(filePath) || isExcel2007(filePath))) {
   System.err.println("文件不是excel文件!");
   return false;
  }
  File file = new File(filePath);
  if(file == null || ! file.exists()) {
   System.err.println("文件不存在!");
   return false;
  }
  return true;
 }
  
 /**
  * 判断是否是2003版excel
  * @param filePath 文件路径
  * @return 结果
  */
 public boolean isExcel2003(String filePath) {
  return filePath.matches("^.+\\.(?i)(xls)$");
 }
  
 /**
  * 判断是否是2007版excel
  * @param filePath 文件路径
  * @return 结果
  */
 public boolean isExcel2007(String filePath) {
  return filePath.matches("^.+\\.(?i)(xlsx)$");
 }
  
 /**
  * 将获取到的数据类型转换成String防止科学计数法
  * @param decimal 数据
  * @return 结果
  */
 public String decimalFormat(Double decimal) {
  return df.format(decimal);
 }
}

内容摘自:

    java读取excel    http://java解析获取Excel中的数据--同时兼容2003及2007

0 0
原创粉丝点击