用stream将dgv中的数据导出到word或excel中小例子
生活随笔
收集整理的這篇文章主要介紹了
用stream将dgv中的数据导出到word或excel中小例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 ?1?private?void?DataGridViewToExcel(DataGridView?dgv)
?2?????????{
?3?????????????SaveFileDialog?dlg?=?new?SaveFileDialog();
?4?????????????//dlg.Filter="Word?files(*.doc)|*.doc";
?5?????????????dlg.Filter?=?"Execl?files?(*.xls)|*.xls";
?6?????????????dlg.FilterIndex?=?0;
?7?????????????dlg.RestoreDirectory?=?true;
?8?????????????dlg.CreatePrompt?=?true;
?9?????????????dlg.Title?=?"保存為Excel文件";
10?????????????if?(dlg.ShowDialog()?==?DialogResult.OK)
11?????????????{
12?????????????????Stream?myStream;
13?????????????????myStream?=?dlg.OpenFile();
14?????????????????StreamWriter?sw?=?new?StreamWriter(myStream,?System.Text.Encoding.GetEncoding(-0));
15?????????????????string?columnTitle?=?"";
16?????????????????try
17?????????????????{
18?????????????????????//寫入列標題
19?????????????????????for?(int?i?=?0;?i?<?dgv.ColumnCount;?i++)
20?????????????????????{
21?????????????????????????if?(i?>?0)
22?????????????????????????{
23?????????????????????????????columnTitle?+=?"\t";
24?????????????????????????}
25?????????????????????????columnTitle?+=?dgv.Columns[i].HeaderText;
26?????????????????????}
27?????????????????????sw.WriteLine(columnTitle);
28?????????????????????//寫入列內容
29?????????????????????for?(int?j?=?0;?j?<?dgv.Rows.Count;?j++)
30?????????????????????{
31?????????????????????????string?columnValue?=?"";
32?????????????????????????for?(int?k?=?0;?k?<?dgv.Columns.Count;?k++)
33?????????????????????????{
34?????????????????????????????if?(k?>?0)
35?????????????????????????????{
36?????????????????????????????????columnValue?+=?"\t";
37?????????????????????????????}
38?????????????????????????????if?(dgv.Rows[j].Cells[k].Value?==?null)
39?????????????????????????????????columnValue?+=?"";
40?????????????????????????????else
41?????????????????????????????????columnValue?+=?dgv.Rows[j].Cells[k].Value.ToString().Trim();
42?????????????????????????}
43?????????????????????????sw.WriteLine(columnValue);
44?????????????????????}
45?????????????????????sw.Close();
46?????????????????????myStream.Close();
47?????????????????}
48?????????????????catch?(Exception?e)
49?????????????????{
50?????????????????????MessageBox.Show(e.ToString());
51?????????????????}
52?????????????????finally
53?????????????????{
54?????????????????????sw.Close();
55?????????????????????myStream.Close();
56?????????????????}
57?????????????}
58?????????}
59??代碼來源:網絡。
60??
61?
?2?????????{
?3?????????????SaveFileDialog?dlg?=?new?SaveFileDialog();
?4?????????????//dlg.Filter="Word?files(*.doc)|*.doc";
?5?????????????dlg.Filter?=?"Execl?files?(*.xls)|*.xls";
?6?????????????dlg.FilterIndex?=?0;
?7?????????????dlg.RestoreDirectory?=?true;
?8?????????????dlg.CreatePrompt?=?true;
?9?????????????dlg.Title?=?"保存為Excel文件";
10?????????????if?(dlg.ShowDialog()?==?DialogResult.OK)
11?????????????{
12?????????????????Stream?myStream;
13?????????????????myStream?=?dlg.OpenFile();
14?????????????????StreamWriter?sw?=?new?StreamWriter(myStream,?System.Text.Encoding.GetEncoding(-0));
15?????????????????string?columnTitle?=?"";
16?????????????????try
17?????????????????{
18?????????????????????//寫入列標題
19?????????????????????for?(int?i?=?0;?i?<?dgv.ColumnCount;?i++)
20?????????????????????{
21?????????????????????????if?(i?>?0)
22?????????????????????????{
23?????????????????????????????columnTitle?+=?"\t";
24?????????????????????????}
25?????????????????????????columnTitle?+=?dgv.Columns[i].HeaderText;
26?????????????????????}
27?????????????????????sw.WriteLine(columnTitle);
28?????????????????????//寫入列內容
29?????????????????????for?(int?j?=?0;?j?<?dgv.Rows.Count;?j++)
30?????????????????????{
31?????????????????????????string?columnValue?=?"";
32?????????????????????????for?(int?k?=?0;?k?<?dgv.Columns.Count;?k++)
33?????????????????????????{
34?????????????????????????????if?(k?>?0)
35?????????????????????????????{
36?????????????????????????????????columnValue?+=?"\t";
37?????????????????????????????}
38?????????????????????????????if?(dgv.Rows[j].Cells[k].Value?==?null)
39?????????????????????????????????columnValue?+=?"";
40?????????????????????????????else
41?????????????????????????????????columnValue?+=?dgv.Rows[j].Cells[k].Value.ToString().Trim();
42?????????????????????????}
43?????????????????????????sw.WriteLine(columnValue);
44?????????????????????}
45?????????????????????sw.Close();
46?????????????????????myStream.Close();
47?????????????????}
48?????????????????catch?(Exception?e)
49?????????????????{
50?????????????????????MessageBox.Show(e.ToString());
51?????????????????}
52?????????????????finally
53?????????????????{
54?????????????????????sw.Close();
55?????????????????????myStream.Close();
56?????????????????}
57?????????????}
58?????????}
59??代碼來源:網絡。
60??
61?
?
轉載于:https://www.cnblogs.com/angleSJW/archive/2009/12/20/1628397.html
總結
以上是生活随笔為你收集整理的用stream将dgv中的数据导出到word或excel中小例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IE 8 Accelerator加速器开
- 下一篇: IT女工作者