导出从C#的数据word文档 [英] Exporting data from c# to word document

查看:318
本文介绍了导出从C#的数据word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在装满数据c#的网格。其中在网格中的列包含字母(后面数字)字母顺序排序,像这样的:

I have a grid in c# filled with data. One of the columns in that grid contains letters (followed by numbers) sorted alphabetically, like this:

A124
A256
A756
B463
B978
D322
etc.

我需要这个数据的word文档(.doc或.DOCX格式)的出口。
这是我做导出一个劲儿地格:

I need to export this data in a word document (.doc or .docx format). This is what i did to export a signle grid:

var dt = FuntionThatReturnsDatatables();

var gd = new GridView
        {
            DataSource = dt                     
        };

        gd.DataBind();

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;

        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;
        filename=" + "List" + DateTime.Now.ToString("dd.MM.yyyy") + ".doc");

        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-word";
        HttpContext.Current.Response.Charset = "UTF-8";

      HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble()); 

        var oStringWriter = new StringWriter();
        var oHtmlTextWriter = new HtmlTextWriter(oStringWriter);

        gd.RenderControl(oHtmlTextWriter);
        HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();

但现在我必须遵循这样的逻辑:
  - 对于来自电网的每一封信与标题的新表应该像这样创建:

But now I have to follow this logic: - For every letter from grid a new table with title should be created like this:

Table A:
 A124
 A256
 A756


  • 每个新的表格应该从一个新的页面开始,像这样的:

  • Each new table should start from a new page, like this:

表A:
    A124,
    A256,
    A756,
    //新页

Table A: A124, A256, A756, //new page

表B:
B463,
B978,
//新页

Table B: B463, B978, //new page

表D:
D322,
等等。

Table D: D322, etc.

这是Word文档中的页面需要进行编号。

Pages in that word document need to be numbered.

有没有办法写在C#code要做到这一点还是有一些库/插件,可以完成这个任务?

Is there any way to write a code in c# to do this or is there some library/plugin that can accomplish this task ?

一些例子是AP preciated。

Some examples would be appreciated.

推荐答案

您应该能够使用OpenXML的SDK从微软实现这一目标。

You should be able to use OpenXML SDK from Microsoft to achieve this.

参考:<一href=\"http://msdn.microsoft.com/en-us/library/bb448854.aspx\">http://msdn.microsoft.com/en-us/library/bb448854.aspx

对照品:


  1. http://blogs.msdn.com/b/acoat/archive/2010/06/19/document-creation-and-conversion-with-the-openxml-sdk-and-sharepoint-2010-word-automation-services.aspx

  2. http://blogs.msdn.com/b/acoat/archive/2011/04/06/document-creation-and-conversion-with-the-openxml-sdk-and-sharepoint-2010-word-automation-services-part-2.aspx

这篇关于导出从C#的数据word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆