将sql server中的数据倒入Excel(c#)
来源:互联网 发布:勒索软件特点 编辑:程序博客网 时间:2024/05/16 13:22
本程序主要代码在按钮函数内。可适应于报表开发的读取数据部分:)
//加入名称空间
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
- 将sql server中的数据倒入Excel
- 将sql数据倒入excel表格
- 将sqlreader中的数据倒入excel中
- 将ORACLE数据库的数据倒入到EXCEL中的方法
- 将Oracle的数据倒入EXCEL中的方法
- 将Oracle的数据倒入EXCEL中的方法
- 将Oracle的数据倒入EXCEL中的方法
- DataGrid中的数据倒入Excel
- EXCEL:将SQL SERVER中的数据导入到 EXCEL
- vs2005中Excel 数据倒入SQL数据库
- VB.NET将EXCEL中的数据导入到SQL SERVER
- 利用ultra edit将excel中的数据导入SQL Server
- 将SQL Server 2005中的数据导出为Excel
- C# 操作 Excel
- dell D610 笔记本购买记
- hibernate+spring
- 跑步了
- 将Asp.Net页面输出到EXCEL里去
- 将sql server中的数据倒入Excel(c#)
- ASP.NET中数据库数据导入Excel并打印
- C++ young 程序库——y_binary_search_tree_base.hpp、y_red_black_tree_base.hpp 和 y_red_black_tree.hpp
- Quick File Viewer V1.1
- C++ young 程序库——y_hash_function.hpp、y_hash_table_base.hpp 和 y_hash_table.hpp
- 使用TC调试c语言的时候运行时出现“unable to open input file 'cos.obj'
- C++ young 程序库——y_algorithm.hpp
- eclipse 安装
- C++ young 程序库——y_functional.hpp