示例#1
0
        public ActionResult LoadMonthMaster(FormCollection formCollection)
        {
            if (Request != null)
            {
                HttpPostedFileBase file = Request.Files["UploadedMonthFile"];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    string path = Server.MapPath("~/Content/Upload/" + file.FileName);
                    file.SaveAs(path);



                    Excel.Application xlApp;
                    Excel.Workbook    xlWorkBook;
                    Excel.Worksheet   xlWorkSheet;
                    Excel.Range       range;

                    string str;
                    int    rCnt;
                    int    cCnt;
                    int    rw = 0;
                    int    cl = 0;

                    xlApp       = new Excel.Application();
                    xlWorkBook  = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                    range = xlWorkSheet.UsedRange;
                    rw    = range.Rows.Count;
                    cl    = range.Columns.Count;

                    List <MonthMaster> monthMasters = new List <MonthMaster>();
                    for (rCnt = 1; rCnt <= rw; rCnt++)
                    {
                        if (rCnt > 1)
                        {
                            MonthMaster t = new MonthMaster();
                            t.TeluguMonth  = (string)(range.Cells[rCnt, 1] as Excel.Range).Value2;
                            t.EnglishMonth = (string)(range.Cells[rCnt, 2] as Excel.Range).Value2;
                            monthMasters.Add(t);
                        }
                    }

                    xlWorkBook.Close(true, null, null);
                    xlApp.Quit();

                    Marshal.ReleaseComObject(xlWorkSheet);
                    Marshal.ReleaseComObject(xlWorkBook);
                    Marshal.ReleaseComObject(xlApp);

                    _context.MonthMasters.AddRange(monthMasters);
                    _context.SaveChanges();
                }
            }
            return(View("Index"));
        }
 protected void Page_UnLoad(object sender, EventArgs e)
 {
     objCommonClass = null;
     objMonthMaster = null;
 }