The project often uses the excel file content to be imported into the database. It just took some time to give an example. Basically, the Excel file can be imported and displayed, the imported excel file must be xls, that is, 2003.
The Code is as follows: the excel file to be read must be on the local hard disk, so generally, the Excel file on the hard disk is selected remotely and uploaded to the local server, then, perform operations on the local server. I posted an important part of the code behind the interface. Let's take a look at it and make comments.
C # code
- // Upload button
- Protected void btnUp_Click (object sender, EventArgs e)
- {
- Bool B = Upload (fuExcel); // Upload an excel file
- If (! B)
- {
- Return;
- }
- String name = fuExcel. FileName;
- String filepath = Server. MapPath ("~ /Upload/") + name;
- DataSet ds = ExcelDataSource (filepath, ExcelSheetName (filepath) [0]. ToString ());
- GridView1.DataSource = ds;
- GridView1.DataBind ();
- }
- // File Upload Method
- Private bool Upload (FileUpload myFileUpload)
- {
- Bool flag = false;
- // Whether upload is allowed
- Bool fileAllow = false;
- // Set the extended file name type that can be uploaded
- String [] allowExtensions = {". xls "};
- // Obtain the website root directory path
- String path = HttpContext. Current. Request. MapPath ("~ /Upload /");
- // Check whether a file exists
- If (myFileUpload. HasFile)
- {
- // Get the extended File Name of the uploaded file and convert it to lowercase letters
- String fileExtension = System. IO. Path. GetExtension (myFileUpload. FileName). ToLower ();
- // Check whether the extension file name meets the specified type
- For (int I = 0; I <allowExtensions. Length; I ++)
- {
- If (fileExtension = allowExtensions [I])
- {
- FileAllow = true;
- }
- }
- If (fileAllow)
- {
- Try
- {
- // Store the file to a folder
- MyFileUpload. SaveAs (path + myFileUpload. FileName );
- LblMes. Text = "file imported successfully ";
- Flag = true;
- }
- Catch (Exception ex)
- {
- LblMes. Text + = ex. Message;
- Flag = false;
- }
- }
- Else
- {
- LblMes. Text = "Upload not allowed:" + myFileUpload. PostedFile. FileName + ", only xls files can be uploaded. Please check! ";
- Flag = false;
- }
- }
- Else
- {
- LblMes. Text = "select the excel file to import! ";
- Flag = false;
- }
- Return flag;
- }
- // This method exports data from Excel to DataSet. The filepath is the absolute path of the Excel file, and the sheetname is the table name in the excel file.
- Public DataSet ExcelDataSource (string filepath, string sheetname)
- {
- String strConn;
- StrConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + filepath + "; Extended Properties = Excel 8.0 ;";
- OleDbConnection conn = new OleDbConnection (strConn );
- OleDbDataAdapter oada = new OleDbDataAdapter ("select * from [" + sheetname + "]", strConn );
- DataSet ds = new DataSet ();
- Oada. Fill (ds );
- Conn. Close ();
- Return ds;
- }
- // Obtain all sheetnames in Excel.
- Public ArrayList ExcelSheetName (string filepath)
- {
- ArrayList al = new ArrayList ();
- String strConn;
- StrConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + filepath + "; Extended Properties = Excel 8.0 ;";
- OleDbConnection conn = new OleDbConnection (strConn );
- Conn. Open ();
- DataTable sheetNames = conn. GetOleDbSchemaTable
- (System. Data. OleDb. OleDbSchemaGuid. Tables, new object [] {null, "TABLE "});
- Conn. Close ();
- Foreach (DataRow dr in sheetNames. Rows)
- {
- Al. Add (dr [2]);
- }
- Return al;
- }
Note that we need to create an upload folder under the root directory of the website from the very beginning, and set its permissions to readable and writable? This permission problem is too big, and I don't know how it should be done. The newly created folders in the XP system seem to be read-only. I right-click the attribute and remove the read-only folder, the results are read-only when they are re-viewed. However, it seems that it has no meaning to the program. After uploading an excel file, you can still read it.