zoukankan      html  css  js  c++  java
  • datagridview 导出excel

    原文地址 http://www.cnblogs.com/newwind521/archive/2007/04/02/697356.html

    谢谢博主

            public static void ExportDataGridViewToExcel(DataGridView dataGridview1)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出Excel文件到";

                saveFileDialog.ShowDialog();

                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
                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();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }

  • 相关阅读:
    准确率99%!基于深度学习的二进制恶意样本检测——瀚思APT 沙箱恶意文件检测使用的是CNN,LSTM TODO
    借贷宝有多少人看得懂?借贷宝系统崩溃分析
    武汉Uber优步司机奖励政策
    杭州优步uber司机第三组奖励政策
    杭州优步uber司机第二组奖励政策
    杭州优步uber司机第一组奖励政策
    优步北京B组(8月10日-8月16日奖励规则)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(8月4日)
    关于借贷宝“骗钱、推广是假的、传销、恶意盗取用户信息等”谣言的澄清
    借贷宝谣言制造者孟某承认造谣并公开致歉
  • 原文地址:https://www.cnblogs.com/ok519/p/1561820.html
Copyright © 2011-2022 走看看