• C# DataGridView导出成Excel


    C# DataGridView导出成Excel

    2种方法参考:

    第一种:用流保存成xls文件. 这种方法比较好, 可以参考

      1C DataGridView导出成Excel (转) - changqingnew - 永不言弃

      2C DataGridView导出成Excel (转) - changqingnew - 永不言弃using System.IO;

      3C DataGridView导出成Excel (转) - changqingnew - 永不言弃

      4C DataGridView导出成Excel (转) - changqingnew - 永不言弃        

      5C DataGridView导出成Excel (转) - changqingnew - 永不言弃

      6C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃        /**//// <summary>

      7C DataGridView导出成Excel (转) - changqingnew - 永不言弃

      8C DataGridView导出成Excel (转) - changqingnew - 永不言弃        /// 另存新档按钮

      9C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     10C DataGridView导出成Excel (转) - changqingnew - 永不言弃        /// </summary>

     11C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     12C DataGridView导出成Excel (转) - changqingnew - 永不言弃        private void SaveAs() //另存新档按钮   导出成Excel

     13C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     14C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃        C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     15C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     16C DataGridView导出成Excel (转) - changqingnew - 永不言弃            SaveFileDialog saveFileDialog = new SaveFileDialog();

     17C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     18C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";

     19C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     20C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.FilterIndex = 0;

     21C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     22C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.RestoreDirectory = true;

     23C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     24C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.CreatePrompt = true;

     25C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     26C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.Title = "Export Excel File To"; 

     27C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     28C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     29C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.ShowDialog();

     30C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     31C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     32C DataGridView导出成Excel (转) - changqingnew - 永不言弃            Stream myStream;

     33C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     34C DataGridView导出成Excel (转) - changqingnew - 永不言弃            myStream = saveFileDialog.OpenFile();

     35C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     36C DataGridView导出成Excel (转) - changqingnew - 永不言弃            //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));

     37C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     38C DataGridView导出成Excel (转) - changqingnew - 永不言弃            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));

     39C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     40C DataGridView导出成Excel (转) - changqingnew - 永不言弃            string str = "";

     41C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     42C DataGridView导出成Excel (转) - changqingnew - 永不言弃            try

     43C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     44C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃            C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     45C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     46C DataGridView导出成Excel (转) - changqingnew - 永不言弃                //写标题

     47C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     48C DataGridView导出成Excel (转) - changqingnew - 永不言弃                for (int i = 0; i < dgvAgeWeekSex.ColumnCount; i++)

     49C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     50C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃                C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     51C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     52C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    if (i > 0)

     53C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     54C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     55C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     56C DataGridView导出成Excel (转) - changqingnew - 永不言弃                        str += "\t";

     57C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     58C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    }

     59C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     60C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    str += dgvAgeWeekSex.Columns[i].HeaderText;

     61C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     62C DataGridView导出成Excel (转) - changqingnew - 永不言弃                }

     63C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     64C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     65C DataGridView导出成Excel (转) - changqingnew - 永不言弃                sw.WriteLine(str);

     66C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     67C DataGridView导出成Excel (转) - changqingnew - 永不言弃 

     68C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     69C DataGridView导出成Excel (转) - changqingnew - 永不言弃                //写内容

     70C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     71C DataGridView导出成Excel (转) - changqingnew - 永不言弃                for (int j = 0; j < dgvAgeWeekSex.Rows.Count; j++)

     72C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     73C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃                C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     74C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     75C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    string tempStr = "";

     76C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     77C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    for (int k = 0; k < dgvAgeWeekSex.Columns.Count; k++)

     78C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     79C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     80C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     81C DataGridView导出成Excel (转) - changqingnew - 永不言弃                        if (k > 0)

     82C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     83C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃                        C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

     84C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     85C DataGridView导出成Excel (转) - changqingnew - 永不言弃                            tempStr += "\t";

     86C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     87C DataGridView导出成Excel (转) - changqingnew - 永不言弃                        }

     88C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     89C DataGridView导出成Excel (转) - changqingnew - 永不言弃                        tempStr += dgvAgeWeekSex.Rows[j].Cells[k].Value.ToString();

     90C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     91C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    }

     92C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     93C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    

     94C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     95C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    sw.WriteLine(tempStr);                    

     96C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     97C DataGridView导出成Excel (转) - changqingnew - 永不言弃                }

     98C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     99C DataGridView导出成Excel (转) - changqingnew - 永不言弃                sw.Close();

    100C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    101C DataGridView导出成Excel (转) - changqingnew - 永不言弃                myStream.Close();

    102C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    103C DataGridView导出成Excel (转) - changqingnew - 永不言弃            }

    104C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    105C DataGridView导出成Excel (转) - changqingnew - 永不言弃            catch (Exception e)

    106C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    107C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃            C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

    108C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    109C DataGridView导出成Excel (转) - changqingnew - 永不言弃                MessageBox.Show(e.ToString());

    110C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    111C DataGridView导出成Excel (转) - changqingnew - 永不言弃            }

    112C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    113C DataGridView导出成Excel (转) - changqingnew - 永不言弃            finally

    114C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    115C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃            C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

    116C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    117C DataGridView导出成Excel (转) - changqingnew - 永不言弃                sw.Close();

    118C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    119C DataGridView导出成Excel (转) - changqingnew - 永不言弃                myStream.Close();

    120C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    121C DataGridView导出成Excel (转) - changqingnew - 永不言弃            }           

    122C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    123C DataGridView导出成Excel (转) - changqingnew - 永不言弃      }

    124C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    125C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    第二种方法:引用Excel组件,先定义Excel对象(实例),再根据DataGridView中数据,一格一格去填充Excel对象(实例),单元格,最后保存Excel对象(实例)[调用对象Save()方法].

     1C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     2C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃      /**//// <summary>

     3C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     4C DataGridView导出成Excel (转) - changqingnew - 永不言弃        /// 另存新档按钮   导出成Excel

     5C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     6C DataGridView导出成Excel (转) - changqingnew - 永不言弃        /// </summary>

     7C DataGridView导出成Excel (转) - changqingnew - 永不言弃

     8C DataGridView导出成Excel (转) - changqingnew - 永不言弃        private void SaveToExcel() //另存新档按钮   导出成Excel

     9C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    10C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃        C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

    11C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    12C DataGridView导出成Excel (转) - changqingnew - 永不言弃            SaveFileDialog saveFileDialog = new SaveFileDialog();

    13C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    14C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";

    15C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    16C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.FilterIndex = 0;

    17C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    18C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.RestoreDirectory = true;

    19C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    20C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.CreatePrompt = true;

    21C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    22C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.Title = "Export Excel File To";

    23C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    24C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    25C DataGridView导出成Excel (转) - changqingnew - 永不言弃            saveFileDialog.ShowDialog();

    26C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    27C DataGridView导出成Excel (转) - changqingnew - 永不言弃            string strName = saveFileDialog.FileName; 

    28C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    29C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    30C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    31C DataGridView导出成Excel (转) - changqingnew - 永不言弃            System.Reflection.Missing miss = System.Reflection.Missing.Value;   

    32C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    33C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    34C DataGridView导出成Excel (转) - changqingnew - 永不言弃            Excel.Application excel = new Excel.ApplicationClass();

    35C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    36C DataGridView导出成Excel (转) - changqingnew - 永不言弃            Excel.Workbooks books = (Excel.Workbooks)excel.Workbooks;

    37C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    38C DataGridView导出成Excel (转) - changqingnew - 永不言弃            Excel.Workbook book = (Excel.Workbook)(books.Add(miss));

    39C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    40C DataGridView导出成Excel (转) - changqingnew - 永不言弃            Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;

    41C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    42C DataGridView导出成Excel (转) - changqingnew - 永不言弃            sheet.Name = "test";

    43C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    44C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    45C DataGridView导出成Excel (转) - changqingnew - 永不言弃            int colIndex=0;

    46C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    47C DataGridView导出成Excel (转) - changqingnew - 永不言弃            foreach (DataGridViewColumn column in dgvAgeWeekSex.Columns)

    48C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    49C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃            C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

    50C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    51C DataGridView导出成Excel (转) - changqingnew - 永不言弃                colIndex++;

    52C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    53C DataGridView导出成Excel (转) - changqingnew - 永不言弃                excel.Cells[1, colIndex] = column.HeaderText; 

    54C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    55C DataGridView导出成Excel (转) - changqingnew - 永不言弃            }

    56C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    57C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    58C DataGridView导出成Excel (转) - changqingnew - 永不言弃            for (int i = 0; i < dgvAgeWeekSex.Rows.Count; i++)

    59C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    60C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃            C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

    61C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    62C DataGridView导出成Excel (转) - changqingnew - 永不言弃                for (int j = 0; j < dgvAgeWeekSex.Columns.Count; j++)

    63C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    64C DataGridView导出成Excel (转) - changqingnew - 永不言弃C DataGridView导出成Excel (转) - changqingnew - 永不言弃                C DataGridView导出成Excel (转) - changqingnew - 永不言弃{

    65C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    66C DataGridView导出成Excel (转) - changqingnew - 永不言弃                    excel.Cells[i + 2, j + 1] = dgvAgeWeekSex.Rows[i].Cells[j].Value.ToString();

    67C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    68C DataGridView导出成Excel (转) - changqingnew - 永不言弃                }

    69C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    70C DataGridView导出成Excel (转) - changqingnew - 永不言弃            }

    71C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    72C DataGridView导出成Excel (转) - changqingnew - 永不言弃         

    73C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    74C DataGridView导出成Excel (转) - changqingnew - 永不言弃            sheet.SaveAs(strName, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss);         

    75C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    76C DataGridView导出成Excel (转) - changqingnew - 永不言弃            

    77C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    78C DataGridView导出成Excel (转) - changqingnew - 永不言弃            book.Close(false, miss, miss);

    79C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    80C DataGridView导出成Excel (转) - changqingnew - 永不言弃            books.Close();

    81C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    82C DataGridView导出成Excel (转) - changqingnew - 永不言弃            excel.Quit();

    83C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    84C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    85C DataGridView导出成Excel (转) - changqingnew - 永不言弃            //System.Runtime.InteropServices.Marshal.ReleaseComObject();   

    86C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    87C DataGridView导出成Excel (转) - changqingnew - 永不言弃            System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);

    88C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    89C DataGridView导出成Excel (转) - changqingnew - 永不言弃            System.Runtime.InteropServices.Marshal.ReleaseComObject(book);

    90C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    91C DataGridView导出成Excel (转) - changqingnew - 永不言弃            System.Runtime.InteropServices.Marshal.ReleaseComObject(books);

    92C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    93C DataGridView导出成Excel (转) - changqingnew - 永不言弃            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

    94C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    95C DataGridView导出成Excel (转) - changqingnew - 永不言弃            GC.Collect();

    96C DataGridView导出成Excel (转) - changqingnew - 永不言弃

    97C DataGridView导出成Excel (转) - changqingnew - 永不言弃        }

  • 相关阅读:
    Flask发送邮件
    python 可调用对象之类实例
    flask 跨域请求
    flask登录插件 flask-login
    flask 更新数据库
    python事物管理及同步锁
    Django signals 信号作用及用法说明
    python中各个response使用
    Ntp时间服务器
    Iptables-主机防火墙设置
  • 原文地址:https://www.cnblogs.com/czsl/p/2949035.html
Copyright © 2020-2023  润新知