yamilinux 2015-06-01 13:02 采纳率: 0%
浏览 2675

OpenXml 单元格合并后Word打开提示错误

string path = @"D:\newdoc.docx" ;
WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document)
MainDocumentPart mainpart = doc.AddMainDocumentPart();
mainpart.Document = new Document();
Body body = mainpart.Document.AppendChild(new Body());

                Table t = new Table();
                TableProperties tpr = new TableProperties(new TableWidth(){Width="0",Type=TableWidthUnitValues.Auto});
                t.Append(tpr);
                TableGrid tg = new TableGrid(new GridColumn(), new GridColumn());
                t.Append(tg);

                TableRow tr1 = new TableRow();
                TableCell r1tc1 = new TableCell();
                TableCell r1tc2 = new TableCell();
                tr1.Append(r1tc1);
                tr1.Append(r1tc2);

                TableRow tr2 = new TableRow();
                TableCell r2tc1 = new TableCell();
                TableCell r2tc2 = new TableCell();
                tr2.Append(r2tc1);
                tr2.Append(r2tc2);

                TableRow tr3 = new TableRow();
                TableCell r3tc1 = new TableCell(new TableCellProperties(new VerticalMerge(){Val=MergedCellValues.Restart}));

                TableCell r3tc2 = new TableCell();
                tr3.Append(r3tc1);
                tr3.Append(r3tc2);

                TableRow tr4 = new TableRow();
                TableCell r4tc1 = new TableCell(new TableCellProperties(new VerticalMerge() { Val = MergedCellValues.Continue }));
                TableCell r4tc2 = new TableCell();
                tr4.Append(r4tc1);
                tr4.Append(r4tc2);

                t.Append(tr1);
                t.Append(tr2);
                t.Append(tr3);
                t.Append(tr4);

                body.Append(t);



                                    代码生成word文档的表格后,用Word打开提示错误:遇到不明确的单元格匹配。可能丢失段落元素。<p> 元素必须位于</tc>元素之前


                                    谁能指点指点,非常感谢。
  • 写回答

3条回答

  • edouardzyc 2015-06-02 03:10
    关注

    看上去没错, 你最后mainpart.Document.save()了吗
    要么你换种写法试试
    TableCellProperties tableCellProperties = new TableCellProperties();
    HorizontalMerge verticalMerge = new HorizontalMerge()
    {
    Val = MergedCellValues.Restart
    };
    tableCellProperties.Append(verticalMerge);

    TableCell r3tc1 = new TableCell();
    r3tc1.Append(tableCellProperties);

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的