public void batchDownLoadcustomerInfo(SearchAssist searchAssist, HttpServletRequest request, HttpServletResponse response ) {
HashSet<String> traderNameSets = new HashSet<>();
try {
// 获取excel路径
Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(searchAssist.getRelateStr())));
// 获取第一面sheet
Sheet sheet = workbook.getSheetAt(0);
// 起始行
int startRowNum = sheet.getFirstRowNum() + 1;
// 结束行
int endRowNum = sheet.getLastRowNum();
// 循环行数
for (int rowNum = startRowNum; rowNum <= endRowNum; rowNum++) {
Row row = sheet.getRow(rowNum);
// 起始列
int startCellNum = row.getFirstCellNum();
// 获取excel的值
String traderName = null;
// 循环列数(下表从0开始)--注意---此处的6是根据表格的列数来确定的,表格列数修改此处要跟着修改
Cell cell = row.getCell(startCellNum);
String traderNameRegex = "^[a-zA-Z0-9\\u4e00-\\u9fa5\\.\\(\\)\\,\\(\\)\\s]{2,128}$";
//客户名称
if (startCellNum == 0) {
//若excel中无内容,而且没有空格,cell为空--默认3,空白
if (cell == null) {
throw new Exception("表格项错误,第" + (rowNum + 1) + "行" + (startCellNum + 1) + "列客户名称不允许为空!");
} else if (cell.getStringCellValue().toString().length() < 2 || cell.getStringCellValue().toString().length() > 128) {
throw new Exception("表格项错误,第" + (rowNum + 1) + "行" + (startCellNum + 1) + "列客户名称长度为2-128个字符长度!");
} else if (!RegexUtil.match(traderNameRegex, cell.getStringCellValue().toString())) {
throw new Exception("表格项错误,第" + (rowNum + 1) + "行" + (startCellNum + 1) + "列客户名称不允许使用特殊字符!");
} else {
traderName = cell.getStringCellValue();
}
}
traderNameSets.add(traderName);
}
//遍历客户
Iterator<String> traderName = traderNameSets.iterator();
while (traderName.hasNext()) {
String traderNameStr = traderName.next();
searchAssist.setExtendStr(traderNameStr);
StatementTableData statementTableData = saleExportService.getStatementTableData(searchAssist);
if (Objects.isNull(statementTableData)){
continue;
}
//赋值表头、表尾数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("customerName",statementTableData.getCustomerName() );
map.put("contact", statementTableData.getContact());
map.put("mobilePhone", statementTableData.getMobilePhone());
map.put("address", statementTableData.getAddress());
map.put("startTime", statementTableData.getStartTime());
map.put("endTime", statementTableData.getEndTime());
map.put("totlOrderMoney", statementTableData.getTotlOrderMoney());
map.put("totlAlreaderMoney",statementTableData.getTotlAlreaderMoney() );
map.put("totlNoPayMoney", statementTableData.getTotlNoPayMoney());
map.put("totlMkInvoiceMoney", statementTableData.getTotlMkInvoiceMoney());
map.put("totlMoney",statementTableData.getTotlMoney());
map.put("excelMakeTime",statementTableData.getExcelMakeTime());
//表detailList数据赋值
map.put("maplist", statementTableData.getStatementDetailDataListMap());
// 导出excle
// 设置导出配置
// 获取导出excel指定模版
File file1 = new File( request.getSession().getServletContext().getRealPath("/static/template/statementsTemplate.xlsx"));
String realPath = file1.getCanonicalPath();
TemplateExportParams params = new TemplateExportParams(realPath);
// 导出excel
FileOutputStream fos = new FileOutputStream(tempExcleFilePath + traderNameStr +"对账单.xlsx");
Workbook workbookExport = ExcelExportUtil.exportExcel(params, map);
workbookExport.write(fos);
fos.close();
}
String zipFileName = "statements.zip";
//创建ZIP文件
Boolean result = cretateZipFile(zipFileName, request);
if (!result){
alertShowMessage(response, "查询结果为空,请重新选择导出条件!");
return;
}
//将ZIP文件输出到浏览器
outputToBrowser(zipFileName,response,request);
//下载完成之后,删掉这个zip包
deleteZipFile(request);
} catch (Exception e) {
logger.info(e.getMessage());
}
}
private FileInfo fileUploadServe(String path, MultipartFile lwfile) {
FileInfo result = new FileInfo();
// 上传文件名称
String fileName = lwfile.getOriginalFilename();
// 文件后缀名称
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
// 时间戳
String time = DateUtil.gainNowDate() + "";
Random random = new Random();
time = time + "_" + String.format("%04d", random.nextInt(10000));// 随机数4位,不足4位,补位
String new_fileName = time + "." + prefix;// 新文件名称
Calendar calendar = Calendar.getInstance();
// 获取年份
int year = calendar.get(Calendar.YEAR);
// 获取月份
int month = calendar.get(Calendar.MONTH) + 1;
// 拼接年份和月份
String date = year + "-" + String.format("%02d", month);
String day = String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH));
String save_path = path + "/" + date + "/" + day + "/file";
File targetFile = new File(save_path, new_fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
lwfile.transferTo(targetFile);
result.setCode(0);
result.setMessage("上传成功");
result.setPrefix(prefix);
result.setFilePath(save_path + File.separator + new_fileName);
} catch (Exception e) {
return result;
}
return result;
}
/**
* 创建Zip文件
* @throws Exception
*/
private Boolean cretateZipFile(String zipFileName, HttpServletRequest request) throws Exception{
Long startTime = System.currentTimeMillis();
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFilePath + zipFileName)));
File[] files = new File(tempExcleFilePath).listFiles();
if (files == null){
return Boolean.FALSE;
}
for (File file : files) {
InputStream fileInputStream = new FileInputStream(file.getPath());
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, 1024 * 10);
zipOut.putNextEntry(new ZipEntry(file.getName()));
// 缓冲
byte[] buffer = new byte[1024 * 10];
int length = 0;
while ((length = bufferedInputStream.read(buffer, 0, 1024 * 10)) != -1) {
zipOut.write(buffer, 0, length);
}
//关闭输入流流
fileInputStream.close();
bufferedInputStream.close();
}
zipOut.close();
Long endTime = System.currentTimeMillis();
logger.info("生成zip文件耗费的时间:{} ms",endTime-startTime);
return Boolean.TRUE;
}
/**
* 将ZIP文件输出到浏览器
* @param zipFileName
* @param response
* @throws Exception
*/
private void outputToBrowser(String zipFileName, HttpServletResponse response,HttpServletRequest request) throws Exception{
String displayName = new String(zipFileName.getBytes("UTF-8"), "ISO-8859-1");
response.setHeader("Content-Disposition", "attchment;filename=\"" + displayName+"\"");
response.setContentType("multipart/octet-stream");
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(zipFilePath + zipFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
// 关闭流
inputStream.close();
outputStream.close();
}
/**
* 删除导出过程中产生的文件
*/
private void deleteZipFile( HttpServletRequest request) {
//删除生成的zip问价
File[] files = new File(zipFilePath).listFiles();
if (files != null){
for (File file : files) {
deleteFile(file);
}
}
//删除生成的客户excle问价
File[] filesExcle = new File(tempExcleFilePath).listFiles();
if (filesExcle != null){
for (File file : filesExcle) {
deleteFile(file);
}
}
//删除上传的客户文件
String filesUploadPath = request.getSession().getServletContext().getRealPath("/upload/trader");
File[] filesUpload = new File(filesUploadPath).listFiles();
if (filesUpload != null){
for (File file : filesUpload) {
deleteFile(file);
}
}
}
/**
* 删除文件
*/
public static void deleteFile(File file){
// 判断是否是文件
if(file.isFile()){
// 是文件就直接删除
file.delete();
}
// 判断是否是文件夹
if(file.isDirectory()){
// 列出该文件夹下的所有文件目录
File[] files = file.listFiles();
if(files.length >0){
// 递归 删除这个文件夹中的所有文件
for (File file2: files) {
deleteFile(file2);
}
}
// 删除外层文件
file.delete();
}
}
12-16
1045