亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Open XML SDK 在 Word 表格的空單元格中設置字體大小?

如何使用 Open XML SDK 在 Word 表格的空單元格中設置字體大???

C#
慕田峪7331174 2021-10-31 19:07:42
我在 Word 文件中使用 C# 在 OpenXml 中創建一個表。我使用了這個問題中提到的一些代碼來設置單元格中文本的字體大小。它適用于包含文本的單元格,但空單元格似乎被賦予了正常樣式,并且具有更大的字體大小,這使得行高更大。這是我的示例代碼,其中一行帶有一個單元格,字體大小應為 9:TableRow tr = new TableRow();TableCell tc = new TableCell();Paragraph par = new Paragraph();Run run = new Run();Text txt = new Text("txt");RunProperties runProps = new RunProperties();FontSize fontSize = new Fontsize() { Val = "18" }; // font size 9runProps.Append(fontSize);run.Append(runProps);run.Append(txt);para.Append(run);tc.Append(para);tr.Append(tc);這是結果表的示例。如您所見,中間一排比其他排高。在顯示“txt”的單元格中,字體大小為 9,但在空白單元格中,字體大小為 11。以上代碼用于所有單元格,其中空單元格僅包含文本“”。當我使用 Open XML Tool 查看文件時,我可以看到值為 18 的 RunProperties 存在于所有單元格中,包括空單元格。如何在不顯示任何文本的情況下設置單元格的字體大???
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

我確認你報告的內容。一種解決方法是用空格" "代替“空”字符串,當然,在文本運行中添加“空格保留”。


另一種可能性是創建字符樣式并將其應用于表格單元格。事實證明,這比聽起來要棘手,因為樣式需要兩次應用于包含文本的單元格:一次應用于 ParagraphMarkRunProperties,一次應用于 RunProperties。對于空單元格,樣式只需要應用于 ParagraphMarkRunProperties。


實際上,經過反思,您可以對字體大小使用相同的方法......


我在下面的代碼中包含了這兩種方法。僅用于字體大小的注釋被注釋掉(四行)。


示例代碼假定單行四列表格的第三個單元格沒有內容。只有在有內容時才添加運行和文本信息。


private void btnCreateTable_Click(object sender, EventArgs e)

{   

    string filePath = @"C:\X\TestCreateTAble.docx";

    using (WordprocessingDocument pkg = WordprocessingDocument.Open(filePath, true))

    {

        MainDocumentPart partDoc = pkg.MainDocumentPart;

        Document doc = partDoc.Document;


        StyleDefinitionsPart stylDefPart = partDoc.StyleDefinitionsPart;

        Styles styls = stylDefPart.Styles;

        Style styl = CreateTableCharacterStyle();

        stylDefPart.Styles.AppendChild(styl);


        Table t = new Table();

        TableRow tr = new TableRow();


        for (int i = 1; i <= 4; i++)

        {

            TableCell tc = new TableCell(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "500" }));

            Paragraph para = new Paragraph();

            ParagraphProperties paraProps = new ParagraphProperties();

            ParagraphMarkRunProperties paraRunProps = new ParagraphMarkRunProperties();

            RunStyle runStyl = new RunStyle() { Val = "Table9Point" };

            paraRunProps.Append(runStyl);

            //    FontSize runFont = new FontSize() {Val = "18" };

            //    paraRunProps.Append(runFont);

            paraProps.Append(paraRunProps);

            para.Append(paraProps);


            Run run = new Run();


            Text txt = null;

            if (i == 3)

            {

            }

            else

            {

                txt = new Text("txt");

                txt.Space = SpaceProcessingModeValues.Preserve;

                RunProperties runProps = new RunProperties();

                RunStyle inRunStyl = (RunStyle) runStyl.Clone();

                runProps.Append(inRunStyl);

                //    FontSize inRunFont = (FontSize) runFont.Clone();

                //    runProps.Append(inRunFont);

                run.Append(runProps);

                run.Append(txt);

                para.Append(run);

           }

            tc.Append(para);

            tr.Append(tc);

        }

        t.Append(tr);

        //Insert at end of document

        SectionProperties sectProps = doc.Body.Elements<SectionProperties>().LastOrDefault();

        doc.Body.InsertBefore(t, sectProps);

    }

}


private Style CreateTableCharacterStyle()

{

    Style styl = new Style()

    {

        CustomStyle = true,

        StyleId = "Table9Point",

        Type = StyleValues.Character,

    };

    StyleName stylName = new StyleName() { Val = "Table9Point" };

    styl.AppendChild(stylName);

    StyleRunProperties stylRunProps = new StyleRunProperties();

    stylRunProps.FontSize = new FontSize() { Val = "18" };

    styl.AppendChild(stylRunProps);

    BasedOn basedOn1 = new BasedOn() { Val = "DefaultParagraphFont" };

    styl.AppendChild(basedOn1);

    return styl;

}



查看完整回答
反對 回復 2021-10-31
  • 1 回答
  • 0 關注
  • 493 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號