用户名:  密码:
兄弟在线   

标题:使用C#将数据导出到Excel

作者:佚名 来源:网络 时间:2011-09-16

原帖及讨论:http://bbs.bccn.net/thread-158359-1-1.html

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

 



编辑:admin 总点击 [1417]   评论  0 查看评论
上一篇:MD5加密简介以及C#下的操作实现
下一篇:C#如何注册系统热键
【关闭窗口】
您可能感兴趣的文章
我要评论
          
评论标题:   可以输入250
 
验证数字: 3 + 2 =
兄弟友情提示
· 请自觉遵守国家有关法律、法规,尊重网上道德。
· 兄弟在线坚决抵制不良言行,违者文责自负。
· 如果文章有版权或其他问题等,请联系我们,我们会尽快处理。
· 文章注名来自网络的旨在传播共享信息,不做其它用途;注名原创的本站支持原创,但不代表同意其观点。
· 兄弟在线拥有管理用户与其文章和评论的一切权利,并有权在网站内转载或引用。
兄弟在线
兄弟热门文章
兄弟推荐文章
兄弟站内搜索

兄弟感兴趣的文章
兄弟最新影视