1

我正在做一个项目,其中涉及创建数据表。我已经设法通过遵循 MSDN 提供的代码在 docx 文件中创建表格,但是当我尝试将样式(例如,边框设置为无、阴影、单元格宽度等)应用于我创建的表格时,它不会附加在上面.

所以我假设我必须使用table.RemoveAllChildren然后附加新的style. 但如果我这样做,整个表将被删除,因此我必须重新创建表。必须有更好的方法来做到这一点。请指教。

以下是 iIhave 的示例代码,带有 1 个单元格表。深蓝色阴影,我正在尝试删除表格边框,设置单元格宽度并修改文本。

  public void docHeaderTableVal(string FileLocation, string replacingVal, int tableNum, int rowNum, int cellNum) //replace a value
    {
        try
        {
            using (var doc = WordprocessingDocument.Open(FileLocation, true))
            {
                // Find the first table in the document.
                Table table =
                    doc.MainDocumentPart.Document.Body.Elements<Table>().ElementAt(tableNum);

                 TableProperties props = new TableProperties(
                    new TableBorders(
                        new TopBorder
                        {
                            Val = new EnumValue<BorderValues>(BorderValues.None),
                            //Size = 10
                        },
                        new BottomBorder
                        {
                            Val = new EnumValue<BorderValues>(BorderValues.None),
                            //Size = 10
                        },
                        new LeftBorder
                        {
                            Val = new EnumValue<BorderValues>(BorderValues.None),
                            //Size = 10
                        },
                        new RightBorder
                        {
                            Val = new EnumValue<BorderValues>(BorderValues.None),
                            //Size = 10
                        },
                        new InsideHorizontalBorder
                        {
                            Val = new EnumValue<BorderValues>(BorderValues.None),
                            //Size = 10
                        },
                        new InsideVerticalBorder
                        {
                            Val = new EnumValue<BorderValues>(BorderValues.None),
                            //Size = 10

                        }
                        )
                    );

                 table.AppendChild<TableProperties>(props);

                TableRow row = table.Elements<TableRow>().ElementAt(rowNum);


                TableCell cell = row.Elements<TableCell>().ElementAt(cellNum);

                cell.RemoveAllChildren();

                //cell.TableCellProperties.TableCellWidth.Width = "2500";



                //finding first run and its first text then replace with passed-in text.
                Paragraph p = new Paragraph() { RsidParagraphAddition = "004F7104", RsidParagraphProperties = "008F2986", RsidRunAdditionDefault = "004F7104" };
                ParagraphProperties pPr = new ParagraphProperties();
                Justification just1 = new Justification() { Val = JustificationValues.Center };
                pPr.Append(just1);
                Run r = new Run();
                RunProperties rProp = new RunProperties(); //making an instance of run property
                RunFonts rFont = new RunFonts();
                FontSize size = new FontSize();
                Color color = new Color() { Val = "#FFFFFF"};
                Bold bld = new Bold();
                Italic italic = new Italic();
                rFont.Ascii = "Arial Black";

                size.Val = new StringValue("32");
                rProp.Append(rFont, size, color, bld, italic);
                r.PrependChild<RunProperties>(rProp);


                Text t = new Text(replacingVal);
                r.Append(t);
                p.Append(pPr);
                p.Append(r);


                //back ground shading for the title cell;

                cell.AppendChild(new TableCellProperties(new Shading { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "##17365d" }));

                cell.Append(p);


            }
        }
        catch
        {
            MessageBox.Show("Can not replace table value due to file open error.");
        }

}
4

0 回答 0