.net程序导出excel的方法可谓是遍地都是.但都只是一些数据的导出.版本,格式,位置,一般都不会去考虑.还有word的导出.可能这个大家不经常会用到.操作word确实比excel忙烦了一点.在这里,小弟收集了一些资料.写了一个demo.感觉挺适合开发过程中使用.下面贴出核心代码与大家分享.
项目先引用一个组件Microsoft.Office.Interop.Word(在此声明一下一般我们导出excel引用在项目中引入Excel.dll这需要考虑啊版本问题.有时代码正确,但是excel版本不一致,也会造成导出失败.当时也迷茫了好久,本机就可以,上传到服务器,笑 了....)
首先,导出excel的方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
上述代码可以直接拿来使用.很方便哦.(*^__^*) 嘻嘻. . . .