zoukankan      html  css  js  c++  java
  • C# DataGridView导出到Excel

     

           private void ExportToExcel(DataGridView dgvGF_IN_INFO)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "保存为Excel文件";
                saveFileDialog.ShowDialog();

                if (saveFileDialog.FileName.IndexOf(":") < 0) return; //被点了"取消"

                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = "";
                try
                {
                    //写入列标题
                    for (int i = 0; i < dgvGF_IN_INFO.ColumnCount; i++)
                    {
                        if (dgvGF_IN_INFO.Columns[i].Visible)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }

                            columnTitle += dgvGF_IN_INFO.Columns[i].HeaderText;
                        }
                    }
                    sw.WriteLine(columnTitle);

                    //写入列内容
                    for (int j = 0; j < dgvGF_IN_INFO.Rows.Count; j++)
                    {
                        string columnValue = "";
                        for (int k = 0; k < dgvGF_IN_INFO.Columns.Count; k++)
                        {
                            if (dgvGF_IN_INFO.Rows[j].Cells[k].Visible)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                          
                                if (dgvGF_IN_INFO.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dgvGF_IN_INFO.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }

  • 相关阅读:
    CSS旧版flex及兼容
    深入理解CSS弹性盒模型flex
    CSS文本方向
    文本溢出text-overflow和文本阴影text-shadow
    深入理解CSS中的空白符和换行
    CSS中常见的6种文本样式
    理解CSS相对定位和固定定位
    CSS绝对定位的应用
    深入理解CSS绝对定位absolute
    深入理解CSS中的margin负值
  • 原文地址:https://www.cnblogs.com/heavyhe/p/4547320.html
Copyright © 2011-2022 走看看