Codee#1313

切换背景色
主题: 字体: 切换行号 全选代码块(Ctrl+C复制) 9个月前贴出, C# 语言
C#代码: Codee#1313
01 private void buttonToExcel_Click(object sender, EventArgs e)
02 {
03
04     SaveFileDialog saveFileDialog = new SaveFileDialog();
05     saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
06     saveFileDialog.FilterIndex = 0;
07     saveFileDialog.RestoreDirectory = true;
08     saveFileDialog.CreatePrompt = true;
09     saveFileDialog.Title = "Export Excel File To";
10     saveFileDialog.ShowDialog();
11     Stream myStream;
12     myStream = saveFileDialog.OpenFile();
13     StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
14     string str = "";
15     try
16     {
17         //写标题
18         for (int i = 0; i < this.dataGridView3.ColumnCount; i++)
19         {
20             if (i > 0)
21             {
22                 str += "\t";
23             }
24
25             str += this.dataGridView3.Columns[i].HeaderText;
26         }
27         sw.WriteLine(str);
28         //写内容
29         for (int j = 0; j < this.dataGridView3.Rows.Count; j++)
30         {
31             string tempStr = "";
32             for (int k = 0; k < this.dataGridView3.Columns.Count; k++)
33             {
34                 if (k > 0)
35                 {
36                     tempStr += "\t";
37                 }
38                 tempStr += this.dataGridView3.Rows[j].Cells[k].Value + "";
39             }
40             sw.WriteLine(tempStr);
41         }
42
43         sw.Close();
44         myStream.Close();
45     }
46     catch (Exception ex)
47     {
48         MessageBox.Show(ex.ToString());
49     }
50
51     finally
52     {
53         sw.Close();
54         myStream.Close();
55     }    
56     MessageBox.Show("OK");
57 }
返回正常查看模式 返回代码发芽网首页 编辑本文