记录一下工作中使用的poi(可以做为工具类来使用)
/**
* 处理excel读入的工具类
* Created by Liujishuai on 2015/8/5.
*/
public class ExcelUtils {
/**
* 要求excel版本在2007以上
*
* @param file 文件信息
* @return
* @throws Exception
*/
public static List<List<Object>> readExcel(File file) throws Exception {
if(!file.exists()){
throw new Exception("找不到文件");
}
List<List<Object>> list = new LinkedList<List<Object>>();
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
// 读取第一张表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null;
for (int i = (sheet.getFirstRowNum() + 1); i <= (sheet.getPhysicalNumberOfRows() - 1); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
Object value = null;
cell = row.getCell(j);
if (cell == null) {
continue;
}
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
//String类型返回String数据
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
//日期数据返回LONG类型的时间戳
if ("yyyy\"年\"m\"月\"d\"日\";@".equals(cell.getCellStyle().getDataFormatString())) {
//System.out.println(cell.getNumericCellValue()+":日期格式:"+cell.getCellStyle().getDataFormatString());
value = DateUtils.getMillis(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())) / 1000;
} else {
//数值类型返回double类型的数字
//System.out.println(cell.getNumericCellValue()+":格式:"+cell.getCellStyle().getDataFormatString());
value = cell.getNumericCellValue();
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
//布尔类型
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
//空单元格
break;
default:
value = cell.toString();
}
if (value != null && !value.equals("")) {
//单元格不为空,则加入列表
linked.add(value);
}
}
if (linked.size()!= 0) {
list.add(linked);
}
}
return list;
}
/**
* 要求excel版本在2007以上
*
* @param fileInputStream 文件信息
* @return
* @throws Exception
*/
public static List<List<Object>> readExcel(FileInputStream fileInputStream) throws Exception {
List<List<Object>> list = new LinkedList<List<Object>>();
XSSFWorkbook xwb = new XSSFWorkbook(fileInputStream);
// 读取第一张表格内容
XSSFSheet sheet = xwb.getSheetAt(1);
XSSFRow row = null;
XSSFCell cell = null;
for (int i = (sheet.getFirstRowNum() + 1); i <= (sheet.getPhysicalNumberOfRows() - 1); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
Object value = null;
cell = row.getCell(j);
if (cell == null) {
continue;
}
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
if ("yyyy\"年\"m\"月\"d\"日\";@".equals(cell.getCellStyle().getDataFormatString())) {
//System.out.println(cell.getNumericCellValue()+":日期格式:"+cell.getCellStyle().getDataFormatString());
value = DateUtils.getMillis(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())) / 1000;
} else {
//System.out.println(cell.getNumericCellValue()+":格式:"+cell.getCellStyle().getDataFormatString());
value = cell.getNumericCellValue();
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
break;
default:
value = cell.toString();
}
if (value != null && !value.equals("")) {
//单元格不为空,则加入列表
linked.add(value);
}
}
if (linked.size()!= 0) {
list.add(linked);
}
}
return list;
}
/**
* 导出excel
* @param excel_name 导出的excel路径(需要带.xlsx)
* @param headList excel的标题备注名称
* @param fieldList excel的标题字段(与数据中map中键值对应)
* @param dataList excel数据
* @throws Exception
*/
public static void createExcel(String excel_name, String[] headList,
String[] fieldList, List<Map<String, Object>> dataList)
throws Exception {
// 创建新的Excel 工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
XSSFSheet sheet = workbook.createSheet();
// 在索引0的位置创建行(最顶端的行)
XSSFRow row = sheet.createRow(0);
// 设置excel头(第一行)的头名称
for (int i = 0; i < headList.length; i++) {
// 在索引0的位置创建单元格(左上端)
XSSFCell cell = row.createCell(i);
// 定义单元格为字符串类型
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
cell.setCellValue(headList[i]);
}
// ===============================================================
//添加数据
for (int n = 0; n < dataList.size(); n++) {
// 在索引1的位置创建行(最顶端的行)
XSSFRow row_value = sheet.createRow(n + 1);
Map<String, Object> dataMap = dataList.get(n);
// ===============================================================
for (int i = 0; i < fieldList.length; i++) {
// 在索引0的位置创建单元格(左上端)
XSSFCell cell = row_value.createCell(i);
// 定义单元格为字符串类型
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
cell.setCellValue((dataMap.get(fieldList[i])).toString());
}
// ===============================================================
}
// 新建一输出文件流
FileOutputStream fos = new FileOutputStream(excel_name);
// 把相应的Excel 工作簿存盘
workbook.write(fos);
fos.flush();
// 操作结束,关闭文件
fos.close();
}
}
在使用poi之前要导入相关的jar包(poi的jar包的作用)
https://github.com/ddodn/wem/issues/1
https://www.python.org/users/niaogezong/
https://www.python.org/users/ljliop/
https://dribbble.com/i134i/shots
https://dribbble.com/f163f/shots
https://dribbble.com/a195a/shots
https://dribbble.com/y205y/shots
https://dribbble.com/m387m/shots
https://dribbble.com/l675l/shots
https://dribbble.com/t548t/shots
https://dribbble.com/f622f/shots
https://dribbble.com/b852b/shots
https://dribbble.com/o503o/shots
https://dribbble.com/s623s/shots
https://dribbble.com/s952s/shots
https://dribbble.com/p643p/shots
https://dribbble.com/z946z/shots
https://dribbble.com/i916i/shots
https://dribbble.com/n281n/collections/4192880
https://dribbble.com/n281n/collections/4192879
https://dribbble.com/n281n/collections/4192877
https://dribbble.com/n281n/collections/4192876
https://dribbble.com/n281n/collections/4192874
https://dribbble.com/n281n/collections/4192873
https://dribbble.com/n281n/collections/4192871
https://dribbble.com/n281n/collections/4192870
https://dribbble.com/n281n/collections/4192868
https://dribbble.com/n281n/collections/4192867
https://dribbble.com/n281n/collections/4192864
https://dribbble.com/n281n/collections/4192862
https://dribbble.com/n281n/collections/4192861
https://dribbble.com/n281n/collections/4192859
https://dribbble.com/n281n/collections/4192858
https://dribbble.com/n281n/collections/4192856
https://dribbble.com/n281n/collections/4192854
https://dribbble.com/n281n/collections/4192853
https://dribbble.com/i134i/collections/4192916
https://dribbble.com/i134i/collections/4192914
https://dribbble.com/i134i/collections/4192913
https://dribbble.com/i134i/collections/4192911
https://dribbble.com/i134i/collections/4192909
https://dribbble.com/i134i/collections/4192905
https://dribbble.com/i134i/collections/4192904
https://dribbble.com/i134i/collections/4192902
https://dribbble.com/i134i/collections/4192901
https://dribbble.com/i134i/collections/4192900
https://dribbble.com/i134i/collections/4192899
https://dribbble.com/i134i/collections/4192896
https://dribbble.com/i134i/collections/4192895
https://dribbble.com/i134i/collections/4192893
https://dribbble.com/i134i/collections/4192892
https://dribbble.com/i134i/collections/4192890
https://dribbble.com/i134i/collections/4192889
https://dribbble.com/i134i/collections/4192887
https://dribbble.com/f163f/collections/4192952
https://dribbble.com/f163f/collections/4192951
https://dribbble.com/f163f/collections/4192949
https://dribbble.com/f163f/collections/4192948
https://dribbble.com/f163f/collections/4192945
https://dribbble.com/f163f/collections/4192943
https://dribbble.com/f163f/collections/4192941
https://dribbble.com/f163f/collections/4192938
https://dribbble.com/f163f/collections/4192937
https://dribbble.com/f163f/collections/4192934
https://dribbble.com/f163f/collections/4192933
https://dribbble.com/f163f/collections/4192932
https://dribbble.com/f163f/collections/4192931
https://dribbble.com/f163f/collections/4192930
https://dribbble.com/f163f/collections/4192929
https://dribbble.com/f163f/collections/4192926
https://dribbble.com/f163f/collections/4192925
https://dribbble.com/f163f/collections/4192923
https://dribbble.com/a195a/collections/4192996
https://dribbble.com/a195a/collections/4192995
https://dribbble.com/a195a/collections/4192993
https://dribbble.com/a195a/collections/4192990
https://dribbble.com/a195a/collections/4192989
https://dribbble.com/a195a/collections/4192987
https://dribbble.com/a195a/collections/4192985
https://dribbble.com/a195a/collections/4192983
https://dribbble.com/a195a/collections/4192980
https://dribbble.com/a195a/collections/4192979
https://dribbble.com/a195a/collections/4192977
https://dribbble.com/a195a/collections/4192975
https://dribbble.com/a195a/collections/4192973
https://dribbble.com/a195a/collections/4192971
https://dribbble.com/a195a/collections/4192969
https://dribbble.com/a195a/collections/4192967
https://dribbble.com/a195a/collections/4192966
https://dribbble.com/a195a/collections/4192962
https://dribbble.com/y205y/collections/4193035
https://dribbble.com/y205y/collections/4193033
https://dribbble.com/y205y/collections/4193032
https://dribbble.com/y205y/collections/4193030
https://dribbble.com/y205y/collections/4193029
https://dribbble.com/y205y/collections/4193026
https://dribbble.com/y205y/collections/4193025
https://dribbble.com/y205y/collections/4193023
https://dribbble.com/y205y/collections/4193020
https://dribbble.com/y205y/collections/4193018
https://dribbble.com/y205y/collections/4193016
https://dribbble.com/y205y/collections/4193015
https://dribbble.com/y205y/collections/4193013
https://dribbble.com/y205y/collections/4193012
https://dribbble.com/y205y/collections/4193010
https://dribbble.com/y205y/collections/4193007
https://dribbble.com/y205y/collections/4193006
https://dribbble.com/y205y/collections/4193003
https://dribbble.com/m387m/collections/4193156
https://dribbble.com/m387m/collections/4193153
https://dribbble.com/m387m/collections/4193151
https://dribbble.com/m387m/collections/4193129
https://dribbble.com/m387m/collections/4193127
https://dribbble.com/m387m/collections/4193124
https://dribbble.com/m387m/collections/4193123
https://dribbble.com/m387m/collections/4193121
https://dribbble.com/m387m/collections/4193119
https://dribbble.com/m387m/collections/4193117
https://dribbble.com/m387m/collections/4193111
https://dribbble.com/m387m/collections/4193110
https://dribbble.com/m387m/collections/4193108
https://dribbble.com/m387m/collections/4193106
https://dribbble.com/m387m/collections/4193104
https://dribbble.com/m387m/collections/4193080
https://dribbble.com/m387m/collections/4193043
https://dribbble.com/l675l/collections/4193216
https://dribbble.com/l675l/collections/4193215
https://dribbble.com/l675l/collections/4193214
https://dribbble.com/l675l/collections/4193213
https://dribbble.com/l675l/collections/4193210
https://dribbble.com/l675l/collections/4193209
https://dribbble.com/l675l/collections/4193207
https://dribbble.com/l675l/collections/4193205
https://dribbble.com/l675l/collections/4193204
https://dribbble.com/l675l/collections/4193202
https://dribbble.com/l675l/collections/4193201
https://dribbble.com/l675l/collections/4193198
https://dribbble.com/l675l/collections/4193196
https://dribbble.com/l675l/collections/4193195
https://dribbble.com/l675l/collections/4193193
https://dribbble.com/l675l/collections/4193191
https://dribbble.com/l675l/collections/4193189
https://dribbble.com/l675l/collections/4193188
https://dribbble.com/t548t/collections/4193249
https://dribbble.com/t548t/collections/4193248
https://dribbble.com/t548t/collections/4193246
https://dribbble.com/t548t/collections/4193245
https://dribbble.com/t548t/collections/4193243
https://dribbble.com/t548t/collections/4193242
https://dribbble.com/t548t/collections/4193240
https://dribbble.com/t548t/collections/4193239
https://dribbble.com/t548t/collections/4193237
https://dribbble.com/t548t/collections/4193235
https://dribbble.com/t548t/collections/4193234
https://dribbble.com/t548t/collections/4193232
https://dribbble.com/t548t/collections/4193230
https://dribbble.com/t548t/collections/4193229
https://dribbble.com/t548t/collections/4193227
https://dribbble.com/t548t/collections/4193226
https://dribbble.com/t548t/collections/4193224
https://dribbble.com/t548t/collections/4193222
https://dribbble.com/f622f/collections/4193290
https://dribbble.com/f622f/collections/4193289
https://dribbble.com/f622f/collections/4193287
https://dribbble.com/f622f/collections/4193284
https://dribbble.com/f622f/collections/4193283
https://dribbble.com/f622f/collections/4193281
https://dribbble.com/f622f/collections/4193280
https://dribbble.com/f622f/collections/4193278
https://dribbble.com/f622f/collections/4193276
https://dribbble.com/f622f/collections/4193274
https://dribbble.com/f622f/collections/4193273
https://dribbble.com/f622f/collections/4193271
https://dribbble.com/f622f/collections/4193269
https://dribbble.com/f622f/collections/4193268
https://dribbble.com/f622f/collections/4193267
https://dribbble.com/f622f/collections/4193264
https://dribbble.com/f622f/collections/4193262
https://dribbble.com/f622f/collections/4193260
https://dribbble.com/b852b/collections/4193335
https://dribbble.com/b852b/collections/4193334
https://dribbble.com/b852b/collections/4193330
https://dribbble.com/b852b/collections/4193328
https://dribbble.com/b852b/collections/4193327
https://dribbble.com/b852b/collections/4193324
https://dribbble.com/b852b/collections/4193323
https://dribbble.com/b852b/collections/4193320
https://dribbble.com/b852b/collections/4193318
https://dribbble.com/b852b/collections/4193317
https://dribbble.com/b852b/collections/4193314
https://dribbble.com/b852b/collections/4193313
https://dribbble.com/b852b/collections/4193310
https://dribbble.com/b852b/collections/4193309
https://dribbble.com/b852b/collections/4193307
https://dribbble.com/b852b/collections/4193305
https://dribbble.com/b852b/collections/4193303
https://dribbble.com/b852b/collections/4193300
https://dribbble.com/o503o/collections/4193371
https://dribbble.com/o503o/collections/4193369
https://dribbble.com/o503o/collections/4193367
https://dribbble.com/o503o/collections/4193366
https://dribbble.com/o503o/collections/4193364
https://dribbble.com/o503o/collections/4193362
https://dribbble.com/o503o/collections/4193361
https://dribbble.com/o503o/collections/4193359
https://dribbble.com/o503o/collections/4193358
https://dribbble.com/o503o/collections/4193356
https://dribbble.com/o503o/collections/4193353
https://dribbble.com/o503o/collections/4193352
https://dribbble.com/o503o/collections/4193350
https://dribbble.com/o503o/collections/4193348
https://dribbble.com/o503o/collections/4193347
https://dribbble.com/o503o/collections/4193345
https://dribbble.com/o503o/collections/4193344
https://dribbble.com/o503o/collections/4193342
https://dribbble.com/s623s/collections/4193405
https://dribbble.com/s623s/collections/4193404
https://dribbble.com/s623s/collections/4193402
https://dribbble.com/s623s/collections/4193401
https://dribbble.com/s623s/collections/4193399
https://dribbble.com/s623s/collections/4193396
https://dribbble.com/s623s/collections/4193395
https://dribbble.com/s623s/collections/4193393
https://dribbble.com/s623s/collections/4193391
https://dribbble.com/s623s/collections/4193390
https://dribbble.com/s623s/collections/4193388
https://dribbble.com/s623s/collections/4193387
https://dribbble.com/s623s/collections/4193386
https://dribbble.com/s623s/collections/4193385
https://dribbble.com/s623s/collections/4193383
https://dribbble.com/s623s/collections/4193381
https://dribbble.com/s623s/collections/4193380
https://dribbble.com/s623s/collections/4193377
https://dribbble.com/s952s/collections/4193442
https://dribbble.com/s952s/collections/4193441
https://dribbble.com/s952s/collections/4193439
https://dribbble.com/s952s/collections/4193437
https://dribbble.com/s952s/collections/4193436
https://dribbble.com/s952s/collections/4193433
https://dribbble.com/s952s/collections/4193432
https://dribbble.com/s952s/collections/4193430
https://dribbble.com/s952s/collections/4193429
https://dribbble.com/s952s/collections/4193427
https://dribbble.com/s952s/collections/4193426
https://dribbble.com/s952s/collections/4193424
https://dribbble.com/s952s/collections/4193423
https://dribbble.com/s952s/collections/4193421
https://dribbble.com/s952s/collections/4193418
https://dribbble.com/s952s/collections/4193417
https://dribbble.com/s952s/collections/4193415
https://dribbble.com/s952s/collections/4193414
https://dribbble.com/p643p/collections/4193482
https://dribbble.com/p643p/collections/4193480
https://dribbble.com/p643p/collections/4193478
https://dribbble.com/p643p/collections/4193474
https://dribbble.com/p643p/collections/4193473
https://dribbble.com/p643p/collections/4193471
https://dribbble.com/p643p/collections/4193469
https://dribbble.com/p643p/collections/4193468
https://dribbble.com/p643p/collections/4193466
https://dribbble.com/p643p/collections/4193463
https://dribbble.com/p643p/collections/4193462
https://dribbble.com/p643p/collections/4193459
https://dribbble.com/p643p/collections/4193458
https://dribbble.com/p643p/collections/4193456
https://dribbble.com/p643p/collections/4193454
https://dribbble.com/p643p/collections/4193453
https://dribbble.com/p643p/collections/4193451
https://dribbble.com/p643p/collections/4193450
https://dribbble.com/z946z/collections/4193517
https://dribbble.com/z946z/collections/4193515
https://dribbble.com/z946z/collections/4193514
https://dribbble.com/z946z/collections/4193512
https://dribbble.com/z946z/collections/4193510
https://dribbble.com/z946z/collections/4193509
https://dribbble.com/z946z/collections/4193507
https://dribbble.com/z946z/collections/4193506
https://dribbble.com/z946z/collections/4193504
https://dribbble.com/z946z/collections/4193503
https://dribbble.com/z946z/collections/4193501
https://dribbble.com/z946z/collections/4193499
https://dribbble.com/z946z/collections/4193498
https://dribbble.com/z946z/collections/4193496
https://dribbble.com/z946z/collections/4193495
https://dribbble.com/z946z/collections/4193493
https://dribbble.com/z946z/collections/4193492
https://dribbble.com/z946z/collections/4193491
https://dribbble.com/i916i/collections/4193557
https://dribbble.com/i916i/collections/4193555
https://dribbble.com/i916i/collections/4193554
https://dribbble.com/i916i/collections/4193551
https://dribbble.com/i916i/collections/4193550
https://dribbble.com/i916i/collections/4193549
https://dribbble.com/i916i/collections/4193548
https://dribbble.com/i916i/collections/4193546
https://dribbble.com/i916i/collections/4193545
https://dribbble.com/i916i/collections/4193542
https://dribbble.com/i916i/collections/4193540
https://dribbble.com/i916i/collections/4193538
https://dribbble.com/i916i/collections/4193536
https://dribbble.com/i916i/collections/4193534
https://dribbble.com/i916i/collections/4193532
https://dribbble.com/i916i/collections/4193530
https://dribbble.com/i916i/collections/4193528
https://dribbble.com/i916i/collections/4193527
https://dribbble.com/n281n/collections
https://dribbble.com/i134i/collections
https://dribbble.com/f163f/collections
https://dribbble.com/a195a/collections
https://dribbble.com/y205y/collections
https://dribbble.com/m387m/collections
https://dribbble.com/l675l/collections
https://dribbble.com/t548t/collections
https://dribbble.com/f622f/collections
https://dribbble.com/b852b/collections
https://dribbble.com/o503o/collections
https://dribbble.com/s623s/collections
https://dribbble.com/s952s/collections
https://dribbble.com/p643p/collections
https://dribbble.com/z946z/collections
https://dribbble.com/i916i/collections
使用之前导入jar包
<!--poi对excel2007以上版本的支持-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
在使用poi时在导入excel文件时遇到了### java.lang.ClassNotFoundException
异常
解决的方案是:
导入了jar包
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
要注意版本,不要造成版本冲突.