(Java操作Word--Aspose Word) 替换指定单元内容

		Document srcDoc = new Document(srcDocPath);
        //获取第一个表格
		Table table = (Table)srcDoc.getChild(NodeType.TABLE, 0, true);
		RowCollection rows = table.getRows();
		for (int i = 1; i < rows.getCount(); i++) {
			Row row = rows.get(i);
			Cell cell = row.getCells().get(5);
			if(null != cell ){
					String text = row.getCells().get(3).getText();
					if(null != text && !"".equals(text)){
					//设置新内容
					cell.getFirstParagraph().getRuns().get(0).setText("");
					}
			}
		}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现将指定字符串替换为表格,并删除不需要的页面,可以使用 Aspose.Words for Java 提供的以下代码: ``` Document doc = new Document("input.docx"); // 遍历文档中的所有段落 for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) { // 如果段落中包含指定的字符串,就将该段落替换为表格 if (para.toString(SaveFormat.TEXT).contains("指定字符串")) { // 创建一个新表格 Table table = new Table(doc); table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW); // 添加表格行和单元格 Row row = new Row(doc); Cell cell = new Cell(doc); cell.getCellFormat().setWidth(100); cell.getCellFormat().getShading().setBackgroundPatternColor(Color.LIGHT_GRAY); cell.getParagraphs().add(new Paragraph(doc)); row.getCells().add(cell); table.getRows().add(row); // 替换段落为表格 int index = para.getParentNode().getChildNodes().indexOf(para); para.getParentNode().getChildNodes().insert(index, table); para.remove(); } // 如果段落中不包含指定的字符串,就删除该页面 else if (para.getParentNode() instanceof Section && ((Section) para.getParentNode()).getPageSetup().getSectionStart() == SectionStart.NEW_PAGE) { para.getParentNode().remove(); } } doc.save("output.docx"); ``` 这段代码会遍历文档中的所有段落,如果某个段落中包含指定的字符串,就创建一个新表格,并将该段落替换为该表格;如果某个段落中不包含指定的字符串,就判断该段落所在的页面是否需要保留,如果不需要就删除该页面。最后将处理后的文档保存为输出文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值