Showing posts with label Import Data from Excel File to Asp.Net Mvc View and Read File. Show all posts
Showing posts with label Import Data from Excel File to Asp.Net Mvc View and Read File. Show all posts

Tuesday, September 15, 2015

Import Data from Excel File to Asp.Net Mvc View and Read File


                                    @using (Html.BeginForm("viewname", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
                                    {
                                        <div class="upload">
                                            @*<span class="btn btn-file">Upload<input type="file" /></span>
                                            <label class="btn btn-success">Fill Down columnM</label>*@
                                            <input type="file" class="btn btn-file" name="file" id="file" />
                                            <input type="submit" class="btn btn-success" value="Hiiiiii" />
                                            @*<input type="submit" class="btn btn-success" href="Calculation">Fill Down column </input>*@
                                        </div>
                                        <div class="row">
                                          
                                            <table id="tablepaging" class="table table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Id </th>
                                                        <th>Fname</th>
                                                        <th>Lname </th>

                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @{
                                        if (ViewData["Excel"] != null)
                                        {
                                            System.Data.DataTable error = (System.Data.DataTable)ViewData["Excel"];
                                            int count = error.Rows.Count;
                                            for (int i = 0; i < count; i++)
                                            {
                                                System.Data.DataRow errorRow = error.Rows[i];
                                                int x = i % 2;
                                                if (x == 0)
                                                {
                                                    <tr class="even">
                                                        <td>@errorRow["Id"]</td>
                                                        <td>@errorRow["Fname"]</td>
                                                        <td>@errorRow["Lname"]</td>

                                                    </tr>
                                                }
                                                else
                                                {
                                                    <tr class="odd">
                                                        <td>@errorRow["Id"]</td>
                                                        <td>@errorRow["Fname"]</td>
                                                        <td>@errorRow["Lname"]</td>
                                                    </tr>
                                                }
                                            }
                                        }
                                                    }
                                                </tbody>
                                            </table>

                                            <div class="row">
                                                <div class="col-xs-6 col-sm-3">

                                                    <div class="form-group">
                                                        <a type="submit" class="btn btn-primary" href="Calculation">Save </a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                   

                                    }
==================================
[HttpPost]
        [Authorize]
        public ActionResult Naresh(HttpPostedFileBase file)
        {
            string savedFileName = "";
            var r = new List<solutionname.model.ViewData>();
            foreach (string files in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[files] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;

                savedFileName = Path.Combine(Server.MapPath("~/Extensions"), Path.GetFileName(hpf.FileName));

                hpf.SaveAs(savedFileName); // Save the file

                r.Add(new CanPayApp.Models.ViewData()
                {
                    Name = hpf.FileName,
                    Length = hpf.ContentLength,
                    Type = hpf.ContentType
                });
            }

            // string csv_file_path = @"+savedFileName";
            DataTable csvData = new DataTable();
            using (TextFieldParser csvReader = new TextFieldParser(savedFileName))
            {
               
                csvReader.SetDelimiters(new string[] { "," });
                csvReader.HasFieldsEnclosedInQuotes = true;
                //read column names
                string[] colFields = csvReader.ReadFields();
                foreach (string column in colFields)
                {
                    DataColumn datecolumn = new DataColumn(column);
                    datecolumn.AllowDBNull = true;
                    csvData.Columns.Add(datecolumn);
                }
                while (!csvReader.EndOfData)
                {
                    string[] fieldData = csvReader.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData.Length; i++)
                    {
                        if (fieldData[i] == "")
                        {
                            fieldData[i] = null;
                        }
                    }
                    csvData.Rows.Add(fieldData);
                }
            }
            ViewData["Excel"] = csvData;