xml文件转真正的docx文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot中,要将HTML文件转换docx格式,可以使用Apache POI库来实现。以下是一个简单的示例: 1. 首先,确保在你的Maven或Gradle配置文件中添加了Apache POI依赖: ```xml <!-- Maven --> <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> </dependencies> ``` 2. 创建一个Controller类,用于接收HTML文件并将其转换docx: ```java import org.apache.poi.xwpf.usermodel.*; import org.springframework.stereotype.Controller; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @Controller public class HtmlToDocxController { @PostMapping("/convert") @ResponseBody public String convertHtmlToDocx(@RequestBody String html) { try { // 创建docx文档对象 XWPFDocument document = new XWPFDocument(); // 创建段落对象 XWPFParagraph paragraph = document.createParagraph(); // 创建文本对象 XWPFRun run = paragraph.createRun(); // 设置HTML内容 run.setText(html); // 保存为docx文件 File file = ResourceUtils.getFile("classpath:result.docx"); FileOutputStream out = new FileOutputStream(file); document.write(out); out.close(); return "转换成功!"; } catch (IOException e) { e.printStackTrace(); return "转换失败!"; } } } ``` 3. 在Spring Boot应用中运行该Controller,可以使用POST请求将HTML内容作为请求体发送到`/convert`路径。转换后的docx文件将保存在项目的`classpath`目录下的`result.docx`文件中。 请注意,该示例仅演示了将HTML内容转换docx文件的基本过程。你可能需要根据实际需求进行更多的处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值