winfom下如何将DATASET 导入EXCEL(原代码已测试)

1,首先添加引用COM—Microsoft Excel 11.0 Object Library

                              Microsoft Excel 5.0

                              Microsoft office12.0 object libarary

2,在.cs 中引用命名空间 using System.Runtime.InteropServices;
                               using Microsoft.Office.Interop.Excel;

3。代码如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
namespace datagridview操作
{
    public partial class Form1 : Form
    {
        SqlDataAdapter com;
        DataSet dst;
        public Form1()
        {
            InitializeComponent();
        }
       private void Form1_Load(object sender, EventArgs e)
        {
            string str = "select * from student_user";
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationSettings.AppSettings["MyConn"];
            //MessageBox.Show("数据说库已连上");
            com = new SqlDataAdapter(str, con);
            SqlCommandBuilder cb = new SqlCommandBuilder(com); //通过该语句生成相应的(insert into/update/delete Sql)
            dst = new DataSet();
            com.Fill(dst); //填充DST
            dataGridView1.DataSource = dst.Tables[0]; //绑定
        }

        private void BtnUpd_Click(object sender, EventArgs e)
        {
            //由于DataGridView已与DataSet绑定,对DataGridView的更改(添加/删除/更新)亦会自动改变DataSet

            //更新回数据库
            try {
                //com.Update(dst);
            //dst.AcceptChanges();
              //dst.GetChanges(DataRowState.Added);
             com.Update(dst);
            MessageBox.Show("数据更新成功!");
            //Application.DoEvents();
            //Invalidate();
            //this.Refresh();
            this.Form1_Load(this, new System.EventArgs());  //加载窗体
         
           }
            catch(Exception ee)
            {
                MessageBox.Show("" + ee.Message + "");
            }
            finally{}
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int a;
            a = dataGridView1.CurrentRow.Index;
            dataGridView1.BindingContext[dataGridView1.DataSource, dataGridView1.DataMember].RemoveAt(a);
            //dst.Tables[0].Rows[a].Delete();
        }
        //导出到EXCEL

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
              int rowIndex=1;
              int colIndex=0;
            excel.Application.Workbooks.Add(true);
            System.Data.DataTable table = dst.Tables[0];
           //将所得到的表的列名,赋值给单元格
          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; 
}

}

posted @ 2008-07-31 16:57  浪子の无悔  阅读(386)  评论(0编辑  收藏  举报