@Test
void request_002() throws FileNotFoundException {
// 读取文件夹
String fileSource = "C:\\Users\\Administrator\\Desktop\\work\\20221217\\invoice\\pageFiles";
String fileName = fileSource + "\\excelFile\\" + "票据_" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx";
long beginTime = System.currentTimeMillis();
List<File> files = FileUtil.loopFiles(fileSource);
List<InvoiceVO> getList = new ArrayList<>();
for (File file : files) {Console.log("开始辨认文件:{}", file.getName());
// 申请参数
RecognizeInvoiceRequest request = new RecognizeInvoiceRequest();
RuntimeOptions runtime = new RuntimeOptions();
request.body = new FileInputStream(file.getPath());
try {RecognizeInvoiceResponse response = client().recognizeInvoiceWithOptions(request, runtime);
Console.log("文件:{} 辨认胜利", file.getName());
JSONObject jsonObject = JSONObject.parseObject(response.body.data);
String data = jsonObject.getString("data");
Console.log("data : => {}", data);
InvoiceVO invoiceData = JSONUtil.toBean(data, InvoiceVO.class);
getList.add(invoiceData);
} catch (TeaException error) {Console.log(error.message);
} catch (Exception _error) {TeaException error = new TeaException(_error.getMessage(), _error);
Console.log(error.message);
}
}
// 执行写出
if (getList.size() > 0) {Console.log("开始写出 excel 文件~");
toExcel(getList, fileName);
Console.log("文件:{} 写出胜利!总耗时:{} 秒", fileName, (System.currentTimeMillis() - beginTime) / 1000);
}
}
private void toExcel(List<InvoiceVO> getList, String filePathName) {// 合并单元格 ( 开始列,完结列)
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
int beforeRow = 1;
// 不进行合并的列
List<Integer> unMergeList = new ArrayList<>();
// 写出的文件列表
List<InvoiceVO> dataList = new ArrayList<>();
for (int i = 0; i < getList.size(); i++) {InvoiceVO invoiceVO = getList.get(i);
List<InvoiceDetails> details = invoiceVO.getInvoiceDetails();
for (InvoiceDetails detail : details) {InvoiceVO vo = new InvoiceVO();
BeanUtil.copyProperties(invoiceVO, vo);
BeanUtil.copyProperties(detail, vo);
dataList.add(vo);
}
// 避免越过合并解决
int detailSize = details.size();
int afterRowSize = beforeRow + detailSize;
treeMap.put(beforeRow, afterRowSize - 1);
if(detailSize <= 1){unMergeList.add(beforeRow);
}
beforeRow = afterRowSize;
}
// 写出文件
ExcelWriter writer = ExcelUtil.getWriter(filePathName);
// 题目
addHeader(writer);
// 主动列宽
writer.autoSizeColumnAll();
treeMap.forEach((k, v) -> {
// 一行详情就不进行合并
if(!unMergeList.contains(k)){for (int i = 0; i < 22; i++) {
//merge : 开始的列号,完结的列号,开始的行号,完结的行号,合并后的数据(主动填充输入数据的列表),是否保留原款式
writer.merge(k, v, i, i, "合并数据", false);
}
}
});
writer.setOnlyAlias(true);
writer.write(dataList,true);
writer.close();}