. Net Program excel export method can be described everywhere. however, it is only the export of some data. versions, formats, and locations are generally not considered. and word export. this may not be frequently used. word operations are indeed a little harder than excel operations. here, the younger brother collected some information. I wrote a demo. it seems suitable for use in the development process. the core code below will be shared with you.
The project first references a component Microsoft. office. interop. word (Here we declare that excel is generally used to import Excel in the project. dll version. sometimes the code is correct, but the excel version is inconsistent, it may cause export failure. at that time, I was confused for a long time. I could upload my local computer to the server and smiled ....)
First, the excel export Method
View Code
1 /// <summary>
2 // export the data in the current DataGridView to 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) // export the new file button to Excel
7 {
8 if (gridView. Rows. Count = 0)
9 {
10 MessageBox. Show ("no data is available for export! "," Prompt ", 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 = "export file storage path ";
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 // write the title
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 // write content
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 ("data has been successfully exported to:" + saveFileDialog. FileName. ToString (), "exported", MessageBoxButtons. OK, MessageBoxIcon. Information );
58 progreesBar. Value = 0;
59 progreesBar. Visible = false;
60}
61 catch (Exception e)
62 {
63 MessageBox. Show (e. Message, "friendly prompt", MessageBoxButtons. OK );
64}
65 finally
66 {
67 sw. Close ();
68 myStream. Close ();
69}
70}
71}
Then export the wrold method (basically the same as the export excel.) Here we recommend a. C # operation word blog (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 ("no data is available for export! "," Prompt ", 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 );
20 // create a table
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 ++) // set the title
25 {
26 table. Cell (0, I + 1). Range. Text = srcDgv. Columns [I]. HeaderText;
27}
28 for (int I = 1; I <srcDgv. Rows. Count; I ++) // fill data
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 );
37 document. Close (ref none, ref none, ref none );
38
39 progreesBar. Value = 100;
40 MessageBox. Show ("data has been exported to:" + sfile. FileName. ToString (), "exported", MessageBoxButtons. OK, MessageBoxIcon. Information );
41 progreesBar. Value = 0;
42 progreesBar. Visible = false;
43}
44 catch (Exception e)
45 {
46 MessageBox. Show (e. Message, "friendly prompt", MessageBoxButtons. OK );
47}
48 finally
49 {
50 wordApp. Quit (ref none, ref none, ref none );
51}
52}
53}
54
55}
The above code can be used directly. It is very convenient. (* ^__ ^ *) ....