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

推荐订阅源

B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
H
Help Net Security
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Full Disclosure
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
T
Tenable Blog
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
B
Blog
V
V2EX
Jina AI
Jina AI
L
LangChain Blog
月光博客
月光博客
W
WeLiveSecurity
U
Unit 42
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
V
Visual Studio Blog
A
Arctic Wolf
T
Tailwind CSS Blog
The Cloudflare Blog
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
www.infosecurity-magazine.com
www.infosecurity-magazine.com
腾讯CDC
雷峰网
雷峰网

博客园 - 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;  
}