xml文件转真正的docx文件

文章描述了如何使用Java编程语言,通过Aspose.Words库将`.doc`文件转换为`.docx`格式,并且移除转换后文档中的Aspose.Words水印。代码示例包括文件路径处理、文档操作和异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void main(String[] args) {
		String tempUrl = "F:\\SJSAO\\code\\defaultroot\\xtbginforeport\\downloadFileWork\\2022年12月至2023年11月份信息上报统计.doc"; //这个是你文件的地址,tepFile是要转换的文件(xml形式的word文件)
		try {
			String transDocxName = tempUrl.replace(".doc", ".docx");//将后缀名字替换也是为了在原有的路径上生成新的文件,到后面一定要删除掉这个文件!不然会积很多文件占用内存
			Document doc = new Document(tempUrl);// 原始word路径
			File wordFile = new File(transDocxName);// 你要的是这个文件********你拿到这个文件就可以了。
			FileOutputStream fileOS = new FileOutputStream(wordFile );
			doc.save(fileOS, SaveFormat.DOCX);//这里执行操作
			//其实aspose.words 是付费的,会产生前后两个水印,这个时候需要调用这个方法将水印清除
			removeAsposeMark(transDocxName);//
			fileOS.close();
			wordFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 /**
     * 使用Aspose.Words会出现前后水印可以通过这个方法去除水印
     * @param docFilePath
     */
	private static void removeAsposeMark(String docFilePath) {
		try (FileInputStream in = new FileInputStream(docFilePath)) {
			XWPFDocument doc = new XWPFDocument(OPCPackage.open(in));
			List<XWPFParagraph> paragraphs = doc.getParagraphs();
			if (paragraphs.size() < 1) {
				return;
			}
			XWPFParagraph firstParagraph = paragraphs.get(0);
			XWPFParagraph lastParagraph = paragraphs.get(paragraphs.size() - 1);
			if (firstParagraph.getText().contains("Aspose.Words")) {
				doc.removeBodyElement(doc.getPosOfParagraph(firstParagraph));
				doc.removeBodyElement(doc.getPosOfParagraph(lastParagraph));
			}
			OutputStream out = new FileOutputStream(docFilePath);
			doc.write(out);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值