private void button5_Click(object sender, EventArgs e)
        {
            SaveFileDialog mSaveFileDialog = new SaveFileDialog();
            mSaveFileDialog.Filter = "文本文件|.txt|Excel文件|*.xls|所有文件|*.*";
            if (mSaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fName = mSaveFileDialog.FileName;
                Encoding encode = Encoding.GetEncoding("GB2312");
                StreamWriter sw = new StreamWriter(fName, false, encode);
                string colname = "";
                string rows = "";
                try
                {
                    for (int i = 0; i < dataGridView1.ColumnCount; i++) { colname += dataGridView1.Columns[i].HeaderText + "      "; }
                    sw.WriteLine(colname);
                    for (int h = 0; h < dataGridView1.Rows.Count - 1; h++)
                    {
                        rows = "";
                        for (int l = 0; l < dataGridView1.ColumnCount; l++)
                        { rows += dataGridView1.Rows[h].Cells[l].Value.ToString() + "   "; }
                        sw.WriteLine(rows);
                    }
                    sw.WriteLine("  ");
                    sw.Close();
                    {
                        MessageBox.Show("文件导出成功");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败");
                }
            } 
        }

相关文章:

  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2021-10-12
  • 2021-12-16
  • 2021-11-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案