今天试了一下从Java向excel导入数据时,报错了
java.lang.NoSuchMethodError: org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V
首先我这个功能是要加入到最近在做的一个项目中去,然后我就单独写了一个demo来测试能否成功从Java中向excel中导入数据,结果能够正常使用。但当我将其放入到我的项目中去,然后就报错了,去网上百度了一些说是依赖冲突。我就去idea中看了一下maven,果然灰色部分显示了冲突
这个是我当时引入的依赖,在单独写的测试demo中没有问题
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
<!-- <exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
</exclusions> -->
</dependency>
然后我就将上面的注释部分加入进去了,然后就没有显示有冲突了
本以为这下能够成功了,结果还是报同样的错误,然后我就改了依赖的版本号,将 poi-ooxml 这个依赖从3.14改成了3.16,但是maven还是显示了有问题
唉!还是得修改依赖,
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
</exclusions>
</dependency>
maven这下也没有显示冲突之类的了,哈哈!这次也算是瞎猫碰上死耗子,还真让我给改对了
最后就成功导入到excel中了
再附上代码部分吧,希望能够帮到有需要的人
调用方法
@GetMapping("/toExcel")
@ResponseBody
public R toExcel(){
// 查找数据
List<Map<String, Object>> maps = declareOrderInfoService.toExcel();
System.out.println("需导入的数据:");
System.out.println(maps);
String filePath = "D:\\police.xlsx";
boolean flag = ToExcel.fileExist(filePath);
System.out.println("flag:"+flag);
if (flag){
ToExcel.writeExcel(maps,filePath);
}else {
File file = new File(filePath);
ToExcel.writeExcel(maps,filePath);
}
return R.ok();
}
功能类
public class ToExcel {
//判断文件是否存在
public static boolean fileExist(String filePath){
boolean flag = false;
File file = new File(filePath);
flag = file.exists();
return flag;
}
//向Excel中写数据
public static void writeExcel(List<Map<String, Object>> list , String filePath){
System.out.println("已经开始导入了");
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("aspolice");
XSSFRow firstRow = sheet.createRow(0);//第一行表头
XSSFCell cells[] = new XSSFCell[8];
String[] titles = new String[]{"批次名称","总金额","申请人","品种名称","购买数量"," 价格","上衣型号","下衣型号"};
//循环设置表头信息
for (int i=0;i<8;i++){
cells[0]=firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
//遍历list,将数据写入Excel中
for (int i=0;i<list.size();i++){
XSSFRow row = sheet.createRow(i+1);
Map<String,Object> map = list.get(i);
XSSFCell cell = row.createCell(0); //第一列
cell.setCellValue(map.get("name").toString());
cell=row.createCell(1); //第二列
cell.setCellValue(map.get("totalPrice").toString());
cell=row.createCell(2); //第三列
cell.setCellValue(map.get("creator").toString());
cell=row.createCell(3); //第三列
cell.setCellValue(map.get("proName").toString());
cell=row.createCell(4); //第三列
cell.setCellValue(map.get("buyCount").toString());
cell=row.createCell(5); //第三列
cell.setCellValue(map.get("price").toString());
cell=row.createCell(6); //第三列
cell.setCellValue(map.get("syhx").toString());
cell=row.createCell(7); //第三列
cell.setCellValue(map.get("xyhx").toString());
}
OutputStream out = null;
try {
out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
} catch (Exception e){
e.printStackTrace();
}
}
}