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, Excel. XlSaveAsAccessMode. xlNoChange,
41 m_objOpt, m_objOpt );
42 m_objBook.Close (false, m_objOpt, m_objOpt );
43 m_objExcel.Quit ();
44}
1. Of course, you must add a reference to excel before performing the preceding operations.
2. Set the Excel permission in decom.