将sql server中的数据倒入Excel(c#)
2011-08-17 08:51
176 查看
虽然,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;
}
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;
}
相关文章推荐
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- C#语言怎么实现从SQL SERVER导出数据到EXCEL
- c#读取excel中的数据到sql server,包括c#读取excel的数据
- C#源码读取excel数据到程序中-SQL SERVER-到dataset中
- 将sql server中的数据倒入Excel
- C# 连接sql数据库对 access,excel导入导出操作 一、SQL SERVER 和ACCESS的数据导入导出
- 将sql server中的数据倒入Excel
- C#读取excel数据到SQL server
- C#导出GridView的数据到Excel以及Excel导入SQL Server
- C# 读取EXCEL中的数据并存入SQL Server 2005数据库
- C#将Excel数据导入数据库(MySQL或Sql Server)
- C#把sql server里是数据导出到excel
- C#源码读取excel数据到程序中-SQL SERVER-到dataset中
- C#实现将Excel的数据复制到sql server
- C#把sql server里是数据导出到excel
- C#语言怎么实现从SQL SERVER导出数据到EXCEL