使用 Apache poi 导入Excel
本文主要记录Excel导入及模板下载,遇到的问题及注意事项。
第一节:Excel导入
/**
* @param returnMap
* @Title: dealExcelData
* @Description: TODO(保存Excel中的数据,并过滤重复的记录)
* @author: yanghai * @param: @param contents 存储 Excel中的内容
* @param: @param item 上传的Excel元素
* @param: @param request
* @param: @param repeatCount
* @return: void
* @throws
*/
private void dealExcelData(List<CompanyInvoiceRecord> contents, MultipartFile item, HttpServletRequest request, Integer repeatCount, Map<String, Object> returnMap) throws Exception
{
List<Integer> l = new ArrayList<Integer>();
Integer count = 0;
CompanyInvoiceRecord dto = null; //临时文件名称
String tempDir = "/files-" +DateTool.formatDate(System.currentTimeMillis(), "yyyy-MM-dd-HH-mm");
//临时文件全路径
String tempFileDir = request.getSession().getServletContext().getRealPath(tempDir);
//创建临时文件
File tempFile = new File(tempFileDir);
if(!tempFile.exists())
{
tempFile.mkdir();
} //获取原始文件全名称
String originalFilename = item.getOriginalFilename();
// 获取文件后缀
String suffix = "";
try
{
suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
}
catch (Exception e)
{
e.printStackTrace();
throw new Exception("没有文件信息!");
}
//完整的文件目录
String fileName = tempFileDir + File.separator + originalFilename;
File newFile = new File(fileName); try
{
// 保存到一个目标文件中。
item.transferTo(newFile);
}
catch (Exception e)
{
e.printStackTrace();
throw new Exception("保存上传Excel文件失败!");
}
Workbook wb = null; FormulaEvaluator formulaEvaluator = null; try
{
FileInputStream inputStream = new FileInputStream(newFile); if(suffix.endsWith("xls"))
{
wb = new HSSFWorkbook(inputStream); formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
}
else
{
wb = new XSSFWorkbook(inputStream); formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) wb);
}
}
catch (IOException e)
{
// 删除目录
deleteDir(new File(tempFileDir));
e.printStackTrace(); } // 保存有效的 Excel行
List<Row> rowList = new ArrayList<Row>(); Sheet sheet = wb.getSheetAt(0);
if(null == sheet)
{
deleteDir(new File(tempFileDir));
throw new Exception("导入失败:导入文件中不存在sheet页!");
}
else
{
/*int lastRowNum = sheet.getLastRowNum();
System.out.println("==============="+lastRowNum);
if(lastRowNum > 5001)
{
throw new Exception("超过导入上限。最多导入5000条!");
}*/
try
{
for(Row row : sheet)
{
// 校验表头
if(row.getRowNum() == 0)
{
if(StringUtils.isNotEmpty(row.getCell(0).toString().trim()) && "付款日期".equals(row.getCell(0).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(1).toString().trim()) && "发票号".equals(row.getCell(1).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(2).toString().trim()) && "金额".equals(row.getCell(2).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(3).toString().trim()) && "税额".equals(row.getCell(3).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(4).toString().trim()) && "合计".equals(row.getCell(4).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(5).toString().trim()) && "公司名称".equals(row.getCell(5).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(6).toString().trim()) && "货物名称".equals(row.getCell(6).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(7).toString().trim()) && "申请人".equals(row.getCell(7).toString().trim())
&& StringUtils.isNotEmpty(row.getCell(8).toString().trim()) && "申请金额".equals(row.getCell(8).toString().trim())
)
{
continue;
}
else
{
deleteDir(new File(tempFileDir));
throw new Exception("表头信息错误!");
}
}
else if(row.getRowNum() >= 1)
{ try
{
if((null == row.getCell(0) || String.valueOf(row.getCell(0)).equals(""))
&& (null == row.getCell(1) || String.valueOf(row.getCell(1)).equals(""))
&& (null == row.getCell(2) || String.valueOf(row.getCell(2)).equals(""))
&& (null == row.getCell(3) || String.valueOf(row.getCell(3)).equals(""))
&& (null == row.getCell(4) || String.valueOf(row.getCell(4)).equals(""))
&& (null == row.getCell(5) || String.valueOf(row.getCell(5)).equals(""))
&& (null == row.getCell(6) || String.valueOf(row.getCell(6)).equals(""))
&& (null == row.getCell(7) || String.valueOf(row.getCell(7)).equals(""))
&& (null == row.getCell(8) || String.valueOf(row.getCell(8)).equals("")))
{
System.out.println("===公司费用 发票 导入 记录 导入===此行"+row.getRowNum()+"为空");
}
else
{
rowList.add(row);
}
} catch (Exception e1)
{
e1.printStackTrace();
}
}
} if(null != rowList && rowList.size() > 0)
{
int lastRowNum = rowList.size();
System.out.println("==============="+lastRowNum);
if(lastRowNum > 5001)
{
throw new Exception("超过导入上限。最多导入5000条!");
} for(Row row : rowList)
{
try
{
dto = new CompanyInvoiceRecord(); short lastCellNum = row.getLastCellNum();
if(lastCellNum < 1)
{
deleteDir(new File(tempFileDir));
throw new Exception("第" + row.getRowNum() + "行列数不足!");
} if(StringUtils.isNotEmpty(row.getCell(0).toString()))
{
// 付款日期
dto.setPayDate(readCellToStringToTrim(row.getCell(0)).toString().trim());
} row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
if(StringUtils.isNotEmpty(row.getCell(1).toString().trim()))
{
// 发票号码
dto.setInvoiceNum(readCell(row.getCell(1)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(2).toString()))
{
// 金额
dto.setAmount(readCell(row.getCell(2)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(3).toString()))
{
// 税额
dto.setTaxAmount(readCell(row.getCell(3)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(4).toString()))
{
// 合计
dto.setTotalAmount(readCell(row.getCell(4)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(5).toString()))
{
// 抬头
dto.setTitle(readCell(row.getCell(5)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(6).toString()))
{
// 货物名称
dto.setGoodsName(readCell(row.getCell(6)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(7).toString()))
{
// 申请人
dto.setApplicantName(readCell(row.getCell(7)).toString().trim());
}
if(StringUtils.isNotEmpty(row.getCell(8).toString()))
{
// 申请金额
dto.setApplyAmount(readCell(row.getCell(8)).toString().trim());
} if(StringUtils.isEmpty(dto.getPayDate()) && StringUtils.isEmpty(dto.getInvoiceNum())
&& StringUtils.isEmpty(dto.getAmount()) && StringUtils.isEmpty(dto.getTitle())
&& StringUtils.isEmpty(dto.getTaxAmount()) && StringUtils.isEmpty(dto.getTotalAmount())
&& StringUtils.isEmpty(dto.getGoodsName()) && StringUtils.isEmpty(dto.getApplicantName())
&& StringUtils.isEmpty(dto.getApplyAmount()))
{ }
else
{
Boolean flag = true;
if(contents.contains(dto)) // 已包含
{
repeatCount++;
flag = false;
}
if(flag)
{
contents.add(dto);
}
}
} catch (Exception e)
{
l.add(row.getRowNum()+1);
count++;
System.out.println("==公司费用导入异常:"+e);
}
}
} returnMap.put("count", count);
returnMap.put("repeatCount", repeatCount); System.out.println("======异常条数:"+count+",发生异常的行数分别是:"+l.toString());
// 删除excel
deleteDir(new File(tempFileDir)); } catch (Exception e)
{
e.printStackTrace();
deleteDir(new File(tempFileDir));
throw new Exception("请按照要求填写Excel的内容!");
}
}
} /**
* 删除文件夹及文件夹下的内容
* @param dir
* @return
*/
private boolean deleteDir(File dir)
{
if (dir.isDirectory())
{
String[] children = dir.list();
// 递归删除目录中的子目录下
for (int i = 0; i < children.length; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
* Numeric Cell type (0)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_NUMERIC = 0; // 数字类型 /**
* String Cell type (1)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_STRING = 1; // 字符串类型 /**
* Formula Cell type (2)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_FORMULA = 2; // 公式类型 /**
* Blank Cell type (3)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_BLANK = 3; // 空白类型 /**
* Boolean Cell type (4)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_BOOLEAN = 4; // 布尔类型 /**
* Error Cell type (5)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_ERROR = 5; // 错误类型
/**
* @description:读取Excel单元格数据
* @param cell excel单元格
* @return String
*/
private static String readCell(Cell cell)
{
String cell_value = ""; if (cell != null)
{
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
// 得到Boolean对象的方法
if (cell.getBooleanCellValue())
{
cell_value = "TRUE";
} else
{
cell_value = "FALSE";
}
break;
case Cell.CELL_TYPE_NUMERIC:
// 先看是否是日期格式
if (DateUtil.isCellDateFormatted(cell))
{
// 读取日期格式
cell_value = DateUtils.formatDate(cell.getDateCellValue(), "yyyy-MM-dd");
} else
{
// 读取数字
cell_value = String.valueOf(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA:
// 读取公式的值
cell_value = cell.getCellFormula();
break;
case Cell.CELL_TYPE_STRING:
// 读取String
cell_value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_ERROR:
cell_value = cell.getErrorCellValue() + "";
break;
case HSSFCell.CELL_TYPE_BLANK:
cell_value = "";
break;
default:
cell_value = "";
}
}
return cell_value;
}
第二节:Excel模板下载
@RequestMapping("/downExcel")
public ModelAndView downBlack(HttpServletRequest request, HttpServletResponse response) throws IOException
{
String realPathName = "";
String tempPath = "";
String fileName = ""; BufferedInputStream bis = null;
BufferedOutputStream bos = null; try {
tempPath = request.getSession().getServletContext().getRealPath("/") + "/download/";
fileName = "batchReceivedTicketTemplate.xlsx";
realPathName = tempPath + fileName; long fileLength = new File(tempPath + fileName).length(); // 文件下载设置response
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload;"); // 火狐
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
{
response.setHeader("Content-disposition", "attachment; filename=" + new String("批量收票导入模板.xlsx".getBytes("utf-8"), "ISO8859-1"));
}
else
{
response.setHeader("Content-Disposition", "attachment;filename=" + new String("批量收票导入模板.xlsx".getBytes("gb2312"), "ISO8859-1"));
} response.setHeader("Content-Length", String.valueOf(fileLength)); // 从模板获取输入流
bis = new BufferedInputStream(new FileInputStream(realPathName)); // 输出流
bos = new BufferedOutputStream(response.getOutputStream()); // 读取文件流输出
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} return null;
}
这段代码,很清晰,在实现的过程中,要注意一点,就是模板的后缀要和下载到的模板文件后缀保持一致。否则在导入的时候会报下面这个异常信息提示:
使用 Apache poi 导入Excel的更多相关文章
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
- Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)
ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...
- Java开发小技巧(六):使用Apache POI读取Excel
前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...
- 利用Apache POI操作Excel
最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...
- 在java poi导入Excel通用工具类示例详解
转: 在java poi导入Excel通用工具类示例详解 更新时间:2017年09月10日 14:21:36 作者:daochuwenziyao 我要评论 这篇文章主要给大家介绍了关于在j ...
- apache POI 导出excel相关方法
apache POI 操作excel无比强大.同时有操作word和ppt的接口. 下面讲解poi中常用方法. 1,设置列宽 HSSFSheet sheet = wb.getSheetAt(0); sh ...
- poi导入Excel,数字科学记数法转换
在这里分享一下使用poi 导入Excel时 把数字转换为科学记数法的解决方法: 就是使用DecimalFormat对 i 进行了格式化 结果为:
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- Java使用Apache POI进行Excel导入和导出
Manve依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> ...
随机推荐
- Android下的HttpClient的使用(9.11)
1 http://liangruijun.blog.51cto.com/3061169/803097 备注:此博客较早,android 4.0之后不允许在UI线程进行网络操作,所以没有输出结果. ...
- 【android】listview改变选中行背景图片
[android]listview改变选中行背景图片 目标:当item选中时,改变其背景图片.效果图如下: 直接在listview的xml文件中使用listselector: 1 2 3 4 5 6 ...
- eureka集群高可用配置,亲测成功配置(转)
转自大神的文章:https://blog.csdn.net/tianyaleixiaowu/article/details/78184793 网上讲这个东西的很多,抄来抄去的,大部分类似,多数没讲明白 ...
- 在Eclipse中使用JUnit4进行单元测试(初级篇、中级篇、高级篇)
本文转载自以下 初级篇: http://blog.csdn.net/andycpp/article/details/1327147 中级篇: http://blog.csdn.net/andycpp/ ...
- 前台freemark获取后台的值
1.后台代码: ModelAndView mv = new ModelAndView("log/logList.ftl"); String info="abc" ...
- Window10 安装问题汇总
7月28号之后,由于没有收到windows的升级提醒,所以下载了ISO文件手动进行了升级. 本文就升级过程中遇到的问题进行一下总结: 1.ISO文件名称:cn_windows_1 ...
- PHP代码中使用post参数上传大文件
今天连续碰到了两个同事向我反应上传大文件(8M)失败的事情! 都是在PHP代码中通常使用post参数进行上传文件时,当文件的大小大于8M时,上传不能不成功. 首先,我想到了nginx的client_m ...
- Laravel 手记(连接mysql)
这几天学习Laravel框架遇到了数据库方面的问题. PDOException in Connector.php line 55:SQLSTATE[HY000] [1045] Access denie ...
- Java final关键字特点
一.特点 1.由于继承,方法可以重写,所以父类的功能就会被子类覆盖2.有时候我们不想子类覆盖父类的功能,这时候我们可以使用final关键字3.final可以修饰:类.变量,方法.4.final修饰类, ...
- Windows下安装appium桌面版和命令行版
安装appium桌面版和命令行版 一 桌面版(打开很慢,常用于辅助元素定位) 1.官网下载window版本: github search appium desktop download late ...