1if(!System.IO.File.Exists(Server.MapPath("aa.xls")))
2 {
3 // Excel object references.
4 Excel.Application m_objExcel = null;
5 Excel.Workbooks m_objBooks = null;
6 Excel._Workbook m_objBook = null;
7 Excel.Sheets m_objSheets = null;
8 Excel._Worksheet m_objSheet = null;
9 Excel.Range m_objRange = null;
10 Excel.Font m_objFont = null;
11
12 // Frequenty-used variable for optional arguments.
13 object m_objOpt = System.Reflection.Missing.Value;
14
15 // Start a new workbook in Excel.
16 m_objExcel = new Excel.Application();
17 m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
18 m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
19
20 // Add data to cells of the first worksheet in the new workbook.
21 m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
22 m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
23
24 m_objRange = m_objSheet.get_Range("A1", m_objOpt);
25 m_objRange.set_Value(m_objOpt,"FirstName");
26 m_objRange = m_objSheet.get_Range("B1", m_objOpt);
27 m_objRange.set_Value(m_objOpt,"Last Name");
28
29 // Apply bold to cells A1:B1.
30 m_objRange = m_objSheet.get_Range("A1", "G1");
31 m_objFont = m_objRange.Font;
32 m_objFont.Bold=true;
33
34 // Paths used by the sample code for accessing and storing data.
35 object m_strSampleFolder = Server.MapPath("TempFile")+"\\";
36
37 // Save the workbook and quit Excel.
38
39 m_objBook.SaveAs(m_strSampleFolder + "aa.xls", m_objOpt, m_objOpt,
40 m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
41 m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
42 m_objBook.Close(false, m_objOpt, m_objOpt);
43 m_objExcel.Quit();
44 }
2 {
3 // Excel object references.
4 Excel.Application m_objExcel = null;
5 Excel.Workbooks m_objBooks = null;
6 Excel._Workbook m_objBook = null;
7 Excel.Sheets m_objSheets = null;
8 Excel._Worksheet m_objSheet = null;
9 Excel.Range m_objRange = null;
10 Excel.Font m_objFont = null;
11
12 // Frequenty-used variable for optional arguments.
13 object m_objOpt = System.Reflection.Missing.Value;
14
15 // Start a new workbook in Excel.
16 m_objExcel = new Excel.Application();
17 m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
18 m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
19
20 // Add data to cells of the first worksheet in the new workbook.
21 m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
22 m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
23
24 m_objRange = m_objSheet.get_Range("A1", m_objOpt);
25 m_objRange.set_Value(m_objOpt,"FirstName");
26 m_objRange = m_objSheet.get_Range("B1", m_objOpt);
27 m_objRange.set_Value(m_objOpt,"Last Name");
28
29 // Apply bold to cells A1:B1.
30 m_objRange = m_objSheet.get_Range("A1", "G1");
31 m_objFont = m_objRange.Font;
32 m_objFont.Bold=true;
33
34 // Paths used by the sample code for accessing and storing data.
35 object m_strSampleFolder = Server.MapPath("TempFile")+"\\";
36
37 // Save the workbook and quit Excel.
38
39 m_objBook.SaveAs(m_strSampleFolder + "aa.xls", m_objOpt, m_objOpt,
40 m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
41 m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
42 m_objBook.Close(false, m_objOpt, m_objOpt);
43 m_objExcel.Quit();
44 }
1 .当然,在进行上述操作之前要添加对Excel的引用。
2.要在decom中对Excel的权限进行设置