如何使用Open XML SDK类插入空白行?

本文关键字:插入 空白 SDK 何使用 Open XML | 更新日期: 2023-09-27 18:11:20

我想格式化我的.doc文件,因为当我使用Open XML SDK检索信息并以.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();
            }
        }

这里,我调用这个方法

public static object GerarDoc(string id, string email, string veiculo)
        {
            try
            {
            id = id.Remove(id.Length - 1);
            string conteudo = string.Empty;
            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                                                                                                                                                                                                                                                                                                                                                                                " +
                                                "INNER JOIN San_Filial ON San_Imovel.Filial_Id = San_Filial.Filial_id                                                                                                                                                                                                                                                                       " +
                                                "INNER JOIN San_ContatoFilial ON San_Filial.Filial_Id = San_ContatoFilial.Filial_Id                                                                                                                                                                                                                                                         " +
                                                "WHERE Imovel_Id IN (" + id + ")                                                                                                                                                                                                                                                                                                                                                                " +
                                                "AND San_ContatoFilial.TipoContatoFilial_Id = 1                                                                                                                                                                                                                                                                                                                             ";            
            Utilidade.Conexao c3 = new Utilidade.Conexao();
            SqlConnection con3 = new SqlConnection(c3.Con);
            SqlCommand cmd3 = new SqlCommand(select3, con3);
            con3.Open();
            SqlDataReader r3 = cmd3.ExecuteReader();
            while (r3.Read())
            {
                conteudo = conteudo + "" + (r3["Contato"]) + "";                
                conteudo = conteudo + "'n"+ (r3["NomeBairro"]);
                conteudo = conteudo + "'n" + (r3["TipoDsc1"]) ;
                conteudo = conteudo + "'n" + (r3["NomeFantasia"]) + " (" + (r3["Imovel_Id"]) + ") " + (r3["TextoAnuncio"]) + "'n'n";
            }            
            con3.Close();
                Random rnd = new Random (DateTime.Now.Millisecond);
                string NomeArquivo = "Anuncio_" + Credenciada + "_" + Usuario + "_" + rnd.Next().ToString();                
                rng.Font.Name = "Arial";                    
                rng.Text = conteudo;
                BuildDocument(@"C:'inetpub'wwwroot'galileu.redenetimoveis.com'Anuncios'" + NomeArquivo + ".doc", id, rng.Text); 
                retorno = "1";                
            }       
        }

如何使用Open XML SDK类插入空白行?

您可以简单地创建一个带有换行符对象的运行,并在每次需要换行符时将该运行插入到代码中,请参见下面的示例:

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));
}

手动创建所需的文档,然后使用Open XML生产力工具反映它。所以你可以找到你需要做的。