| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3281 人关注过本帖, 1 人收藏
标题:如何将DataGridView里的数据通过一个按钮导入到Excel表中去?
只看楼主 加入收藏
小孔
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-2-25
结帖率:100%
收藏(1)
已结贴  问题点数:10 回复次数:2 
如何将DataGridView里的数据通过一个按钮导入到Excel表中去?
这个是我前面的代码和窗口,然后想通过Excel键将DataGridView里的数据导入到Excel表中去。
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;



namespace 表格处理
{
    public partial class Form1 : Form
    {
        OleDbConnection conn = new OleDbConnection();
        OleDbCommand comm = new OleDbCommand();
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=studentinfo.mdb";
            comm.Connection = conn;
             = "select * from studentinfo where 分数>75";
            adapter.SelectCommand = comm;
            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);//处理的新表放在了adapter中
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ds.Clear();
            adapter.Fill(ds, "good");//现在将adapter中的表以good名称填到ds中
            d1.DataSource = ds.Tables["good"];//d1中即将呈现的是新表good
        }

        private void button2_Click(object sender, EventArgs e)
        {
            adapter.Update(ds, "good");//更新数据库中的表
        }      
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: public Excel 如何 
2016-02-26 15:43
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:10 

   唯实惟新 至诚致志
2016-02-26 16:19
多多啦啦杰
Rank: 2
等 级:论坛游民
威 望:3
帖 子:2
专家分:20
注 册:2016-3-15
收藏
得分:0 
   public static bool FastExportToExcel(System.Data.DataTable dt, ProgressBar proBar, System.Windows.Forms.Label labPercent)
        {
            System.Windows.Forms.SaveFileDialog saveDia = new SaveFileDialog();
            saveDia.Filter = "Excel|*.xls";
            saveDia.Title = "导出为Excel文件";
            if (!(saveDia.ShowDialog() == System.Windows.Forms.DialogResult.OK
             && !string.Empty.Equals(saveDia.FileName)))
            {
                return false;
            }
            long totalCount = dt.Rows.Count;
            long rowRead = 0;
            float percent = 0;
            string FileName = "";
            FileName = saveDia.FileName;
            FileStream objFileStream;
            StreamWriter objStreamWriter;
            string strLine = "title";
            objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
            objStreamWriter = new StreamWriter(objFileStream, Encoding.Unicode);

            //objStreamWriter.WriteLine(strLine);//寫入標題
            strLine = "";

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                strLine = strLine + dt.Columns[i].ColumnName.ToString() + "" + Convert.ToChar(9);
            }
            objStreamWriter.WriteLine(strLine);
            strLine = "";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //strLine = strLine + (i + 1) + Convert.ToChar(9);//增加序號
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (dt.Columns[j].ColumnName.ToLower() == "time" && dt.Rows[i][j] != Convert.DBNull)
                    {
                        string strDateTime = Convert.ToDateTime(dt.Rows[i][j]).ToString("yyyy/MM/dd HH:mm:ss.fff");
                        strLine = strLine + " " + strDateTime + Convert.ToChar(9); //9:tab字符
                    }
                    else
                    {
                        strLine = strLine + dt.Rows[i][j].ToString() + Convert.ToChar(9); //9:tab字符
                    }
                }
                objStreamWriter.WriteLine(strLine);
                strLine = "";

                rowRead++;
                percent = ((float)(100 * rowRead)) / totalCount;
                System.Windows.Forms.Application.DoEvents();
                labPercent.Text = percent + "%"; labPercent.Refresh();//顯示百分比進度
                proBar.Value += 1;
            }
            objStreamWriter.Close();
            objFileStream.Close();

            return true;
        }
2016-03-15 15:30
快速回复:如何将DataGridView里的数据通过一个按钮导入到Excel表中去?
数据加载中...
 
   



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

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