Now we often encounter the problem that customers want to print reports. We know that in B/S architecture, there are only two types of printing: one is Crystal Report, one is to import data into Excel for printing. Compared with the first one, Excel has obvious advantages! First of all, the price of Crystal Reports is very expensive. For a small and medium-sized software development company, you cannot invest so much money into purchasing related development tools. If you choose a report provided by vs.net, it is often difficult to meet the real needs of customers. If you use the Excel method, you can save a lot of money, or reduce the development cycle, then we are happy !!!
The following is a practical example:CodeTest passed
// In. CS
Using system. configuration; // you must specify the connected database in config.
Using common_function; // declare namespace
Using system. Data. sqlclient;
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
Bindgrid ();
}
}
Private void bindgrid ()
{
String SQL _str = "select * From repair_now where repair_sort_id = ''";
Mydatagrid. datasource = public_class.get_select_table (SQL _str); // call the public class to return the datatable
Mydatagrid. databind ();
// When importing an Excel table, first calculate the number
String SQL _count = "select count (*) from repair_now ";
String temp = public_class.judge_repeat (SQL _count );
Viewstate ["count"] = temp;
}
Private string table_name ()
{
String Tn = "to_excel ";
Return tn;
}
Private void toexcel (system. Web. UI. Control CTL, string table_name)
{
Httpcontext. Current. response. appendheader ("content-disposition", "attachment; filename =" + "" + table_name + ". xls ");
Httpcontext. Current. response. charset = "gb-2312 ";
Httpcontext. Current. response. contentencoding = system. Text. encoding. default;
Httpcontext. current. response. contenttype = "application/MS-Excel/MS-word"; // image/JPEG; text/html; image/GIF; VND. MS-Excel/MSWord
CTL. Page. enableviewstate = true;
System. Io. stringwriter Tw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (TW );
CTL. rendercontrol (HW );
Httpcontext. Current. response. Write (TW. tostring ());
Httpcontext. Current. response. End ();
}
Private void button_excel_click (Object sender, system. Web. UI. imageclickeventargs e) // click an Excel button
{
Mydatagrid. pagesize = convert. toint32 (viewstate ["count"]. tostring ());
Bindgrid ();
Mydatagrid. pagerstyle. Visible = false;
Toexcel (mydatagrid, table_name ());
}
The following are called public class public_class.cs
Using system. Data. sqlclient;
Using system. configuration;
Public class public_class
{
Private Static string connectstring = configurationsettings. deleettings ["connectstring"];
Public static datatable get_select_table (string SQL _str) // execute the query and return the datatable
{
Sqlconnection myconnection;
Myconnection = new sqlconnection (connectstring );
Sqldataadapter mycommand = new sqldataadapter (SQL _str, myconnection );
Dataset DS = new dataset ();
Mycommand. Fill (DS, "T1 ");
Return Ds. Tables ["T1"];
}
Public static string judge_repeat (string SQL _str)
{
Sqlconnection myconnection = new sqlconnection (connectstring );
Sqlcommand mycommand = new sqlcommand (SQL _str, myconnection );
Mycommand. Connection. open ();
Sqldatareader Dr;
Dr = mycommand. executereader ();
Try
{
Dr. Read ();
Return Dr [0]. tostring ();
}
Catch (exception ERR)
{
Return "";
}
Finally
{
Dr. Close ();
Mycommand. Connection. Close ();
}
}
}
ArticleFrom: http://ms.blogger.cn/wayneliu/