我正在关注EPPlus上的https://support.microsoft.com/en-us/kb/116142教程,但是我仍然不知道如何将下载位置设置为登录用户“下载”文件夹?我知道我的位置就是这个,但是他们是一种分配它的方式,而不知道登录的人员帐户信息?
C:\ Users \用户膝上型\下载
这是我的语法:
string location = "C:\\";
string filename = "EPPlusTest.xlsx";
using (ExcelPackage objExcelPackage = new ExcelPackage())
{
ExcelWorksheet objWorksheet = objExcelPackage.Workbook.Worksheets.Add("Sheet 1");
objWorksheet.Cells["A1"].LoadFromDataTable(dataTable, true);
using (ExcelRange objRange = objWorksheet.Cells["A1:XFD1"])
{
objRange.Style.Font.Bold = true;
objRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
objRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
}
using (ExcelRange dataRange = objWorksheet.Cells["A2:XFD20"])
{
dataRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
dataRange.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
}
FileStream objFileStrm = File.Create(filename);
objFileStrm.Close();
File.WriteAllBytes(location + filename, objExcelPackage.GetAsByteArray());
}
答案 0 :(得分:1)
你正在搅拌苹果和橘子。 File.WriteAllBytes是System.IO的一部分,它旨在操作文件"本地",例如该应用程序写入本地硬盘驱动器。
Asp.net是网络,所以你需要做这样的事情:
using (ExcelPackage pck = new ExcelPackage())
{
var ws = pck.Workbook.Worksheets.Add("Demo");
ws.Cells[1, 2].Value = "Excel Test";
var fileBytes = pck.GetAsByteArray();
Response.Clear();
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Content-Disposition",
String.Format("attachment; filename=\"{0}\"; size={1}; creation-date={2}; modification-date={2}; read-date={2}"
, "temp.xlsx"
, fileBytes.Length
, DateTime.Now.ToString("R"))
);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(fileBytes);
Response.End();
}
(请点击我的帖子:Open ExcelPackage Object with Excel application without saving it on local file path)
这将通过浏览器触发下载,该浏览器将使用该用途的默认设置并为您提供所需内容。