Exemplo n.º 1
0
        public void ToExcel(string url, string name, string stateName, string columnsOverride, bool?labelColumns, string xlsName = "", string controllerAssembly = "Concentrator.ui.Management.Controllers")
        {
            if (string.IsNullOrEmpty(controllerAssembly))
            {
                controllerAssembly = "Concentrator.ui.Management.Controllers";
            }
            var grid            = new System.Web.UI.WebControls.GridView();
            var addExtraColumns = labelColumns ?? false;

            if (string.IsNullOrEmpty(xlsName))
            {
                xlsName = name;
            }

            var jsonData = (((JsonResult)GetDataSource(url, controllerAssembly)).Data);
            var data     = jsonData.GetType().GetProperty("results").GetValue(jsonData, null);

            DatabaseStateColumnProvider provider = new DatabaseStateColumnProvider();
            var columns = provider.GetColumnDefinitions(Client.User.UserID, stateName, GetUnitOfWork());

            if (columnsOverride != null)
            {
                string[] values = columnsOverride.Split(',');

                foreach (var item in values)
                {
                    var stateColumns = columns.Where(c => columnsOverride.Contains(c.Header)).ToList();

                    columns = stateColumns;
                }
            }
            if (addExtraColumns)
            {
                DefaultColumnDefinition fromDateColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "fromDate", Header = "FromDate", Hidden = false, Width = 100
                };
                columns.Add(fromDateColumn);

                DefaultColumnDefinition toDateColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "toDate", Header = "toDate", Hidden = false, Width = 100
                };
                columns.Add(toDateColumn);

                DefaultColumnDefinition labelColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "Label", Header = "Label", Hidden = false, Width = 100
                };
                columns.Add(labelColumn);

                DefaultColumnDefinition priceColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "Price", Header = "Price", Hidden = false, Width = 100
                };
                columns.Add(priceColumn);

                DefaultColumnDefinition productIDColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "ProductID", Header = "ProductID", Hidden = false, Width = 100
                };
                columns.Add(productIDColumn);

                DefaultColumnDefinition vendorIDColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "VendorID", Header = "VendorID", Hidden = false, Width = 100
                };
                columns.Add(vendorIDColumn);

                DefaultColumnDefinition connectorIDColumn = new DefaultColumnDefinition()
                {
                    DataIndex = "ConnectorID", Header = "ConnectorID", Hidden = false, Width = 100
                };
                columns.Add(connectorIDColumn);
            }
            var excelProvider = new ExcelProvider(columns, data, xlsName);

            var excel = excelProvider.GetExcelDocument();

            Response.Clear();
            Response.ClearContent();

            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xlsx", name));
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.BinaryWrite(excel);
            Response.End();
        }