前两天一个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)
{
}
}
}