惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
S
Schneier on Security
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
U
Unit 42
Hacker News - Newest:
Hacker News - Newest: "LLM"
V2EX - 技术
V2EX - 技术
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
H
Heimdal Security Blog
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Cloudbric
Cloudbric
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
The Cloudflare Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
D
DataBreaches.Net
N
News and Events Feed by Topic
Security Archives - TechRepublic
Security Archives - TechRepublic
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
F
Full Disclosure
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
T
Threatpost
Spread Privacy
Spread Privacy
小众软件
小众软件
AWS News Blog
AWS News Blog
S
Secure Thoughts
S
Security @ Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks

博客园 - iCeSnaker

[转载]两个未公开的ACCESS方法的使用技巧 Microsoft SQL Reporting Services – Running a Report from the Command Line Microsoft AntiSpyware Beta1 发布了,贴几张图片上来 大型组图:微软总部印象 两个桌面主题 [讨论] 制作博客园年刊 用C#实现生成PDF文档 C# Delegate 简介 单元测试的基本方法 小软件项目开发的管理 C#中为DataGrid添加下拉列表框 用C#读取XML文档 什么是需求? 呐喊 -- 希望博客圆一直是世外桃源 在.net中轻松掌握Windows窗体间的数据交互(三) 在.net中轻松掌握Windows窗体间的数据交互(二) 在.net中轻松掌握Windows窗体间的数据交互(一) C# 编码规范和编程好习惯 C#实现的基本算法
将sql server中的数据倒入Excel(c#)
iCeSnaker · 2004-09-01 · via 博客园 - iCeSnaker

出处:http://www.csyh.com
虽然,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;  
}