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

     SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "Export Excel File";
                saveFileDialog.ShowDialog();
                if (saveFileDialog.FileName == "")
                    return;
                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string str = "";
                try
                {
                    for (int i = 0; i < dataGridView5.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            str += "	";
                        }
                        str += dataGridView5.Columns[i].HeaderText;
                    }
                    sw.WriteLine(str);
                    for (int j = 0; j < dataGridView5.Rows.Count; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < dataGridView5.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                tempStr += "	";
                            }
                            tempStr += dataGridView5.Rows[j].Cells[k].Value.ToString();
                        }
                        sw.WriteLine(tempStr);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
  • 相关阅读:
    Redis杂谈
    General mistakes in parallel computing
    life of a NPTL pthread
    casting in C++
    function calling convention
    How exception works ?
    How `delete’ works ?
    How `new’ operator works ?
    老白的JAVA课程17 集合
    老白的JAVA课程16 卡片布局 javaBean
  • 原文地址:https://www.cnblogs.com/yuyingming/p/5162091.html
Copyright © 2011-2022 走看看