Wednesday, 28 December 2016

upload excel file without saving any where in system in mvc, asp ,vb.net

Using ClosedXML and Excel.4.5 dll.

//Install-Package ClosedXML -Version 0.76.0
//https://www.nuget.org/packages/ClosedXML/0.76.0

//Install-Package ExcelDataReader -Version 2.1.2.3
//https://www.nuget.org/packages/ExcelDataReader/2.1.2.3

Html:
============
 <tr>
                                        <td style="width: 112px;">@common.GetTranslations("File Name", LID)</td>
                                        <td>@Html.TextBox("Dfilename", "", new { type = "file", Class = "Dtext tht fs fel" })</td>
                                        <td><input type="submit" value="@common.GetTranslations("Upload file", LID)" id="btnuploadexcel" class="formBtn" onclick="Loader();" style="min-width: 130px !important;  padding: 3px 0px !important; background-color:cadetblue;   font-size: 15px !important;" /></td>
                                    </tr>

C# code
===============

using ClosedXML.Excel;
using Excel;
   public ActionResult Diamondfile(HttpPostedFileBase Dfilename)
        {
if (Dfilename != null && Dfilename.ContentLength > 0)
            {
                string Extension = Path.GetExtension(Dfilename.FileName);

                if (!(Extension == "csv"))
                {
                    #region Excel File
                    var attachedFile = Dfilename;
                    IExcelDataReader excelReader;
                    if (Extension.ToLower() == ".xls")
                        excelReader = ExcelReaderFactory.CreateBinaryReader(attachedFile.InputStream);
                    else
                        excelReader = ExcelReaderFactory.CreateOpenXmlReader(attachedFile.InputStream);
                   //In asp.net or vb.net 
                   // excelReader = ExcelReaderFactory.CreateBinaryReader(attachedFile.FileContent)
                    //Else
                   // excelReader = ExcelReaderFactory.CreateOpenXmlReader(attachedFile.FileContent)                  



  excelReader.IsFirstRowAsColumnNames = true;
                    DataSet result = excelReader.AsDataSet();
                    System.Data.DataTable dtResult = result.Tables[0];
                    uploadexcelfile(dtResult);


                    #endregion
                }            
                else
                {
                  //code here csv file
                }
            }
            else
            {
                TempData["Fileupload"] = "no";
            }
            
            return View();
}
=================================


    public void uploadexcelfile(System.Data.DataTable dtResult)
        {
            if (dtResult.Rows.Count > 0)
            {
                string inputDataRead;
                string StockNumbers = string.Empty;
                string Stockexist = string.Empty;

                foreach (DataRow eachValue in dtResult.Rows)
                {           
                    //code here
                }
            }
            else
            {
                TempData["Fileupload"] = "no";
               
            }
            
        }
==============================



No comments:

Post a Comment