ofd 文件发票解析

您所在的位置:网站首页 ofd格式的发票怎么打开 ofd 文件发票解析

ofd 文件发票解析

#ofd 文件发票解析| 来源: 网络整理| 查看: 265

一、OFD

OFD(Open Fixed-layout Document) ,是由工业和信息化部软件司牵头中国电子技术标准化研究院成立的版式编写组制定的版式文档国家标准,属于中国的一种自主格式,要打破政府部门和党委机关电子公文格式不统一,以方便地进行电子文档的存储、读取以及编辑。

二、OFD格式发票解析 ofd文件类似与zip,解析思路是将其解压之后,然后对其中的xml文件进行处理,可以用dom4j,根据需要选择要处理的xml

OFD.XML

- - - 605a0643270f4874b54b541369ea24f7 China Tax 2020-09-10 - 1.0.20.0422 SuwellFormSDK 1.0.20.0603 033022000313 00000001 11283.71 86797.75 2020年09月01日 11058 76307 04808 54113 913302127995019136 91330203MA2H7WU45J Doc_0/Document.xml Doc_0/Signs/Signatures.xml

original_invoice.xml

- 03 033022000313 00000001 0 2020年09月01日 11058763070480854113 667900296527 00945*12*62377>93158 } /** * 日期格式化. */ static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日"); // 注意月份是MM public static void main(final String[] args) { // 1.解压ofd文件 try { final String xmlPath = unzip("C:\\Users\\qxy\\Desktop\\033022000313_00000001.ofd"); final SAXReader reader = new SAXReader(); Document doc; try { doc = reader.read(new File(xmlPath)); final Element rootElement = doc.getRootElement(); // 获取document对象根节点,即最外层节点下的内容 // 发票代码、发票号码、 Invoice invoice = new Invoice(); invoice = generateInvoice(rootElement, invoice); System.out.println(invoice.getInvoiceCode()); } catch (DocumentException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } /** * ofd发票文件解析. * @param filePath filePath * @return Invoice */ public static Invoice parseOfd(final String filePath) { // 发票代码、发票号码、 Invoice invoice = new Invoice(); try { final String xmlPath = unzip(filePath); final SAXReader reader = new SAXReader(); Document doc; try { doc = reader.read(new File(xmlPath)); final Element rootElement = doc.getRootElement(); // 获取document对象根节点,即最外层节点下的内容 invoice = generateInvoice(rootElement, invoice); System.out.println(invoice.getInvoiceCode()); } catch (DocumentException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return invoice; } /** * 生成发票信息. * @param rootElement rootElement * @param invoice invoice * @return Invoice */ private static Invoice generateInvoice(final Element rootElement, final Invoice invoice) { // 发票代码 final Element invoiceCodeElement = rootElement.element("InvoiceCode"); // 发票号码 final Element invoiceNoElement = rootElement.element("InvoiceNo"); // 开票日期 final Element issueDateElement = rootElement.element("IssueDate"); // 校验码 final Element invoiceCheckCodeElement = rootElement.element("InvoiceCheckCode"); // 机器编号 final Element machineNoElement = rootElement.element("MachineNo"); // 密码区 final Element taxControlCodeElement = rootElement.element("TaxControlCode"); // 购买方 final Element buyerElement = rootElement.element("Buyer"); // 销售方 final Element sellerElement = rootElement.element("Seller"); // 税额 final Element taxTotalAmountElement = rootElement.element("TaxTotalAmount"); // 金额 final Element taxExclusiveTotalAmountElement = rootElement.element("TaxExclusiveTotalAmount"); // 价税合计 final Element taxInclusiveTotalAmountElement = rootElement.element("TaxInclusiveTotalAmount"); // 收款人 final Element payeeElement = rootElement.element("payee"); // 开票人 final Element invoiceClerkElement = rootElement.element("InvoiceClerk"); // 复核 final Element checkerElement = rootElement.element("Checker"); if (invoiceCodeElement != null) { invoice.setInvoiceCode(invoiceCodeElement.getText()); } if (invoiceNoElement != null) { invoice.setInvoiceNumber(invoiceNoElement.getText()); } if (issueDateElement != null && issueDateElement.getText() != null) { Date date = null; try { date = simpleDateFormat.parse(issueDateElement.getText()); } catch (ParseException e) { e.printStackTrace(); } invoice.setBillingDate(date); } if (invoiceCheckCodeElement != null) { invoice.setCheckCode(invoiceCheckCodeElement.getText()); } if (machineNoElement != null) { invoice.setMachineCode(machineNoElement.getText()); } if (taxControlCodeElement != null) { invoice.setMmq(taxControlCodeElement.getText()); } if (buyerElement != null) { final List buyerName = buyerElement.elements("BuyerName"); final List buyerTaxID = buyerElement.elements("BuyerTaxID"); final List buyerAddrTel = buyerElement.elements("BuyerAddrTel"); final List buyerFinancialAccount = buyerElement.elements("BuyerFinancialAccount"); invoice.setPurchaserName(buyerName.get(0).getText()); invoice.setPurchaserTaxNo(buyerTaxID.get(0).getText()); invoice.setPurchaserAddressPhone(buyerAddrTel.get(0).getText()); invoice.setPurchaserBank(buyerFinancialAccount.get(0).getText()); } if (sellerElement != null) { final List sellerName = sellerElement.elements("SellerName"); final List sellerTaxID = sellerElement.elements("SellerTaxID"); final List sellerAddrTel = sellerElement.elements("SellerAddrTel"); final List sellerFinancialAccount = sellerElement.elements("SellerFinancialAccount"); invoice.setSalesName(sellerName.get(0).getText()); invoice.setSalesTaxNo(sellerTaxID.get(0).getText()); invoice.setSalesAddressPhone(sellerAddrTel.get(0).getText()); invoice.setSalesBank(sellerFinancialAccount.get(0).getText()); } if (taxTotalAmountElement != null) { invoice.setTotalTax(new BigDecimal(taxTotalAmountElement.getText())); } if (taxExclusiveTotalAmountElement != null) { invoice.setTotalAmount(new BigDecimal(taxExclusiveTotalAmountElement.getText())); } if (taxInclusiveTotalAmountElement != null) { invoice.setTotalAmount(new BigDecimal(taxInclusiveTotalAmountElement.getText())); } if (payeeElement != null) { invoice.setSkr(payeeElement.getText()); } if (invoiceClerkElement != null) { invoice.setKpr(invoiceClerkElement.getText()); } if (checkerElement != null) { invoice.setFh(checkerElement.getText()); } return invoice; } /** * 从字节流写到文件中. * * @param is * 流 * @return file */ private static File getFileByBytes(final InputStream is, final String fileName, final String oldFileName) { final String tempPath = System.getProperty("java.io.tmpdir"); final File dirFile = new File(tempPath); if (!dirFile.exists()) { dirFile.mkdirs(); } final File file = new File(dirFile, fileName); // zip文件需临时存放服务器 final File oldFile = new File(dirFile, oldFileName); OutputStream os = null; try { if (!file.exists()) { file.createNewFile(); } else { file.delete(); file.createNewFile(); } if (oldFile.exists()) { file.delete(); } os = new FileOutputStream(file); int len; final byte[] buffer = new byte[4096]; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } return file; } /** * 解压ofd文件. * @param ofdFile ofdFile * @return ofdFile路径 * @throws IOException IOException */ public static String unzip(final String ofdFile) throws IOException { final ZipFile zipFile = new ZipFile(ofdFile); String lastFileName = ""; String temp = ""; // 循环查找文件 final Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry zipEntry = (ZipEntry) entries.nextElement(); if (!zipEntry.isDirectory()) { String fileFullName = zipEntry.getName(); // 若当前文件包含文件夹名称,则直接去文件名称 if (fileFullName.indexOf("/") != -1 && fileFullName.indexOf("original_invoice") > -1) { fileFullName = fileFullName.substring(fileFullName.lastIndexOf("/") + 1); lastFileName = fileFullName; final InputStream in = zipFile.getInputStream(zipEntry); final File xmlfile = getFileByBytes(in, fileFullName, lastFileName); temp = xmlfile.toString(); } } } zipFile.close(); return temp; } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3