头图

支持pdf/图片/表格等格式文件装换成excel文件或其他格式文件

首先,图片识别过程

 @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);
        }
    }

接着,写出excel文件

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();
    }

最后,效果图

提取效果图

更多具体功能实现微/电:16717696360

业务范围


Jame
13 声望1 粉丝

专注技术开发、自动化办公脚本、数据处理、数据爬取...Wecat:W13611455764