向SdtBlock中添加Table

本文介绍如何使用C#和WordprocessingDocument库处理Word文档,具体包括选择特定文档、复制文件到指定路径、读取文档内容,并在特定位置插入表格。此过程涉及到文件操作、XML解析和文档编辑等技术。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;

namespace ConsoleApplication15
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "Word Document|*.docx";
            ofd.ShowDialog();
            string usepath =  AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.docx";
            File.Copy(ofd.FileName,usepath,true);
            using (WordprocessingDocument wpd = WordprocessingDocument.Open(usepath, true))
            {
                MainDocumentPart mdp = wpd.MainDocumentPart;
                Document dc = mdp.Document;
                SdtBlock target = null;
                foreach (SdtBlock sb in dc.Descendants<SdtBlock>())
                {
                    Tag tg = sb.Descendants<Tag>().Where(T => T.Val == "TestField").FirstOrDefault();
                    if (tg != null)
                    {
                        target = sb;
                        break;
                    }
                }
                if (target != null)
                {
                    SdtContentBlock scb = target.SdtContentBlock;
                    Table tb = new Table();
                    TableGrid tg = new TableGrid();
                    GridColumn gc = new GridColumn() { Width = "2840" };
                    GridColumn gc1 = new GridColumn() { Width = "2840" };
                    tg.Append(gc);
                    tg.Append(gc1);
                    tb.Append(tg);
                    TableRow tr = new TableRow();
                    TableCell tc = new TableCell();
                    Paragraph p = new Paragraph();
                    TableCell tc1 = new TableCell();
                    Paragraph p1 = new Paragraph();
                    tc.Append(p);
                    tc1.Append(p1);
                    tr.Append(tc);
                    tr.Append(tc1);
                    tb.Append(tr);
                    scb.Append(tb);
                }
                dc.Save();
            }
        }
    }
}


public static void main(String[] args) throws Exception { // 1. 创建文档 XWPFDocument doc = new XWPFDocument(); // 2. 创建表格,3 行 3 列 XWPFTable table2 = doc.createTable(3, 3); // 3. 设置每个单元格文字(通过段落 + Run) // 第一行 setCellText(table2.getRow(0).getCell(0), "课程"); setCellText(table2.getRow(0).getCell(1), "成绩"); setCellText(table2.getRow(0).getCell(2), "学分"); // 第二行 setCellText(table2.getRow(1).getCell(0), "数学"); setCellText(table2.getRow(1).getCell(1), "95"); setCellText(table2.getRow(1).getCell(2), "4"); // 第三行 setCellText(table2.getRow(2).getCell(0), "英语"); setCellText(table2.getRow(2).getCell(1), "88"); setCellText(table2.getRow(2).getCell(2), "3"); // ===== 第一个内容控件 ===== CTSdtBlock sdtBlock1 = doc.getDocument().getBody().addNewSdt(); CTSdtPr sdtPr1 = sdtBlock1.addNewSdtPr(); sdtPr1.addNewTag().setVal("tableControl1"); CTSdtContentBlock sdtContent1 = sdtBlock1.addNewSdtContent(); sdtContent1.addNewTbl().set(table2.getCTTbl()); int pos1 = doc.getPosOfTable(table2); if (pos1 >= 0) { doc.removeBodyElement(pos1); } long l = System.currentTimeMillis(); // 4. 保存文档 try (FileOutputStream out = new FileOutputStream(l+".docx")) { doc.write(out); } doc.close(); System.out.println(l); } /** * 使用段落 + Run 设置单元格文字 */ private static void setCellText(XWPFTableCell cell, String text) { // 删除默认段落 cell.removeParagraph(0); // 新增段落 XWPFParagraph para = cell.addParagraph(); // 添加书签起点 CTBookmark bookmarkStart = para.getCTP().addNewBookmarkStart(); bookmarkStart.setName("标签名字"+count++); bookmarkStart.setId(null); // 添加书签终点 CTMarkupRange bookmarkEnd = para.getCTP().addNewBookmarkEnd(); bookmarkEnd.setId(null); // 新增 Run 并设置文字 XWPFRun run = para.createRun(); run.setText(text); } 改造这个代码放到这个控件里面 CTTbl ctTbl = table2.getCTTbl(); CTSdtRow ctSdtRow = ctTbl.addNewSdt();
09-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值