In the development application, it is often used to import xls files to the database for tutorials. Let's look at this program. If you need it, you can download it.
Using system;
Using system. configuration;
Using system. data;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system.web.ui.html controls;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system. io; public partial class _ default: system. web. ui. page
{
Protected void page_load (object sender, eventargs e)
{}/// <Summary>
/// Upload an excel file
/// </Summary>
/// <Param name = "inputfile"> name of the uploaded Control </param>
/// <Returns> </returns>
Private string uploadxls(system.web.ui.htmlcontrols.html inputfile)
{
String orifilename = string. empty;
String uploadfilepath = string. empty;
String modifyfilename = string. empty;
String fileext = ""; // File Extension
Int filesize = 0; // File Size
Try
{
If (inputfile. value! = String. empty)
{
// Get the file size
Filesize = inputfile. postedfile. contentlength;
If (filesize = 0)
{
Throw new exception ("the size of the imported excel file is 0. Check whether the file is correct! ");
}
// Get the extension
Fileext = inputfile. value. substring (inputfile. value. lastindexof (".") + 1 );
If (fileext. tolower ()! = "Xls ")
{Throw new exception ("the file format you selected is incorrect. Only excel files can be imported! ");
}
// Path
Uploadfilepath = server. mappath ("~ /");
// New file name
Modifyfilename = system. guid. newguid (). tostring ();
Modifyfilename + = "." + inputfile. value. substring (inputfile. value. lastindexof (".") + 1 );
// Determine whether the directory exists
System. io. directoryinfo dir = new system. io. directoryinfo (uploadfilepath); if (! Dir. exists)
{
Dir. create ();
}
Orifilename = uploadfilepath + modifyfilename;
// Delete an object if it exists
If (file. exists (orifilename ))
{
File. delete (orifilename );
}
// Upload a file
Inputfile. postedfile. saveas (orifilename );
}
Else
{
Throw new exception ("select the excel file to import! ");}
}
Catch (exception ex)
{
Throw ex;
}
Return orifilename;
} Protected void btnimport_click (object sender, system. eventargs e)
{Try
{
String strfilename = this. uploadxls (this. comfile );
String strconn = "provider = microsoft. jet. oledb.4.0; data source =" + strfilename + "; extended properties = 'excel 8.0; hdr = no; imex = 1 '";
Oledbconnection conn = new oledbconnection (strconn );
Oledbdataadapter mycommand = new oledbdataadapter ("select * from [sheet1 $]", strconn );
Dataset mydataset = new dataset ();
Mycommand. fill (mydataset );
String str = "";
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Deletefile (strfilename );
}
} // <Summary>
/// Delete an object
/// </Summary>
/// <Param name = "filename"> name of the file to be deleted </param>
Private void deletefile (string filename)
{
If (filename! = String. empty & file. exists (filename ))
{
File. delete (filename );
}
}
}
Front-end code
<% @ Page language = "c #" default. asp tutorial x. cs "inherits =" _ default "%>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "<a href =" http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd "> http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd </a>">
<Html xmlns = "<a href =" http://www.w3.org/1999/xhtml "> http://www.w3.org/1999/xhtml </a>">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
Select the file to import: <input id = "fileexcel" style = "width: 300px "type =" file "size =" 42 "name =" filephoto "runat =" server "/>
<Asp: button id = "btnimport" text = "" css tutorial class = "button" runat = "server" onclick = "btnimport_click"> </asp: button> <br/>
<Asp: label id = "lblmessage" runat = "server" font-bold = "true" forecolor = "red"> </asp: label>
</Form>
</Body>
</Html>