Read and display excel content in ASP. NET

Source: Internet
Author: User

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
  1. // Upload button
  2. Protected void btnUp_Click (object sender, EventArgs e)
  3. {
  4. Bool B = Upload (fuExcel); // Upload an excel file
  5. If (! B)
  6. {
  7. Return;
  8. }
  9. String name = fuExcel. FileName;
  10. String filepath = Server. MapPath ("~ /Upload/") + name;
  11. DataSet ds = ExcelDataSource (filepath, ExcelSheetName (filepath) [0]. ToString ());
  12. GridView1.DataSource = ds;
  13. GridView1.DataBind ();
  14. }
  15. // File Upload Method
  16. Private bool Upload (FileUpload myFileUpload)
  17. {
  18. Bool flag = false;
  19. // Whether upload is allowed
  20. Bool fileAllow = false;
  21. // Set the extended file name type that can be uploaded
  22. String [] allowExtensions = {". xls "};
  23. // Obtain the website root directory path
  24. String path = HttpContext. Current. Request. MapPath ("~ /Upload /");
  25. // Check whether a file exists
  26. If (myFileUpload. HasFile)
  27. {
  28. // Get the extended File Name of the uploaded file and convert it to lowercase letters
  29. String fileExtension = System. IO. Path. GetExtension (myFileUpload. FileName). ToLower ();
  30. // Check whether the extension file name meets the specified type
  31. For (int I = 0; I <allowExtensions. Length; I ++)
  32. {
  33. If (fileExtension = allowExtensions [I])
  34. {
  35. FileAllow = true;
  36. }
  37. }
  38. If (fileAllow)
  39. {
  40. Try
  41. {
  42. // Store the file to a folder
  43. MyFileUpload. SaveAs (path + myFileUpload. FileName );
  44. LblMes. Text = "file imported successfully ";
  45. Flag = true;
  46. }
  47. Catch (Exception ex)
  48. {
  49. LblMes. Text + = ex. Message;
  50. Flag = false;
  51. }
  52. }
  53. Else
  54. {
  55. LblMes. Text = "Upload not allowed:" + myFileUpload. PostedFile. FileName + ", only xls files can be uploaded. Please check! ";
  56. Flag = false;
  57. }
  58. }
  59. Else
  60. {
  61. LblMes. Text = "select the excel file to import! ";
  62. Flag = false;
  63. }
  64. Return flag;
  65. }
  66. // 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.
  67. Public DataSet ExcelDataSource (string filepath, string sheetname)
  68. {
  69. String strConn;
  70. StrConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + filepath + "; Extended Properties = Excel 8.0 ;";
  71. OleDbConnection conn = new OleDbConnection (strConn );
  72. OleDbDataAdapter oada = new OleDbDataAdapter ("select * from [" + sheetname + "]", strConn );
  73. DataSet ds = new DataSet ();
  74. Oada. Fill (ds );
  75. Conn. Close ();
  76. Return ds;
  77. }
  78. // Obtain all sheetnames in Excel.
  79. Public ArrayList ExcelSheetName (string filepath)
  80. {
  81. ArrayList al = new ArrayList ();
  82. String strConn;
  83. StrConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + filepath + "; Extended Properties = Excel 8.0 ;";
  84. OleDbConnection conn = new OleDbConnection (strConn );
  85. Conn. Open ();
  86. DataTable sheetNames = conn. GetOleDbSchemaTable
  87. (System. Data. OleDb. OleDbSchemaGuid. Tables, new object [] {null, "TABLE "});
  88. Conn. Close ();
  89. Foreach (DataRow dr in sheetNames. Rows)
  90. {
  91. Al. Add (dr [2]);
  92. }
  93. Return al;
  94. }


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.

Related Article

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.