我目前正在使用epPlus创建一个Web应用程序,以将当前数据库保存到excel文件中。这部分很容易并且有效,但我要做的是创建一个弹出对话框,允许客户端选择他们想要保存excel文件的目录。然后使用他们给我的路径,并使用epPlus中的SaveAs函数和他们选择的路径。
问题是如何让对话框工作并获取路径?我尝试过使用Response,但似乎无法使用它。问题是excel文件只是一个对象,直到保存完成,我需要路径来执行保存。想法?这是我的代码。
protected void OnbtnSaveExcelFileClick(object sender, EventArgs e)
{
String FileName = "GamingRecords";
String FilePath = @"C:\....\";
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();
try
{
using (var package = new ExcelPackage(flUploadLink.FileContent))
{
var worksheet = package.Workbook.Worksheets.Add("Games to Date - " + DateTime.Now.ToShortDateString());
worksheet.DefaultRowHeight = 22;
var headers = new[] { Constants.GameTitle, Constants.GameGenre, Constants.Price, Constants.Quantity };
for (var i = 1; i < headers.Count() + 1; i++)
worksheet.Cells[1, i].Value = headers[i - 1];
var game = new GameClass();
var list = game.FetchAll();
var rowNumber = 2;
foreach (var t in list)
{
worksheet.Cells[rowNumber, 1].Value = t.GameTitle;
worksheet.Cells[rowNumber, 2].Value = t.GameGenre;
worksheet.Cells[rowNumber, 3].Value = t.Price;
worksheet.Cells[rowNumber, 4].Value = t.Quantity;
rowNumber++;
}
for (var i = 1; i < worksheet.Dimension.End.Column; i++)
worksheet.Column(i).AutoFit();
package.Workbook.Properties.Title = "Games on Record";
package.Workbook.Properties.Author = "Kirk Rudzinski";
package.Workbook.Properties.Company = "Logistics+";
package.Save();
litExcelError.Visible = false;
}
}
catch (IOException)
{ litExcelError.Text = "Please close the file to make modifications"; }
}
答案 0 :(得分:0)
好吧,只是为了澄清我弄清楚如何按照我的方式去做。如果有人遇到这样的错误,请参考以下代码!
protected void OnbtnSaveExcelFileClick(object sender, EventArgs e)
{
try
{
using (var package = new ExcelPackage(flUploadLink.FileContent))
{
var worksheet = package.Workbook.Worksheets.Add("Games to Date - " + DateTime.Now.ToShortDateString());
worksheet.DefaultRowHeight = 22;
var headers = new[] { Constants.GameTitle, Constants.GameGenre, Constants.Price, Constants.Quantity };
for (var i = 1; i < headers.Count() + 1; i++)
worksheet.Cells[1, i].Value = headers[i - 1];
int rowNumber = 2;
foreach (GridViewRow row in grdGamingTable.Rows)
{
var index = row.RowIndex;
worksheet.Cells[rowNumber, 1].Value = grdGamingTable.Rows[index].Cells[2].Text;
worksheet.Cells[rowNumber, 2].Value = grdGamingTable.Rows[index].Cells[3].Text;
worksheet.Cells[rowNumber, 3].Value = Convert.ToInt16(grdGamingTable.Rows[index].Cells[4].Text);
worksheet.Cells[rowNumber, 4].Value = Convert.ToInt16(grdGamingTable.Rows[index].Cells[5].Text);
rowNumber++;
}
for (var i = 1; i < worksheet.Dimension.End.Column; i++)
worksheet.Column(i).AutoFit();
package.Workbook.Properties.Title = "Games on Record";
package.Save();
litExcelError.Visible = false;
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=GameRecords.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
}
}
catch (IOException)
{ litExcelError.Text = "Please close the file to make modifications"; }
}