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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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