C # The winform program reads Excel

Source: Internet
Author: User
/**/ ///   <Summary>
/// Upload an Excel file to the server
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Protected   Void Btnupdatecomment_click ( Object Sender, eventargs E) {
// Step 1: upload an Excel file to the server
If (Fileupload1.hasfile) {
// Verify the Excel File Format
If (Fileupload1.filename. tolower (). indexof ( " . Xls " ) =   - 1 ) {
Requiredfieldvalidator1.errormessage =   " Invalid Excel File " ;
Requiredfieldvalidator1.isvalid =   False ;
Return ;
}

// Excel comment storage location
String _ Filepath =   String . Empty;
If (System. configuration. configurationmanager. receivettings [ " Commentexcelfile " ] ! =   Null ) {
_ Filepath=System. configuration. configurationmanager. configurettings ["Commentexcelfile"]. Tostring ();
}

// If this directory does not exist, create this directory
If ( ! System. Io. Directory. exists (_ filepath )) {
System. Io. Directory. createdirectory (_ filepath );
}

// Generate file name
String _ Filename = Datetime. Now. tostring ( " Yyyymmddhhmmss " ) +   " - "   + Datetime. Now. ticks. tostring (). substring ( 0 , 4 ) +   " . Xls " ;
_ Filename = _ Filepath +   @" \ "   + _ Filename;

// Save the file to the specified location on the server
Try   {
Fileupload1.saveas (_ filename );
Response. Redirect (String. Format ("Commentdetail. aspx? Filename = {0}", _ Filename ));
}   Catch (Exception exp) {
ThrowExp;
}
}
}

Read Excel # Region Read Excel
/**/ ///   <Summary>
/// Read Excel documents
///   </Summary>
///   <Param name = "path"> File Name </Param>
///   <Returns> Returns a dataset. </Returns>
///   Http://dev.csdn.net/article/72/72658.shtm
Public   Static Dataset exceltods ( String Path) {
If ( ! String . Isnullorempty (PATH )) {
String Strconn =   " Provider = Microsoft. Jet. oledb.4.0; "   +   " Data Source = "   + Path +   " ; "   +   " Extended properties = Microsoft Excel 8.0; " ;
Oledbconnection Conn =   New Oledbconnection (strconn );
Conn. open ();
String Strexcel =   "" ;
Oledbdataadapter mycommand =   Null ;
Dataset DS =   Null ;
Strexcel =   " Select * from [sheet1 $] " ;
Mycommand =   New Oledbdataadapter (strexcel, strconn );
DS =   New Dataset ();
Mycommand. Fill (DS );
Return DS;
}
Return   Null ;
}
# Endregion

Try   {
Dataset DS = Commutil. exceltods (_ filename );
If (DS ! =   Null
&& DS. Tables. Count >   0
&& DS. Tables [ 0 ]. Rows. Count >   0 ) {

//Omitted

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.