引用了组件还要引用命名空间,这是我的一段代码,你看看- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Excel=Microsoft.Office.Interop.Excel;
- using System.Reflection;
- using System.Runtime.InteropServices;
- namespace ITSERVICE
- {
- public partial class 向excel传值 : System.Web.UI.Page
- {
- // Excel object references.
- private Excel.Application m_objExcel = null;
- private Excel.Workbooks m_objBooks = null;
- private Excel._Workbook m_objBook = null;
- private Excel.Sheets m_objSheets = null;
- private Excel._Worksheet m_objSheet = null;
- private Excel.Range m_objRange = null;
- private Excel.Font m_objFont = null;
- private Excel.QueryTables m_objQryTables = null;
- private Excel._QueryTable m_objQryTable = null;
- private object m_objOpt = Missing.Value;
- private string m_strSampleFolder;
- protected void Page_Load(object sender, EventArgs e)
- {
- string filename = @"C:\Users\dell\Desktop\EuOrderReport.xls";
- m_objExcel = new Excel.Application();
- m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
- m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
- m_objExcel.Workbooks.Open(filename);
- // Add data to cells in the first worksheet in the new workbook.
- m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
- m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
- m_objRange = m_objSheet.get_Range("A1", m_objOpt);
- m_objRange.Value = "Last Name";
- m_objRange = m_objSheet.get_Range("B1", m_objOpt);
- m_objRange.Value = "First Name";
- m_objRange = m_objSheet.get_Range("A2", m_objOpt);
- m_objRange.Value = "Doe";
- m_objRange = m_objSheet.get_Range("B2", m_objOpt);
- m_objRange.Value = "John";
- // Apply bold to cells A1:B1.
- m_objRange = m_objSheet.get_Range("A1", "B1");
- m_objFont = m_objRange.Font;
- m_objFont.Bold = true;
- // Save the Workbook and quit Excel.
-
- m_objBook.SaveAs(@"C:\Users\dell\Desktop\Books1.xls", m_objOpt, m_objOpt,
- m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
- m_objOpt, m_objOpt, m_objOpt, m_objOpt);
- m_objBook.Close(false, m_objOpt, m_objOpt);
- m_objExcel.Quit();
- }
- }
- }
复制代码 |