ASP. NET allows you to upload an Excel file, and asp.net uploads an excel file.

Source: Internet
Author: User

ASP. NET allows you to upload an Excel file, and asp.net uploads an excel file.

In the past few days, it has been easy to use to upload an Excel file and perform corresponding processing based on the data in the Excel file.

Resources used:

(1) NOPI 2.2.0.0 can download their own official website, you can also click: http://pan.baidu.com/s/1b1EMdg

(2) using some common file processing public method class, can be added to the project: http://pan.baidu.com/s/1bJpHuQ

If the above connection is unavailable for some reason, you can leave a comment in your mailbox and I will package it and send it in the past. If you have better suggestions, thank you for your guidance.

The prompt method ShowMsgHelper in the background can be rewritten as needed.

Front-end code:

<! DOCTYPE html> 

Background code;

Protected void ImpClick (object sender, EventArgs e) {try {# region check var fileName = this. fileUpload1.FileName; if (string. isNullOrWhiteSpace (fileName) {// The message ShowMsgHelper is displayed. alert ("Select Upload Excel file"); return ;}// obtain the extension name of the uploaded file if (! (FileName. indexOf (". xlsx ")> 0 | fileName. indexOf (". xls ")> 0) {ShowMsgHelper. alert ("the format of the uploaded file is incorrect. Check the format! "); Return ;}# endregion # region uploads the Excel file to the temporary folder on the server // Temporary Folder, under the root directory/Upload/tmp /, select string path = Server according to your configuration. mapPath ("~ /") +" Upload \ tmp \ "; string retStr = UploadHelper. FileUpload (path, this. FileUpload1); if (! RetStr. equals ("uploaded") {ShowMsgHelper. alert (retStr); return ;}# endregion # region reads the content of the first table in the Excel file and converts it to a DataTable. You can also add a timestamp to delete the temporary file, dataTable dt = this. excelToDataTable (path + this. fileUpload1.FileName, true); if (dt = null) {ShowMsgHelper. alert_Error ("failed to get"); return;} // example: Get the value string test = dt in dt. rows [0] ["name"]. toString (); string test2 = dt. rows [1] ["class"]. toString (); // Delete the temporary file dirfileel Per. deleteFile ("Upload \ tmp \" + fileName); # endregion} catch (Exception ex) {throw ex ;}} /// <summary> /// import excel to datatable /// </summary> /// <param name = "filePath"> excel path </param> // /<param name = "isColumnName"> whether the first row is a column name </param> // <returns> returns the datatable </returns> public DataTable ExcelToDataTable (string filePath, bool isColumnName) {DataTable dataTable = null; FileStream fs = null; DataCo Lumn column = null; DataRow dataRow = null; IWorkbook workbook = null; ISheet sheet = null; IRow row = null; ICell cell = null; int startRow = 0; try {using (fs = File. openRead (filePath) {// version 2007 if (filePath. indexOf (". xlsx ")> 0) workbook = new XSSFWorkbook (fs); // else if (filePath. indexOf (". xls ")> 0) workbook = new HSSFWorkbook (fs); if (workbook! = Null) {sheet = workbook. GetSheetAt (0); // read the first sheet. Of course, you can also read each sheet dataTable cyclically = new DataTable (); if (sheet! = Null) {int rowCount = sheet. lastRowNum; // The total number of rows if (rowCount> 0) {IRow firstRow = sheet. getRow (0); // The first line int cellCount = firstRow. lastCellNum; // Number of columns // if (isColumnName) {startRow = 1; // if the first row is a column name, read for (int I = firstRow. firstCellNum; I <cellCount; ++ I) {cell = firstRow. getCell (I); if (cell! = Null) {if (cell. StringCellValue! = Null) {column = new DataColumn (cell. stringCellValue); dataTable. columns. add (column) ;}}} else {for (int I = firstRow. firstCellNum; I <cellCount; ++ I) {column = new DataColumn ("column" + (I + 1); dataTable. columns. add (column) ;}// fill in the row for (int I = startRow; I <= rowCount; ++ I) {row = sheet. getRow (I); if (row = null) continue; dataRow = dataTable. newRow (); for (int j = row. firstCellNum; J <cellCount; ++ j) {cell = row. getCell (j); if (cell = null) {dataRow [j] = "";} else {// CellType (Unknown =-1, Numeric = 0, string = 1, Formula = 2, Blank = 3, Boolean = 4, Error = 5,) switch (cell. cellType) {case CellType. blank: dataRow [j] = ""; break; case CellType. numeric: short format = cell. cellStyle. dataFormat; // processing if (format = 14 | format = 31 | Format = 57 | format = 58) dataRow [j] = cell. dateCellValue; else dataRow [j] = cell. numericCellValue; break; case CellType. string: dataRow [j] = cell. stringCellValue; break ;}} dataTable. rows. add (dataRow) ;}}}return dataTable;} catch (Exception) {if (fs! = Null) {fs. Close ();} return null ;}}

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.