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

发布于 2024-12-09 18:44:04 字数 6202 浏览 0 评论 0原文

我想要格式化我的 .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";                
            }       

        }

I want format my .doc file, because when I retrive information and save in a .doc format using Open XML SDK, the document with all information is just in one line, and I need some information in other lines, just to format.

How can I do that?

This is my method that build an .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();
            }
        }

And here, I call this method

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

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-12-16 18:44:04

您可以简单地创建一个带有中断对象的运行,并在每次需要分页符时在代码中插入此运行,请参阅下面的示例:

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

You can simply create a run with a break object in it and insert this run in your code everytime a page break is required, please see below for an example:

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));
}
晨光如昨 2024-12-16 18:44:04

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

Create your desired document manually and then reflect it Using Open XML productivity tool. so you can find what you need to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文