C#將資料來源匯出EXCEL和Word

來源:互聯網
上載者:User

.net程式匯出excel的方法可謂是遍地都是.但都只是一些資料的匯出.版本,格式,位置,一般都不會去考慮.還有word的匯出.可能這個大家不經常會用到.操作word確實比excel忙煩了一點.在這裡,小弟收集了一些資料.寫了一個demo.感覺挺適合開發過程中使用.下面貼出核心代碼與大家分享.

項目先引用一個組件Microsoft.Office.Interop.Word(在此聲明一下一般我們匯出excel引用在項目中引入Excel.dll這需要考慮啊版本問題.有時代碼正確,但是excel版本不一致,也會造成匯出失敗.當時也迷茫了好久,本機就可以,上傳到伺服器,笑 了....)

首先,匯出excel的方法

View Code

 1   ///<summary>  
2 /// 匯出當前頁DataGridView中的資料到EXcel中
3 ///</summary>
4 ///<param name="dataGridView">dataGridView</param>
5 ///<param name="progreesBar">progreesBar</param>
6 public void ExportTOExcelWithoutWidget(DataGridView gridView, ProgressBar progreesBar, SaveFileDialog saveFileDialog) //另存新檔按鈕 匯出成Excel
7 {
8 if (gridView.Rows.Count == 0)
9 {
10 MessageBox.Show("沒有資料可供匯出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
11 return;
12 }
13 else
14 {
15 saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
16 saveFileDialog.FilterIndex = 0;
17 saveFileDialog.RestoreDirectory = true;
18 saveFileDialog.CreatePrompt = true;
19 saveFileDialog.Title = "匯出檔案儲存路徑";
20 saveFileDialog.ShowDialog();
21 progreesBar.Visible = true;
22 Stream myStream;
23 myStream = saveFileDialog.OpenFile();
24 //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
25 StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
26 string str = "";
27 try
28 {
29 //寫標題
30 for (int i = 0; i < gridView.ColumnCount; i++)
31 {
32 if (i > 0)
33 {
34 str += "\t";
35 }
36 str += gridView.Columns[i].HeaderText;
37 }
38 sw.WriteLine(str);
39 //寫內容
40 for (int j = 0; j < gridView.Rows.Count-1; j++)
41 {
42 string tempStr = "";
43 for (int k = 0; k < gridView.Columns.Count; k++)
44 {
45 if (k > 0)
46 {
47 tempStr += "\t";
48 }
49 tempStr += gridView.Rows[j].Cells[k].Value.ToString();
50 }
51 sw.WriteLine(tempStr);
52 progreesBar.Value += 100 / gridView.RowCount;
53 }
54 sw.Close();
55 myStream.Close();
56 progreesBar.Value = 100;
57 MessageBox.Show("資料已經成功匯出到:" + saveFileDialog.FileName.ToString(), "匯出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
58 progreesBar.Value = 0;
59 progreesBar.Visible = false;
60 }
61 catch (Exception e)
62 {
63 MessageBox.Show(e.Message, "友情提示", MessageBoxButtons.OK);
64 }
65 finally
66 {
67 sw.Close();
68 myStream.Close();
69 }
70 }
71 }

然後匯出wrold的方法(跟匯出excel大體一致.)這裡推薦一下.C#操作word的一個部落格(http://www.cnblogs.com/fellowcheng/articles/1274276.html)

 1  public void ExportDataGridViewToWord(DataGridView srcDgv, ProgressBar progreesBar, SaveFileDialog sfile)
2 {
3 if (srcDgv.Rows.Count == 0)
4 {
5 MessageBox.Show("沒有資料可供匯出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
6 return;
7 }
8 else
9 {
10 sfile.AddExtension = true;
11 sfile.DefaultExt = ".doc";
12 sfile.Filter = "(*.doc)|*.doc";
13 if (sfile.ShowDialog() == DialogResult.OK)
14 {
15 progreesBar.Visible = true;
16 object path = sfile.FileName;
17 Object none = System.Reflection.Missing.Value;
18 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
19 Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
20 //建立表格
21 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count, srcDgv.Columns.Count, ref none, ref none);
22 try
23 {
24 for (int i = 0; i < srcDgv.Columns.Count; i++)//設定標題
25 {
26 table.Cell(0, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
27 }
28 for (int i = 1; i < srcDgv.Rows.Count; i++)//填充資料
29 {
30 for (int j = 0; j < srcDgv.Columns.Count; j++)
31 {
32 table.Cell(i + 1, j + 1).Range.Text = srcDgv[j, i - 1].Value.ToString();
33 }
34 progreesBar.Value += 100 / srcDgv.RowCount;
35 }
36 document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
37 document.Close(ref none, ref none, ref none);
38
39 progreesBar.Value = 100;
40 MessageBox.Show("資料已經成功匯出到:" + sfile.FileName.ToString(), "匯出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
41 progreesBar.Value = 0;
42 progreesBar.Visible = false;
43 }
44 catch(Exception e)
45 {
46 MessageBox.Show(e.Message, "友情提示", MessageBoxButtons.OK);
47 }
48 finally
49 {
50 wordApp.Quit(ref none, ref none, ref none);
51 }
52 }
53 }
54
55 }

上述代碼可以直接拿來使用.很方便哦.(*^__^*) 嘻嘻. . . .

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.