C# 如何使用开放的XMLSDK类插入空行

C# 如何使用开放的XMLSDK类插入空行,c#,interop,openxml,openxml-sdk,C#,Interop,Openxml,Openxml Sdk,我想格式化我的.doc文件,因为当我使用OpenXMLSDK检索信息并以.doc格式保存时,包含所有信息的文档只在一行中,我需要在其他行中的一些信息,以便格式化 我该怎么做 这是我构建.doc的方法 private static void BuildDocument(string fileName, string id, string conteudo) { Utilidade.QuebraToken tk2 = new Utilidade.QuebraT

我想格式化我的.doc文件,因为当我使用OpenXMLSDK检索信息并以.doc格式保存时,包含所有信息的文档只在一行中,我需要在其他行中的一些信息,以便格式化

我该怎么做

这是我构建.doc的方法

private static void BuildDocument(string fileName, string id, string conteudo)
        {
            Utilidade.QuebraToken tk2 = new Utilidade.QuebraToken();

            ////id = id.Remove(id.Length - 1);

            string select3 = "SELECT San_Imovel.TextoAnuncio, San_Imovel.Filial_Id, San_Imovel.NomeBairro,San_Imovel.TipoDsc1,San_Imovel.Imovel_Id,San_Filial.NomeFantasia ,San_ContatoFilial.Contato   " +
                                                "FROM San_Imovel    " +
                                                "JOIN San_Filial ON San_Imovel.Filial_Id = San_Filial.Filial_id " +
                                                "JOIN San_ContatoFilial ON San_Filial.Filial_Id = San_ContatoFilial.Filial_Id " +
                                                "WHERE Imovel_Id IN (" + id + ") " +
                                                " AND San_ContatoFilial.TipoContatoFilial_Id = 1";    

            using (WordprocessingDocument w = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
            {
                MainDocumentPart mp = w.AddMainDocumentPart();
                DocumentFormat.OpenXml.Wordprocessing.Document d = new DocumentFormat.OpenXml.Wordprocessing.Document();
                Body b = new Body();
                DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                Run r = new Run();
                Text t = new Text();
                t.Text = conteudo;
                r.Append(t);
                p.Append(r);
                b.Append(p);
                HeaderPart hp = mp.AddNewPart<HeaderPart>();
                string headerRelationshipID = mp.GetIdOfPart(hp);
                SectionProperties sectPr = new SectionProperties();
                HeaderReference headerReference = new HeaderReference();
                headerReference.Id = headerRelationshipID;
                headerReference.Type = HeaderFooterValues.Default;
                sectPr.Append(headerReference);
                b.Append(sectPr);
                d.Append(b);
                hp.Header = BuildHeader(hp, "Anuncio");
                hp.Header.Save();
                mp.Document = d;
                mp.Document.Save();
                w.Close();
            }
        }

您只需创建一个包含break对象的运行,并在每次需要分页符时在代码中插入此运行,请参见下面的示例:

using (WordprocessingDocument package = WordprocessingDocument.Create("D:\\LineBreaks.docx", WordprocessingDocumentType.Document))
{
    package.AddMainDocumentPart();

    Run run1 = new Run();
    Text text1 = new Text("The quick brown fox");
    run1.Append(text1);

    Run lineBreak = new Run(new Break());

    Run run2 = new Run();
    Text text2 = new Text("jumps over a lazy dog");
    run2.Append(text2);

    Paragraph paragraph = new Paragraph();
    paragraph.Append(new OpenXmlElement[] { run1, lineBreak, run2 });

    Body body = new Body();
    body.Append(paragraph);

    package.MainDocumentPart.Document = new Document(new Body(body));
}

您只需创建一个包含break对象的运行,并在每次需要分页符时在代码中插入此运行,请参见下面的示例:

using (WordprocessingDocument package = WordprocessingDocument.Create("D:\\LineBreaks.docx", WordprocessingDocumentType.Document))
{
    package.AddMainDocumentPart();

    Run run1 = new Run();
    Text text1 = new Text("The quick brown fox");
    run1.Append(text1);

    Run lineBreak = new Run(new Break());

    Run run2 = new Run();
    Text text2 = new Text("jumps over a lazy dog");
    run2.Append(text2);

    Paragraph paragraph = new Paragraph();
    paragraph.Append(new OpenXmlElement[] { run1, lineBreak, run2 });

    Body body = new Body();
    body.Append(paragraph);

    package.MainDocumentPart.Document = new Document(new Body(body));
}

手动创建所需的文档,然后使用开放式XML生产力工具反映它。因此,您可以找到需要执行的操作。

手动创建所需的文档,然后使用开放式XML生产力工具反映它。因此,您可以找到需要执行的操作。

Text t=new Text();t、 文本=上下文;r、 追加(新的CarriegeReturn());r、 附加(t);Text t=新文本();t、 文本=上下文;r、 追加(新的CarriegeReturn());r、 附加(t);我也有类似的需求,只是想指出,您可以创建一个Run变量,并将尽可能多的Text和Break元素附加到此Run变量,然后再附加(runVar)。您不需要创建run1和run2。不过,您的回答帮助了我,谢谢。我也有类似的需求,只是想指出,您可以创建一个运行变量,并将尽可能多的文本和中断元素附加到此运行变量,然后再附加(runVar)。您不需要创建run1和run2。你的回答帮助了我,谢谢。