Using Microsoft. Office. InterOP. Excel;
For Office 2003, you must add a reference to Microsoft Excel 11.0 Object Library.
If you cannot use using Microsoft. Office. Core or Excel after adding
The referenced DLL is
InterOP. Microsoft. Office. Core. dll
InterOP. Excel. dll
Excel export class:
Using system; using system. io; using system. data; using Microsoft. office. interOP. excel; /// <summary> /// export the excal file /// </Summary> public class importexcel {private String title; private string rpt_name; private string outfilepath; private system. data. datatable DT; private system. data. datatable outtable; Public importexcel () {}/// <summary> /// set the Excel title /// </Summary> Public String rptname {get {Return title;} set {Title = value ;}/// <summary> /// table to be imported into Excel /// </Summary> public system. data. datatable rptdata {get {return DT;} set {dt = value ;}} /// <summary> /// set the storage location of the output file /// </Summary> Public String outfilepath {get {return outfilepath;} set {outfilepath = value ;}} /// <summary> /// constructor. To use this class, enter the Excel title, datatable, file Storage path // </Summary> /// <Param name = "rptname"> the E Xcel title </param> /// <Param name = "dtable"> name of the table to be filled in Excel </param> /// <Param name = "filepath"> path </param> Public importexcel (string rptname, system. data. datatable dtable, string file_path) {rpt_name = rptname; outtable = dtable; outfilepath = file_path; try {This. insertexcel ();} catch (exception e) {Throw new exception (E. message) ;}//< summary> /// this function inserts records into an Excel table. The data to be inserted is a memory table. the column name in the memory table is the column name in the table/ // The dynamic connection library used by this function, Excel. DLL, office. DLL, vbide. DLL. /// </Summary> Public void insertexcel () {int col_count = 0; int row_count = 0; int K = 0; int L = 0; // check whether the object exists if (file. exists (outfilepath) {Throw new exception ("the file already exists. creation failed! ");} Microsoft. office. interOP. excel. applicationclass rptapp = new Microsoft. office. interOP. excel. applicationclass (); rptapp. application. workbooks. add (true); rptapp. visible = false; object missing = system. reflection. missing. value; Microsoft. office. interOP. excel. workbook rptbook = rptapp. workbooks [1]; Microsoft. office. interOP. excel. worksheet rptsheet = (Microsoft. office. interOP. excel. worksheet) rptbook. activesheet; // set the report title rptsheet. cells [1, 1] = rpt_name; // Insert the column title foreach (datacolumn Col in outtable. columns) {col_count ++; rptsheet. cells [2, col_count] = Col. columnname. tostring ();} // set the column title format rptsheet. get_range (rptsheet. cells [2, 1], rptsheet. cells [2, col_count]). font. bold = true; // bold or not: Yes // sets the cell format foreach (datarow DR in outtable. rows) {row_count ++; col_count = 0; foreach (datacolumn Col in outtable. columns) {col_count ++; If (Col. datatype = system. type. getType ("system. string ") {rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [row_count + 2, col_count]). numberformatlocal = "@";} else if (Col. datatype = system. type. getType ("system. datetime ") {rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [row_count + 2, col_count]). numberformatlocal = "yyyy-m-d" ;}}// insert a record to the report for (INT I = 0; I <outtable. rows. count; I ++) {for (Int J = 0; j <outtable. columns. count; j ++) {k = I + 3; L = J + 1; rptsheet. cells [K, L] = outtable. rows [I] [J]. tostring (); rptsheet. get_range (rptsheet. cells [1, L], rptsheet. cells [1, l]). borders. linestyle = 7; rptsheet. get_range (rptsheet. cells [2, L], rptsheet. cells [2, l]). borders. linestyle = 1; rptsheet. get_range (rptsheet. cells [K, L], rptsheet. cells [K, L]). borders. linestyle = 1 ;}/// set the title format rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [1, 1]). font. bold = true; rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [1, 1]). font. size = 22; // set the report title to cross-column center merge cell rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [1, col_count]). select (); rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [1, col_count]). horizontalalignment = Microsoft. office. interOP. excel. xlhalign. xlhaligncenteracrossselection; // set the report table to the rptsheet of the Optimum width. get_range (rptsheet. cells [1, 1], rptsheet. cells [row_count, col_count]). select (); rptsheet. get_range (rptsheet. cells [1, 1], rptsheet. cells [row_count, col_count]). columns. autofit (); // save the file to rptbook in the specified path. saveas (outfilepath, missing, Microsoft. office. interOP. excel. xlsaveasaccessmode. xlnochange, missing, and missing); // close the file rptbook. close (false, outfilepath, true); rptapp. quit (); system. runtime. interopservices. marshal. releasecomobject (rptsheet); system. runtime. interopservices. marshal. releasecomobject (rptbook); system. runtime. interopservices. marshal. releasecomobject (rptapp); // force release of useless resource GC. collect ();}}
Error:Failed to retrieve components whose CLSID is {00024500-0000-0000-c000-000000000046} in the com class factory because of the following error: 80070005.
Solution 1:
Control Panel-> Administrative Tools-> component services-> Computer-> my computer-> DCOM configuration-> Microsoft Excel applicationProgram
After
Click properties to open the Properties dialog box for this application.
2. Click the ID tab and select an interactive user.
3. Click the "Security" tab, select "Custom" in the "Start and activate Permissions" and "Access Permissions" groups, and then
Custom-> edit-> Add ASP. NET account and iuser _ computer name: Local activation and local access are required.
4. Select an interactive user on the "ID" tab