| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖
标题:Winform中怎么将datagridview中数据导入到excel表中?
只看楼主 加入收藏
erton_zyh
Rank: 2
等 级:论坛游民
帖 子:27
专家分:15
注 册:2011-11-4
结帖率:100%
收藏
 问题点数:0 回复次数:1 
Winform中怎么将datagridview中数据导入到excel表中?
Winform中怎么将datagridview中数据导入到excel表中,在网上看了一下,都不能实现。
搜索更多相关主题的帖子: excel 数据 网上 
2012-03-11 00:34
thjsunday
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-7-23
收藏
得分:0 
public static bool ExportDataGridViewToExcel1(DataGridView dataGridview1)
        {
            if (dataGridview1.Rows.Count <= 0)
            {
                MessageBox.Show("没有可导出的记录。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }


            //设置保存对话框
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出Excel文件到";
            //
            //打开保存对话框并判断,若在保存对话框上按取消按钮则返回false
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
            { return false; }
            //   
            //把dataGridview1的内容写入要保存到的excle文档
            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("GBK"));
            string str = "";
            try
            {
                //写标题  
                for (int i = 0; i < dataGridview1.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += dataGridview1.Columns[i].HeaderText;
                }
                sw.WriteLine(str);
                //写内容
                for (int j = 0; j < dataGridview1.Rows.Count; j++)
                {
                    string tempStr = "";
                    for (int k = 0; k < dataGridview1.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            tempStr += "\t";
                        }
                        if (dataGridview1.Rows[j].Cells[k].Value != null)
                            tempStr += dataGridview1.Rows[j].Cells[k].Value.ToString();
                    }
                    sw.WriteLine(tempStr);
                }
                sw.Close();
                myStream.Close();
                return true;
            }
            catch (Exception e)
            {
                //显示错误信息
                MessageBox.Show(e.ToString());
                return false;
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }
        }
2012-07-23 11:50
快速回复:Winform中怎么将datagridview中数据导入到excel表中?
数据加载中...
 
   



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

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