| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1937 人关注过本帖, 4 人收藏
标题:原创:数据导出到Excel
只看楼主 加入收藏
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏(4)
 问题点数:0 回复次数:7 
原创:数据导出到Excel

前两天一个MM问的问题,昨晚忙了好一哈哈儿,才搞定的。先要

在解决方案里,点右键叫如Micorsoft excel .labrary 什么的记

不清了,反正就是那的.然后加如引用就OK啦!下面是一段

SQLSERVER导出到EXCEL的代码:希望能给一些需要帮助的朋友一

些启示:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data .SqlClient ;
//using Excel;


namespace 导出数据
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container

components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任

何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing

)
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修


/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new

System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new

System.Drawing.Point(56, 128);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size

(88, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new

System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new

System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size

(232, 166);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender,

System.EventArgs e)
{
Excel.Application excel=new Excel

.ApplicationClass ();
excel.Application .Workbooks .Add (true);
excel.Visible =true;
SqlConnection con=new SqlConnection ();

con.ConnectionString ="workstation

id=localhost;integrated security=SSPI;database=yun";
SqlCommand comd=new SqlCommand();
comd.CommandText ="select * from TABLE1";
comd.Connection =con;
con.Open ();
SqlDataReader read;
read=comd.ExecuteReader ();
int row=2,col;
for(col=0;col<read.FieldCount ;col++)
{
// excel.cells[1,col+1]=read.GetName

(col);
excel.Cells[ 1 , col+1 ] = read.GetName

(col);
}
//得到标题
while (read.Read ())
   {
   for(col =0;col<read.FieldCount;col++)
   excel.Cells [row,col+1]=read.GetValue

(col).ToString();
   row++;
   }
//取得数据

excel.Visible = true ;
//显示Excel内容
}


}
}
================================================================================

下面才是那个MM问我的问题:有兴趣的可以看哈的.因为本人没有U盘,代码是在记事本里打的,要是那里有错自己改一下,就可以拉.主程序没有问题的,我测试过了,呵呵!
using system;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
//using Excel;

namespace output_excel
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private system.windows.forms.listbox listbox1;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container

components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任

何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing

)
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修


/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new

System.Windows.Forms.Button();
this.openFileDialog1 = new

System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();

//
// button1
//
this.button1.Location = new

System.Drawing.Point(80, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size

(72, 24);
this.button1.TabIndex = 0;
this.button1.Text = "打开";
this.button1.Click += new

System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new

System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size

(248, 150);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler

(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender,System.EventArgs e)
{
Excel.Application excel=new Excel.ApplicationClass ();
excel.Workbooks .Add (true);
FileStream MyStream;
string s,filename;
int row=1,col=1;
filename=this.openFileDialog1 .FileName ;
if(this.openfiledialog1.showdialog()==showdialogresult.ok)
{
MyStream=new FileStream(filename,FileMode.Open );
StreamReader read;
read=new StreamReader (MyStream);

while(read.peek()!=-1)

{
s=read.ReadLine ();
this.listbox.item.add(s);
   excel.Cells [row++,col]=s;
  

}
excel.Visible =true;
}
else
{
excel.visible=false;
//显示Excel的内容
}
}

private void Form1_Load(object sender,

System.EventArgs e)
{

}
}
}

搜索更多相关主题的帖子: using Excel System 数据 Data 
2007-07-26 17:27
卡卡艾
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:672
专家分:0
注 册:2007-4-3
收藏
得分:0 
不错.先收下了,以后肯定会用到..好多地方都有用到导入导出功能..谢咯.

革命尚未成功,同志仍需努力-----+++
2007-07-26 21:46
didixiong
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-7-18
收藏
得分:0 
存了,不过小弟过学习,能提供原文件下载就更好了。谢谢。
2007-08-05 14:42
wzerolai
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-5-14
收藏
得分:0 
呵呵.那么多.慢慢看,
谢谢楼主
2007-08-06 01:50
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

昏,我那代码都贴出来了额!你改一下数据库就可以用了啊


真的想象风一样去流浪!
2007-08-07 18:41
sonnzy
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-8-2
收藏
得分:0 
效率太低了
2007-08-31 10:25
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 
我只是用的数字完成的测试,至于文本的,编码方式没有整清楚的  还在研究

真的想象风一样去流浪!
2007-09-04 12:13
战争学院灬骷
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-6-14
收藏
得分:0 
收藏
2013-06-14 21:27
快速回复:原创:数据导出到Excel
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023640 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved