c#中将数据库数据导出到EXCEL中
来源:互联网 发布:津巴布韦通货膨胀知乎 编辑:程序博客网 时间:2024/04/30 09:20
我分以下几步进行介绍: 1,新建一个C#应用程序,在对话框上放置一个按钮,Name=buttonOutput,Text=Output,用这个按钮激发导出程序;
2,添加对“Microsoft Excel 9.0 Object Library”的引用,根据自己计算机上安装Office版本的来确定Library的版本;
3,在代码中加入引用:
using Excel;
using System.Reflection;
4,在buttonOutput按钮单机事件中加入以下代码:
string strSql;
int col = 0, row = 2;
//定义数据变量
private OleDbConnection conn;
private OleDbCommand comm;
private OleDbDataReader reader;
//连接到数据库
string strConnectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb";
conn = new OleDbConnection(strConnectionstring);
conn.Open();
comm = conn.CreateCommand();
//建立Excel应用对象
Excel.Application xlapp = new Excel.ApplicationClass();
xlapp.Workbooks.Add(true );
xlapp.Visible = false ;
strSql = "select * from INFO";
comm.CommandText = strSql;
reader=comm.ExecuteReader();
if (reader.HasRows)
{
for (col = 0; col < reader.FieldCount; col++)
{
xlapp.Cells[1, col + 1] = reader.GetName(col);
}
while (reader.Read())
{
for (col = 0; col < reader.FieldCount; col++)
{
xlapp.Cells[row, col + 1] = reader.GetValue(col).ToString();
}
row++;
}
}
xlapp.Visible = true;
xlapp.Worksheets.PrintPreview(true);
//关闭数据库纪录集
reader.Close();
comm.Close();
- c#中将数据库数据导出到EXCEL中
- C#中将ListView中数据导出到Excel代码
- 如何将C#中将TextBox的数据以及从数据库中查询得到的数据导出到Excel中
- C#中将数据导到excel中
- java中将数据和图片导出到Excel文件中
- vb中将数据导出到excel表格中
- Java中将JTable中的数据导出到Excel中
- VC中将数据导出到Excel表格中
- C#中将DataGridView中数据导出至Excel
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- Sql2008R2中将数据库数据导出到sql文件中
- Java中将数据导出到Excel
- C#中将ListView数据导出为excel
- C#中将控件数据导出Excel
- 事务和锁(一)
- 使用lua编写Wireshark(Ethereal)的dissector插件
- 隔离存贮(Isolated Storage)
- 事务和锁(二)
- 我用c语言写了一个关于商人过河的问题
- c#中将数据库数据导出到EXCEL中
- 在Silverlight 2中创建密码输入框
- 事务和锁(三)
- MFC的 CString 学习笔记 -1
- 第二人生的源码分析(四十七)发送下载纹理图片请求
- 事务和锁(四)
- MFC的 CString 学习笔记 -2
- Reporting Services 2005 for the DBA – Connectivity Issues
- 事务和锁(五)