Protected void btnexcel_click (Object sender, eventargs E)
{
If (this. dallistcontract. Items. Count> 0)
{
Dataset DS = new dataset ();
Datatable dt = new datatable ();
DT. Columns. Add ("no ");
DT. Columns. Add ("name ");
If (null! = Cache ["toexcel"])
{
Datatable temp = (datatable) cache ["toexcel"];
For (INT I = 0; I <temp. Rows. Count; I ++)
{
Datarow DR = DT. newrow ();
Dr [0] = I + 1;
Dr [1] = temp. Rows [I] ["code"];
Dr [2] = temp. Rows [I] ["classne"];
DT. Rows. Add (DR );
}
DS. Tables. Add (DT );
Exportexcel (DS, "test ");
}
}
}
Private void exportexcel (Dataset ds, string filename)
{
Microsoft. Office. InterOP. Excel. Application oexcel;
Oexcel = new Microsoft. Office. InterOP. Excel. Application ();
Try
{
Microsoft. Office. InterOP. Excel. Workbook obook;
Object omissing = system. reflection. Missing. value;
Obook = oexcel. workbooks. Add (omissing );
Httpresponse response = httpcontext. Current. response;
Int lie = Ds. Tables [0]. Columns. count;
Int hang = Ds. Tables [0]. Rows. count;
Int I, j, T;
String panduanstring = "";
I = 1;
For (j = 0; j <lie; j ++) // Title
{
Oexcel. cells [1, I ++] = Ds. Tables [0]. Columns [J]. columnname;
}
T = 1;
For (I = 0; I {
For (j = 0; j <lie; j ++)
{
Panduanstring = Ds. Tables [0]. Rows [I] [J]. tostring ();
If (panduanstring. GetType (). tostring () = "system. String ")
{
Oexcel. cells [I + 2, T ++] = "'" + Ds. Tables [0]. Rows [I] [J]. tostring ();
}
Else
Oexcel. cells [I + 2, T ++] = Ds. Tables [0]. Rows [I] [J]. tostring ();
}
T = 1;
}
Oexcel. Visible = true;
Obook. Saved = true;
Oexcel. usercontrol = false;
String Path = server. mappath ("Excel /");
String Mm = path + filename + ". xls ";
Oexcel. activeworkbook. savecopyas (mm );
Oexcel. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (object) oexcel );
GC. Collect ();
Response. Redirect ("Excel/" + filename + ". xls ");
}
Catch (exception E)
{
System. runtime. interopservices. Marshal. releasecomobject (object) oexcel );
GC. Collect ();
}
}