Exemplo n.º 1
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(""));
        }