Read the Excel content and display it on the webpage.

Source: Internet
Author: User
Class Code :

  /**/ ///   <Summary>
/// Reads data from an Excel table and returns the dataset.
///   </Summary>
///   <Param name = "FILENAME"> File Name </Param>
///   <Returns> Dataset </Returns>
Public Dataset getexceldata ( String Filename)
{
/**/ ///Establish a connection to read data in the table
String Strcon =   " Provider = Microsoft. Jet. oledb.4.0; Data Source = D: \ temp \\ "   + Filename +   " ; Extended properties = Excel 8.0 " ; /**/ ///Establish a connection. The address is the address passed by Str.
Oledbconnection myconn =   New Oledbconnection (strcon );
String Strcom =   " Select * from [sheet1 $] " ; /**/ ///SQL operation statement, that is, getting all data from sheet1
Myconn. open ();
Oledbdataadapter mycommand =   New Oledbdataadapter (strcom, myconn );
Dataset mydataset =   New Dataset (); /**/ ///Create a new dataset mydataset
Mycommand. Fill (mydataset, " [Sheet1 $] " ); /**/ ///Fill a dataset
Myconn. Close ();
// Columnnum = mydataset. Tables [0]. Columns. count; // /Read the number of columns and assign them to columnnum
// Rownum = mydataset. Tables [0]. Rows. count; // /Read the number of rows and assign the value to rownum
// Resultrownum = columnnum-1; // /Read the number of results rows (in fact, the number column in the first column is removed, and the results are displayed in a horizontal position in my program)
Return Mydataset;
}

Background code:

Protected   Void Btnshowexcel_click ( Object Sender, eventargs E)
{
Try
{
This . Fileupload1.saveas ( " D: \ temp \\ "   + Fileupload1.filename );
Dataset DS = LH. getexceldata (fileupload1.filename );
String Excelname = Fileupload1.filename;
Hiddenfield1.value = Excelname;
Gridview1.datasource = DS;
Gridview1.databind ();
Btnshowexcel. Enabled =   False ;
Btnaddexcel. Visible =   True ;
}
Catch
{
Response. Write ("<SCRIPT> alert ('check failed! Check whether the path is correct! '); </SCRIPT>");
}

}

Because you need to import data to the database in another button event, you can use the hiddenfield control to easily store a variable value.
This control is very handsome ~~~~~~~~
Shuang Shi Jie is doing image design next to me, think of "broken head ~~~~ Hey !!!
It's so happy to ask Lao Zhu for help if you have any problems.
Don't you have to worry about the issues above. I just want to decide when to get it ~~~~~~~~~

Really happy :)
If you have any questions, contact Baidu. Baidu does not contact Zhu.
Hey! It's much happier than the elder sister's design ~~~~

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.