当前位置:编程学习 > C#/ASP.NET >>

求C#中的datagridview控件导出至excel表格的方法

这是全部代码

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.OleDb;


namespace Myaccess2
{
    public partial class Form1 : Form
    {
        DataTable dt;

        string myConn = "provider= Microsoft.Jet.OLEDB.4.0; Data Source =db1.mdb";

        string Sql = "select * from Myuser";

        public Form1()
        {
            InitializeComponent();
            
        }
        private OleDbConnection GetConn()
        {
            OleDbConnection olecon = new OleDbConnection(myConn);

            olecon.Open();

            return olecon;

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //显示按钮
            DataView dv = this.dt.DefaultView;

            dv.RowFilter = "签到日期<'" + DateTime.Now.AddDays(-7).ToShortDateString() + "'";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dt = new DataTable();

            OleDbConnection Conn = GetConn();

            OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            Conn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //刷新按钮
            DataView dv = this.dt.DefaultView;
            
            dv.RowFilter = "签到日期<'" + DateTime.Now.AddDays(+1).ToShortDateString() + "'";
        }
        private void UpdateDatabase(string sql, DataTable dt)
        {
            OleDbConnection Conn = GetConn();

            OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);

            OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);

            try
            {
                lock (this)
                {
                    da.Update(dt);
                }
            }
            catch (OleDbException ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                Conn.Close();
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            //记录按钮
            try
            {
                DataRow[] dr = dt.Select("姓名='" + textBox1.Text.Trim() + "'");

                if (dr.Length > 0)
                    for (int i = 0; i < dr.Length; i++)
                    {
                        dr[i]["签到日期"] = DateTime.Now.ToShortDateString();
                        MessageBox.Show("记录成功!");
                    }
                else
                    MessageBox.Show("找不到该人员,请检查姓名输入是否有误!");

                UpdateDatabase(Sql, dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
            textBox1.Text = "";
            textBox1.Focus();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

       
    }
}

导出方法1:
在DGV显示的数据中,我直接鼠标选取全部,Ctrl+C,然后在Excel中Ctrl+V。
     得到的结果:数字显示正常,但是文字内容全部为乱码。


导出方法2:
单击Button5,将筛选出来的数据导出到桌面的一个Excel文件

希望高手能帮忙解决这个问题,无论是哪个方法解决都可以 --------------------编程问答-------------------- 简单点的
Exporting a DataGridView to Excel in .NET 2.0 (C# code)

高级点的

http://www.codeproject.com/KB/reporting-services/ReportExporters_WinForms.aspx --------------------编程问答-------------------- 我的QQ:271012219
希望高手能在线指导 --------------------编程问答-------------------- 救命啊 不要搞英文 看不懂 --------------------编程问答-------------------- 看看就能拿10分
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,