Excel 通过 ext.net 按钮下载文件
Excel file download via ext.net button
在我的应用程序中,我使用网络表单+ext.net、EPPlus。我想像这样在 Excel 文件中导出:
<ext:Button
runat="server"
Text="Экспорт в Excel"
Icon="PageExcel"
Padding="5">
<DirectEvents>
<Click OnEvent="ExportDataToExcel">
<EventMask ShowMask="true" Msg="Exporting..."></EventMask>
</Click>
</DirectEvents>
</ext:Button>
在我的 C# 代码中是这样的:
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();
}
}
这里,我只是把这个文件保存在服务器端。
但是当我尝试像这样下载这个文件时:
Response.BinaryWrite(package.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=export.xlsx");
我收到一个错误。我认为,问题出在 ext.net 事件中。有人可以帮我吗?
好的...解决方案很简单 - Why is the file not downloading?
如我们所见,我们只需要在我们的 aspx 文件事件 IsUpload="true".
中添加一个标志
在我的应用程序中,我使用网络表单+ext.net、EPPlus。我想像这样在 Excel 文件中导出:
<ext:Button
runat="server"
Text="Экспорт в Excel"
Icon="PageExcel"
Padding="5">
<DirectEvents>
<Click OnEvent="ExportDataToExcel">
<EventMask ShowMask="true" Msg="Exporting..."></EventMask>
</Click>
</DirectEvents>
</ext:Button>
在我的 C# 代码中是这样的:
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();
}
}
这里,我只是把这个文件保存在服务器端。
但是当我尝试像这样下载这个文件时:
Response.BinaryWrite(package.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=export.xlsx");
我收到一个错误。我认为,问题出在 ext.net 事件中。有人可以帮我吗?
好的...解决方案很简单 - Why is the file not downloading?
如我们所见,我们只需要在我们的 aspx 文件事件 IsUpload="true".
中添加一个标志