Thursday 3 August 2017

Excell manupulation in dot net

CONTROLLER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Office.Interop.Excel;
using System.IO;

namespace  ABC.Controllers
{
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Fetchexcelfile()
        {
            try
            {
                var file = Request.Files[0];
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/ExcelFileFolder/"), fileName);
                if (!System.IO.File.Exists(path))
                {
                    file.SaveAs(path);
                }
                int index = 0, Tr = 0, Pv = 0, Fopv = 0;
                string cvalue = string.Empty;
                Range objRange = null;
                Application app = new Application();
                Workbook WB = app.Workbooks.Open(path);
                List<string> listMT = new List<string>();
                foreach (Worksheet obj in WB.Worksheets)
                {
                    if (obj.Name.Substring(0, 2) == "MT")
                    {
                        for (index = 1; index < obj.Columns.Count; index++)
                        {
                            objRange = obj.Cells[1, index];
                            cvalue = objRange.Text;
                            string[] tvalue = cvalue.Split('\n');
                            cvalue = tvalue[0].Trim() + " " + tvalue[1].Trim();
                            if (cvalue.Trim() == "T R".Trim())
                                Tr = index;
                            if (cvalue.Trim() == "P V".Trim())
                                Pv = index;
                            if (cvalue.Trim() == "F O P V".Trim())
                            {
                                Fopv = index;
                                break;
                            }
                        }
                        listMT.Add("T r : " + ((Range)obj.Cells[3, Tr]).Text + "  Iv : " + ((Range)obj.Cells[3, Pv]).Text + "  O v : " + ((Range)obj.Cells[3, Fopv]).Text);
                    }
                }
                return Json(listMT, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }


        }

       
    }

}

View

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="@Url.Content("~/Content/sd-common-style.css")" rel="stylesheet">
<div class="well" style="height:600px">
    <div class="container">`
        <p class="section_title">Convert Excell to XML</p>
        <form class="form-horizontal" enctype="multipart/form-data">
            <fieldset>
                <legend></legend>
                <div class="form-group">
                    <label class="col-sm-3 abc">Excel File Upload</label>
                    <div class="col-sm-5">
                        <input type="file" id="Upload" value="Upload" class="form-control"/>
                        <span id="errorMessage" style="color:#a94442"></span>
                    </div>
                    <div class="col-sm-4">
                        <input type="button" id="btnUpload" value="Upload" class="sd-btn sd-btn-default disabled" />
                    </div>
                </div>
            </fieldset>
            <fieldset class="fields">
                <legend></legend>
                <div class="form-group">
                    <label class="col-sm-3 abc">Product</label>
                    <div class="col-sm-9">
                        <span class="cbGroup"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3"></label>
                    <div class="col-sm-9">
                        <input type="button" id="btnUpload" value="Convert Into XML" class="sd-btn sd-btn-default disabled" />
                    </div>
                </div>
            </fieldset>
        </form>
    </div>
</div>

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(function () {

        $('#Upload').change(function () {
            if ($('#Upload').val() == "") {
                $('#errorMessage').text('Please select a file');
                return false;
            }
            else {
                $('#errorMessage').text('');
            }
        }); $('.fields').hide();
        $('#btnUpload').click(function () {
            if ($('#Upload').val() == "") {
                $('#errorMessage').text('Please select a file');
                return false;
            }
            else {
                $('#errorMessage').text('');
            }
            var formData = new FormData();
            var file = document.getElementById("Upload").files[0];
            formData.append("rcexcel", file);
            $('.cbGroup').empty();
            $('.fields').hide();
            $.ajax({
                type: "POST",
                url: '/EXCELLTOXML/Fetchexcelfile',
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (data) {
                    $('.fields').show();
                    $.each(data, function (i, j) {
                        $('.cbGroup').append("<input type='checkbox' class=\"checkbox\"/>  " + j + "<br>");
                        $('.cbGroup').addClass("checkbox");
                    });
                 
                },
                error: function (error) {
                    alert("errror");
                }
            });
        });
    });
</script>

No comments:

Post a Comment