Directly output data into Excel content in ASP. NET

Source: Internet
Author: User
<Meta name = "generator" content = "Microsoft Visual Studio. net 7.0 "> <meta name =" code_language "content =" Visual Basic 7.0 "> <meta name =" vs_defaultclientscript "content =" JavaScript "> <meta name =" vs_targetschema "content = "http://schemas.microsoft.com/intellisense/ie5">

Excelexport. aspx. VB

Public class excelexport <br/> inherits system. web. UI. page <br/> protected withevents datagrid1 as system. web. UI. webcontrols. dataGrid <br/> # region "code generated by web forms designer" <br/> 'This call is required by web forms designer. <Br/> <system. diagnostics. debuggerstepthrough> private sub initializecomponent () <br/> end sub <br/> private sub page_init (byval sender as system. object, byval e as system. eventargs) _ <br/> handles mybase. init <br/> 'codegen: This method is required by the web form designer. <br/> 'do not use the code editor to modify it. <Br/> initializecomponent () <br/> end sub <br/> # End Region <br/> private sub page_load (byval sender as system. object, byval e as system. eventargs) _ <br/> handles mybase. load <br/> 'user code that places the initialization page here <br/> 'defines whether the database is an SQL Server database. Here, it is false <br/> dim blnissqlserver as system. boolean = false <br/> dim strsql as string <br/> dim objdataset as new dataset () <br/> dim objconn as object <br/> dim strcnn as string <br/> If blnissqlserver then <br/> strcnn = "User ID = sa; initial catalog = northwind; data Source =. /netsdk; "<br/> objconn = new system. data. sqlclient. sqlconnection (strcnn) <br/> objconn. open () <br/> dim objadapter as new system. data. sqlclient. sqldataadapter () <br/> strsql = "select * from MERs where country = 'usa'" <br/> objadapter. selectcommand = new system. data. sqlclient. sqlcommand (strsql, objconn) <br/> objadapter. fill (objdataset) <br/> else <br/> strcnn = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ server. mappath ("test. mdb ") <br/> objconn = new system. data. oledb. oledbconnection (strcnn) <br/> objconn. open () <br/> dim objadapter as new system. data. oledb. oledbdataadapter () <br/> strsql = "select top 10 title from document" <br/> objadapter. selectcommand = new system. data. oledb. oledbcommand (strsql, objconn) <br/> objadapter. fill (objdataset) <br/> end if <br/> dim oview as new dataview (objdataset. tables (0) <br/> maid = oview <br/> maid () <br/> objconn. close () <br/> objconn. dispose () <br/> objconn = nothing <br/> If request. querystring ("bexcel") = "1" then <br/> response. contenttype = "application/vnd. MS-excel "<br/> 'removes charset settings from the Content-Type Header <br/> response. charset = "" <br/> 'Close viewstate <br/> me. enableviewstate = false <br/> dim TW as new system. io. stringwriter () <br/> dim HW as new system. web. UI. htmltextwriter (TW) <br/> 'get control HTML <br/> datagrid1.rendercontrol (HW) <br/> 'write HTML back to the browser <br/> response. write (TW. tostring () <br/> response. end () <br/> end if <br/> end sub <br/> end class <br/> </system. diagnostics. debuggerstepthrough>

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.