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 ~~~~