コード例 #1
0
        public JsonResult UploadFile()
        {
            DtoRisk dto = new DtoRisk();

            if (Request.Files.Count > 0)
            {
                try
                {
                    object[,] obj = null;
                    int noOfCol = 0;
                    int noOfRow = 0;
                    HttpFileCollectionBase file = Request.Files;
                    if ((file != null) && (file.Count > 0))
                    {
                        byte[] fileBytes = new byte[Request.ContentLength];
                        int    data      = Request.InputStream.Read(fileBytes, 0, Convert.ToInt32(Request.ContentLength));
                        using (ExcelPackage package = new ExcelPackage(Request.InputStream))
                        {
                            ExcelWorksheets worksheets = package.Workbook.Worksheets;
                            ExcelWorksheet  workSheet  = worksheets.Single(x => x.Name.Equals(dto.WorksheetName));
                            noOfCol = workSheet.Dimension.End.Column;
                            noOfRow = workSheet.Dimension.End.Row;
                            obj     = new object[noOfRow, noOfCol];
                            obj     = (object[, ])workSheet.Cells.Value;
                        }
                    }
                    return(Json(new { data = obj, row = noOfRow, col = noOfCol }, JsonRequestBehavior.AllowGet));
                }
                catch (Exception ex)
                {
                }
            }
            return(Json("", JsonRequestBehavior.AllowGet));
        }