1. First, import the COM file Microsoft Excel 11.0 Object Library.
2. Add the InterOP. Excel. dll file.
Http://files.cnblogs.com/ghostljj/Interop.Excel.rar
3. perform the following steps:
/// <Summary>
/// Export Excel
/// </Summary>
/// <Param name = "DT"> datatable to be exported </param>
Public void exporttoexcel (system. Data. datatable DT)
{
If (Dt = NULL) return;
Excel. Application xlapp = new excel. Application ();
If (xlapp = NULL)
{
// Lblmsg. Text = "an Excel object cannot be created. Excel may not be installed on your computer ";
Lblmsg. Text = getlocalresourceobject ("noexcel"). tostring ();
Return;
}
Excel. workbooks = xlapp. workbooks;
Excel. Workbook workbook = workbooks. Add (Excel. xlwbatemplate. xlwbatworksheet );
Excel. worksheet = (Excel. worksheet) Workbook. worksheets [1]; // get sheet1
Excel. Range = NULL;
Long totalcount = DT. Rows. count;
Long rowread = 0;
Float percent = 0;
// Write the title
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Worksheet. cells [1, I + 1] = DT. Columns [I]. columnname;
Range = (Excel. Range) worksheet. cells [1, I + 1];
// Range. Interior. colorindex = 15; // background color
Range. Font. Bold = true; // bold
Range. horizontalalignment = excel. xlhalign. xlhaligncenter; // center
// Add a border
Range. borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, null );
// Range. columnwidth = 4.63; // you can specify the column width.
// Range. entirecolumn. autofit (); // automatically adjust the column width
// R1.entirerow. autofit (); // automatically adjust the Row Height
}
// Write content
For (INT r = 0; r <DT. Rows. Count; r ++)
{
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Worksheet. cells [R + 2, I + 1] = DT. Rows [r] [I];
Range = (Excel. Range) worksheet. cells [R + 2, I + 1];
Range. Font. size = 9; // font size
// Add a border
Range. borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, null );
Range. entirecolumn. autofit (); // automatically adjusts the column width.
}
Rowread ++;
Percent = (float) (100 * rowread)/totalcount;
System. Windows. Forms. application. doevents ();
}
range. borders [Excel. xlbordersindex. xlinsidehorizontal]. weight = excel. xlborderweight. xlthin;
If (DT. columns. count> 1)
{< br> range. borders [Excel. xlbordersindex. xlinsidevertical]. weight = excel. xlborderweight. xlthin;
}
try
{< br> workbook. saved = true;
workbook. savecopyas (system. web. httpruntime. appdomainapppath + "xmlfiles \ educewordfiles \" + this. context. user. identity. name + ". xls ");
}< br> catch (exception ex)
{< br> // lblmsg. TEXT = "An error occurred while exporting the file. The file may be opened! \ N "+ ex. message;
lblmsg. TEXT = getlocalresourceobject ("error "). tostring () + "\ n" + ex. message;
}
Workbooks. Close ();
If (xlapp! = NULL)
{
Xlapp. workbooks. Close ();
Xlapp. Quit ();
Int Generation = system. gc. getgeneration (xlapp );
System. runtime. interopservices. Marshal. releasecomobject (xlapp );
Xlapp = NULL;
System. gc. Collect (generation );
}
GC. Collect (); // forcibly destroy
# Region forcibly kills the recently opened Excel Process
System. Diagnostics. Process [] excelproc = system. Diagnostics. process. getprocessesbyname ("Excel ");
System. datetime starttime = new datetime ();
Int M, killid = 0;
For (m = 0; m <excelproc. length; m ++)
{
If (starttime <excelproc [M]. starttime)
{
Starttime = excelproc [M]. starttime;
Killid = m;
}
}
If (excelproc [killid]. hasexited = false)
{
Excelproc [killid]. Kill ();
}
# Endregion
// Download
Biclass. businesslogic. util. responsefile (page. Request, page. response, "reporttoexcel.xls"
, System. Web. httpruntime. appdomainapppath + "xmlfiles \ educewordfiles \" + this. Context. User. Identity. Name + ". xls", 1024000 );
}
//--------------------------------------------------------
4. If it is stored in IIS, it cannot be output now. You need to configure it.
Solution 1: Add in Web. config
<System. Web>
<Identity impersonate = "true" username = "Administrator name" Password = "password"/>
<System. Web>
Solution 2:
(1) In the Run-> dcomcnfg open the component service
(2) choose console root directory> component service> Computer> my computer> DCOM configuration> Microsoft Excel application. Program -> Property-> Security
(3) Start and activate permissions-> use custom-> Add an ASPnet user, and open Local startup And Local activation
Access permission-> use custom-> Add an ASPnet user, and open Local access And Remote Access