110,545
社区成员
发帖
与我相关
我的任务
分享
using Excel;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
namespace TestWord
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Word 2007");
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;
Microsoft.Office.Interop.Word._Document oDoc;
object oMissing = System.Reflection.Missing.Value;
object strFileName = "C:\\test.docx";
if (File.Exists((string)strFileName))
File.Delete((string)strFileName);
// Create An New Word
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Paragraphs.Last.Range.Text = "Created An New word !\n\r";
oDoc.SaveAs(ref strFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
Console.WriteLine("Created An New word");
// Open the Word
oDoc = null;
oWord = null;
oWord = new Microsoft.Office.Interop.Word.Application();
oDoc = oWord.Documents.Open(ref strFileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Paragraphs.Last.Range.Text = "Open The Word !\n\r";
Console.WriteLine("Open The New word");
// Insert a paragraph
Microsoft.Office.Interop.Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
Console.WriteLine("Insert a paragraph");
// Close
oDoc.Save();
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
Console.WriteLine("Close . Over");
}
}
}
1. using System;
2. using System.Collections.Generic;
3. using System.Text;
4. using System.Data;
5. using System.Data.OleDb;
6. using System.Data.SqlClient;
7. using System.IO;
8. using Microsoft.Office.Interop.Excel;
9. namespace TestAccess
10. {
11. class Program
12. {
13. static void Main(string[] args)
14. {
15.
16. string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
17. strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";
18. strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
19. OleDbConnection objConnection = new OleDbConnection(strConnection);
20. objConnection.Open();
21. OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);
22. DataSet ds = new DataSet();
23. myCommandd.Fill(ds, "[Sheet1$]");
24. System.Data.DataTable dt = ds.Tables["[Sheet1$]"];
25. Console.WriteLine(dt.Columns[0].ToString());
26. Console.WriteLine(dt.Columns[1].ToString());
27. DataRow drDisplay = dt.Rows[0];
28. int[] num = new int[dt.Columns.Count];
29. for (int j = 0; ; )
30. {
31. for (int i = 0; i < dt.Columns.Count; i++)
32. {
33.
34. if (drDisplay[i] is DBNull) ;
35. else
36. num[i] += Convert.ToInt32(drDisplay[i]);
37.
38. }
39. if (++j >= dt.Rows.Count) break;
40. drDisplay = dt.Rows[j];
41. }
42. objConnection.Close();
43. object MissingValue = Type.Missing;
44. Microsoft.Office.Interop.Excel.Application app = new Application();
45. Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,
46. MissingValue, MissingValue, MissingValue,
47. MissingValue, MissingValue, MissingValue,
48. MissingValue, MissingValue, MissingValue,
49. MissingValue, MissingValue, MissingValue,
50. MissingValue);
51. Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
52. for (int i = 0; i < dt.Columns.Count; i++)
53. {
54. //注意下面是i+1,,excel小标默认从1开始
55. wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString();
56. }
57.
58. wbook.Save();
59. wbook.Close(true, null, null);
60. System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
61. System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
62. System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
63. wsheet = null;
64. wbook = null;
65. app = null;
66. GC.Collect();
67. }
68. }
69. }