SQL Server and Excel export from datatable to excel in Asp.net

Source: Internet
Author: User
SQL Server and Excel export from datatable to excel in Asp.net


The following body:

1. Read the database directly from Excel

Insert into t_test (field)

Select Field

From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C: \ test.xls ";
User ID = admin; Password =;
Extended properties = Excel 8.0 ')... [sheet1 $]

2. directly write data to excel from the database

Exec master .. xp_cmdshell 'bcp "select au_fname, au_lname from pubs .. authors order by au_lname "queryout c: \ test.xls-C-s" Soa "-U" sa "-P" sa "'pay attention to the Case sensitivity of parameters. In addition, this method writes data

No title

3. export data from datatable to excel

Stringwriter = new stringwriter ();
Htmltextwriter htmlwriter = new htmltextwriter (stringwriter );
DataGrid Excel = new DataGrid ();
System. Web. UI. webcontrols. tableitemstyle alternatingstyle = new tableitemstyle ();
System. Web. UI. webcontrols. tableitemstyle headerstyle = new tableitemstyle ();
System. Web. UI. webcontrols. tableitemstyle itemstyle = new tableitemstyle ();
Alternatingstyle. backcolor = system. Drawing. color. lightgray;
Headerstyle. backcolor = system. Drawing. color. lightgray;
Headerstyle. Font. Bold = true;
Headerstyle. horizontalalign = system. Web. UI. webcontrols. horizontalalign. Center;
Itemstyle. horizontalalign = system. Web. UI. webcontrols. horizontalalign. Center ;;

Excel. alternatingitemstyle. mergewith (alternatingstyle );
Excel. headerstyle. mergewith (headerstyle );
Excel. itemstyle. mergewith (itemstyle );
Excel. gridlines = gridlines. Both;
Excel. headerstyle. Font. Bold = true;
Excel. datasource = DT. defaultview; // output datatable content
Excel. databind ();
Excel. rendercontrol (htmlwriter );

String filestr = "D: \ data \" + filepath; // filepath is the file path.
Int Pos = filestr. lastindexof ("\\");
String file = filestr. substring (0, POS );
If (! Directory. exists (File ))
{
Directory. createdirectory (File );
}
System. Io. streamwriter Sw = new streamwriter (filestr );
Sw. Write (stringwriter. tostring ());
Sw. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.