简体   繁体   English

数据检索与下载

[英]Data retrieval and download

How to download the data which is displayed on windows forms that is retrieved from the database in the form of columns and rows? 如何下载Windows窗体上显示的数据,这些窗体是从数据库中以行和列的形式检索的? I want to download the data in a Word format. 我想以Word格式下载数据。 Can anyone help with this, please? 有人可以帮忙吗? This is the code which I used to retrieve the data 这是我用来检索数据的代码

private void button6_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();

    con.ConnectionString = "Data Source=MYPC\\MSSQLSERVERNAVEN;Initial Catalog=Test;Integrated Security=True;";

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "select * from login_details1";

    con.Open();

    SqlDataReader dr = cmd.ExecuteReader();

    BindingSource bg = new BindingSource();
    bg.DataSource = dr;
    dataGridView1.DataSource = bg;

    con.Close();
}

So what is the code to download the data in doc format? 那么,以doc格式下载数据的代码是什么?

To get .doc you'd have to run the app interactively as the user and use interop. 要获取.doc,您必须以用户身份交互式运行该应用程序并使用互操作。

What you can do easily, though, is use OpenXml SDK: http://www.microsoft.com/en-us/download/confirmation.aspx?id=5124 不过,您可以轻松地使用OpenXml SDK: http : //www.microsoft.com/zh-cn/download/confirmation.aspx?id= 5124

Then you can do: 然后,您可以执行以下操作:

using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(@"C:\folder\file.docx", WordprocessingDocumentType.Document))
        {
            var p = wordDocument.AddMainDocumentPart();
            p.Document = new Document();
            Body body = p.Document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text("Hello, world!"));
        }

To find out how to do tables follow this: http://msdn.microsoft.com/en-us/library/office/cc850841(v=office.14).aspx 若要查找如何做表,请按照以下步骤操作: http : //msdn.microsoft.com/zh-cn/library/office/cc850841(v=office.14).aspx

Please try the following . 请尝试以下方法。 which creates the Datagridview data in in the form of Table in the word document. 它在Word文档中以Table的形式创建Datagridview数据。

It relies on the Interoplibrary ' Microsoft.Office.Interop.Word ' 它依赖于互用的“ Microsoft.Office.Interop.Word”

Either you can use datagridview or preferbaly use the data source it self to create the Table in word 您可以使用datagridview或首选使用其自身的数据源来创建Word表

Check this link 检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM