Example #1
0
        public ActionResult ExcelImport(byte[] files)
        {
            //if (Request != null)
            //{
            //    HttpPostedFileBase file = Request.Files[0];

            //    if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
            //    {
            //        string fileName = file.FileName;
            //        string fileContentType = file.ContentType;
            //        byte[] fileBytes = new byte[file.ContentLength];
            //        var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
            //    }
            //}

            IGumruk iGumruk = new BSGumruk();

            if (Request != null)
            {
                List <ColumnToExcel> listCltoExcel = new List <ColumnToExcel>();
                HttpPostedFileBase   file          = Request.Files[0];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName        = file.FileName;
                    string fileContentType = file.ContentType;
                    byte[] fileBytes       = new byte[file.ContentLength];
                    var    data            = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                    var    usersList       = new List <d_b_columns>();
                    using (var package = new ExcelPackage(file.InputStream))
                    {
                        var currentSheet = package.Workbook.Worksheets;
                        var workSheet    = currentSheet.First();
                        var noOfCol      = workSheet.Dimension.End.Column;
                        var noOfRow      = workSheet.Dimension.End.Row;

                        string tableName;
                        string schemaName;
                        string columnName;
                        bool   allowNull;
                        string type;
                        string exp1;
                        string exp2;
                        bool   primary;
                        string lookup;
                        string link;
                        string formAlani;
                        string systemType;



                        for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                        {
                            schemaName = (workSheet.Cells[rowIterator, 2].Value == null) ? "" : workSheet.Cells[rowIterator, 2].Value.ToString();
                            tableName  = (workSheet.Cells[rowIterator, 3].Value == null) ? "" : workSheet.Cells[rowIterator, 3].Value.ToString();
                            columnName = (workSheet.Cells[rowIterator, 4].Value == null) ? "" : workSheet.Cells[rowIterator, 4].Value.ToString();
                            primary    = (workSheet.Cells[rowIterator, 5].Value == null) ? false : bool.Parse(workSheet.Cells[rowIterator, 5].Value.ToString());
                            allowNull  = (workSheet.Cells[rowIterator, 6].Value == null) ? false : bool.Parse(workSheet.Cells[rowIterator, 6].Value.ToString());
                            type       = (workSheet.Cells[rowIterator, 7].Value == null) ? "" : workSheet.Cells[rowIterator, 7].Value.ToString();
                            exp1       = (workSheet.Cells[rowIterator, 8].Value == null) ? "" : workSheet.Cells[rowIterator, 8].Value.ToString();
                            exp2       = (workSheet.Cells[rowIterator, 9].Value == null) ? "" : workSheet.Cells[rowIterator, 9].Value.ToString();
                            lookup     = (workSheet.Cells[rowIterator, 10].Value == null) ? "" : workSheet.Cells[rowIterator, 10].Value.ToString();
                            link       = (workSheet.Cells[rowIterator, 11].Value == null) ? "" : workSheet.Cells[rowIterator, 11].Value.ToString();
                            formAlani  = (workSheet.Cells[rowIterator, 12].Value == null) ? "" : workSheet.Cells[rowIterator, 12].Value.ToString();
                            systemType = (workSheet.Cells[rowIterator, 13].Value == null) ? "" : workSheet.Cells[rowIterator, 13].Value.ToString();

                            //veriler excel'den alındı. İlgili tablolara atılması gerekiyor.

                            ColumnToExcel cl = new ColumnToExcel();
                            cl.AllowNull    = allowNull.ToString();
                            cl.Explanation1 = exp1;
                            cl.Explanation2 = exp2;
                            cl.Field        = columnName;
                            cl.FormAlani    = formAlani;
                            cl.Link         = link;
                            cl.LookUp       = lookup;
                            cl.Module       = "";
                            cl.Primary      = primary.ToString();
                            cl.Schema       = schemaName;
                            cl.SystemType   = systemType;
                            cl.Table        = tableName;
                            cl.Type         = type;
                            cl.UserID       = GetCurrentUser().id;

                            listCltoExcel.Add(cl);
                        }
                    }

                    iGumruk.ExcelImport(listCltoExcel);
                }
            }

            return(View("Index"));
        }
Example #2
0
        public ActionResult TableExcelExport(double tableID, int isSchema)
        {
            IGumruk iGumruk = new BSGumruk();

            List <d_b_columns> columns1;
            //List<d_b_tables> tables;
            int tblID = (int)(tableID / 0.123123);

            if (!Convert.ToBoolean(isSchema))
            {
                columns1 = iGumruk.GetAllColumns(tblID);
            }
            else
            {
                columns1 = iGumruk.GetAllColumns(tblID);
            }

            string filename = columns1[0].d_b_tables.name + "_Columns.xlsx";

            List <ColumnToExcel> excelList = new List <ColumnToExcel>();

            foreach (var item in columns1)
            {
                ColumnToExcel cl = new ColumnToExcel()
                {
                    Module     = "",
                    Schema     = item.d_b_tables.d_b_schemas.name,
                    Table      = item.d_b_tables.name,
                    Field      = item.name,
                    Primary    = "False", // bu kolon yok
                    AllowNull  = item.isnull.ToString(),
                    Type       = item.d_b_column_types.name,
                    LookUp     = "",//şimdilik boş gönderiyorum. Doldurulacak.
                    FormAlani  = "",
                    SystemType = "",
                };

                foreach (var item2 in item.d_b_column_explanations)
                {
                    cl.Explanation1 = item2.details;
                    break;
                }

                if (item.d_b_column_explanations.Count > 1)
                {
                    for (int i = 1; i < item.d_b_column_explanations.Count; i++)
                    {
                        if (i != item.d_b_column_explanations.Count - 1)
                        {
                            cl.Explanation2 += item.d_b_column_explanations.ToList()[i].details + " & ";
                        }
                        else
                        {
                            cl.Explanation2 += item.d_b_column_explanations.ToList()[i].details;
                        }
                    }
                }


                foreach (var item3 in item.d_b_column_lookups)
                {
                    cl.LookUp = item3.d_b_Column_to.d_b_tables.d_b_schemas.name + "." + item3.d_b_Column_to.d_b_tables.name + "." + item3.d_b_Column_to.name;
                }

                excelList.Add(cl);
            }

            //excel e export kısmını yapan burası .
            System.Data.DataTable dt = ToDataTable <ColumnToExcel>(excelList);

            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));

            using (ExcelPackage pck = new ExcelPackage())
            {
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Columns");
                ws.Cells["A1"].LoadFromDataTable(dt, true);
                var ms = new System.IO.MemoryStream();
                pck.SaveAs(ms);
                ms.WriteTo(Response.OutputStream);
            }

            HttpContext.Response.Flush();
            HttpContext.Response.End();

            return(Content(""));
        }