将sql server中的数据倒入Excel
2007-07-12 22:00
204 查看
author:ouyang76cn()
虽然,sql server中的DTS也能将数据倒入Excel,但不如使用程序灵活,
本程序主要代码在按钮函数内。可适应于报表开发的读取数据部分:)
我删除了原程序的很多垃圾代码,只留主要起作用的代码
//加入名称空间
using System.Data;
using System.Data.SqlClient;
//定义方法GetData(),返回一个数据表
private System.Data.DataTable GetData()
{
SqlConnection conn= new SqlConnection(@"Server=PXGD2;Initial Catalog=pingxiang;Uid=sa;Pwd=;");
SqlDataAdapter adapter= new SqlDataAdapter("select username 用户名,catalyst_port 占用端口,home_address 住宅地址,ip_address
ip地址,phone 电话,addtime 开通日期 from userinfo where catalyst_port=1 or catalyst_port=' order by ip_address desc",conn);
DataSet ds= new DataSet();
try
{
adapter.Fill(ds,"Customer");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0];
}
//按钮
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
DataTable table=GetData();
//将所得到的表的列名,赋值给单元格
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
//同样方法处理数据
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//不可见,即后台处理
excel.Visible=true;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=654760
虽然,sql server中的DTS也能将数据倒入Excel,但不如使用程序灵活,
本程序主要代码在按钮函数内。可适应于报表开发的读取数据部分:)
我删除了原程序的很多垃圾代码,只留主要起作用的代码
//加入名称空间
using System.Data;
using System.Data.SqlClient;
//定义方法GetData(),返回一个数据表
private System.Data.DataTable GetData()
{
SqlConnection conn= new SqlConnection(@"Server=PXGD2;Initial Catalog=pingxiang;Uid=sa;Pwd=;");
SqlDataAdapter adapter= new SqlDataAdapter("select username 用户名,catalyst_port 占用端口,home_address 住宅地址,ip_address
ip地址,phone 电话,addtime 开通日期 from userinfo where catalyst_port=1 or catalyst_port=' order by ip_address desc",conn);
DataSet ds= new DataSet();
try
{
adapter.Fill(ds,"Customer");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0];
}
//按钮
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
DataTable table=GetData();
//将所得到的表的列名,赋值给单元格
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
//同样方法处理数据
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//不可见,即后台处理
excel.Visible=true;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=654760
相关文章推荐
- 将sql server中的数据倒入Excel
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将SQL SERVER中查询到的数据导成一个Excel文件
- 【转】SQL Server数据表在编程中实现导出EXCEL的几种方法
- Sql Server与Access、Excel之间的数据传输
- 用sql server导入导出功能 把excel导入到sql的表中,提示导入成功,但是数据库表中并没有数据?
- 将SQL Server 2005中的数据导出为Excel
- Excel文件导入到SQL Server——2007 Office system 驱动程序:数据连接组件
- excel数据导入SQL SERVER
- 将数据从EXCEL中导入到SQL SERVER
- Sharepoint2013商务智能学习笔记之Excel Service展示Sql Server数据Demo(五)
- SQL Server与Excel、Access数据互导
- ASP.NET中如何把EXCEL中数据导入到SQL SERVER
- 将Excel中的数据导入到SQL Server 2000数据库中
- 如何将 Excel 数据导入 SQL Server