Search code examples
c#asp.netepplusext.net

Excel file download via ext.net button


In my app I use web forms+ext.net, EPPlus. I want to make export in Excel file like this:

<ext:Button
                                runat="server"
                                Text="Экспорт в Excel"
                                Icon="PageExcel"
                                Padding="5">
                                <DirectEvents>
                                    <Click OnEvent="ExportDataToExcel">
                                        <EventMask ShowMask="true" Msg="Exporting..."></EventMask>                                           
                                    </Click>
                                </DirectEvents>
                            </ext:Button>

In my C# code it is like this:

private void CreateExcelFile(JournalEventItemDto[] data)
    {
        var tempFolderPath = Path.GetTempPath();
        var filePath = tempFolderPath + "export.xlsx";

        FileInfo newFile = new FileInfo(filePath);
        if (newFile.Exists)
        {
            try
            {
                newFile.Delete();
                newFile = new FileInfo(filePath);
            }
            catch (IOException)
            {
                X.Msg.Alert("Error!", "File is opened in another program").Show();
                return;
            }
        }

        using (ExcelPackage package = new ExcelPackage(newFile))
        {
            var workSheet = package.Workbook.Worksheets.Add("Export");

            DataTable dataTable = new DataTable { Locale = CultureInfo.CurrentCulture };

            dataTable.Columns.Add("FullName", typeof(string));
            dataTable.Columns.Add("GuestName", typeof(string));
            dataTable.Columns.Add("Email", typeof(string));
            dataTable.Columns.Add("Date", typeof(string));
            dataTable.Columns.Add("Event", typeof(string));
            dataTable.Columns.Add("Page", typeof(string));
            dataTable.Columns.Add("Type", typeof(string));

            foreach (var item in data)
            {
                dataTable.Rows.Add(item.UserFullName, item.GuestName, item.Email,
                    item.EventDate.ToString("dd.MM.yyyy HH:mm:ss"), item.EventTypeText, item.EventPlaceText, item.ObjectTypeText);
            }

            workSheet.Cells["A1"].LoadFromDataTable(dataTable, true);
            workSheet.Cells.AutoFitColumns(0);
            package.Save();                                               
        }

        X.Msg.Alert("", "Successful export in " + filePath).Show();
    }
}

Here, I just save this file on a server side.

But when I try to download this file like this:

Response.BinaryWrite(package.GetAsByteArray());
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=export.xlsx");

I get an error. I think, that problem is in the ext.net event. Can somebody help me?

Awfull error


Solution

  • O'k... solution is easy - Why is the file not downloading?

    As we can see, we just need to add a flag to our aspx file event IsUpload="true".