数据检索和下载
本文关键字:下载 检索 数据 | 更新日期: 2023-09-27 18:24:40
如何下载显示在windows窗体上的数据,这些数据以列和行的形式从数据库中检索?我想下载Word格式的数据。有人能帮忙吗?这是我用来检索数据的代码
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();
}
那么,以文档格式下载数据的代码是什么呢?
要获得.doc,您必须以用户身份交互运行应用程序并使用interop。
不过,您可以轻松地使用OpenXml SDK:http://www.microsoft.com/en-us/download/confirmation.aspx?id=5124
然后你可以做:
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!"));
}
要了解如何制作表格,请执行以下操作:http://msdn.microsoft.com/en-us/library/office/cc850841(v=office.14).aspx
请尝试以下操作。它在word文档中以表的形式创建Datagridview数据。
它依赖于Interoplibrary"Microsoft.Office.Interop.Word"
您可以使用datagridview,也可以使用自己的数据源创建单词中的表
检查此链接