C# 询问保存下载文件的位置
我正在编写在本地系统中下载excel文件的代码C# 询问保存下载文件的位置,c#,asp.net,export-to-excel,C#,Asp.net,Export To Excel,我正在编写在本地系统中下载excel文件的代码 Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks workBooks = excelApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workBook
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workBooks = excelApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workBook = workBooks.Open(@"D:\Myfile.xlsx");
// Microsoft.Office.Interop.Excel.Worksheet workSheet = workBook.Worksheets.get_Item(1);
workBook.SaveCopyAs(@"D:\Copy_Myfile.xlsx");
workBook.Close();
现在的要求不是将文件保存到D驱动器,而是要求用户将文件保存到指定位置
有什么想法吗?也许可以通过文件浏览对话框询问他们?您的意思是,用户应该从服务器下载文件并保存到本地驱动器?与
响应类似。传输文件
?
public static void TransmitFile(Page CurrentPage, string FilePath)
// CurrentPage = this when called from CodeBehind
{
if (File.Exists(FilePath))
{
string FileName = Path.GetFileName(FilePath);
string Extension = Path.GetExtension(FilePath).ToLowerInvariant();
string ContentType;
switch (Extension)
{
case "xlsx":
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "xls":
ContentType = "application/vnd.ms-excel";
break;
case "csv":
ContentType = "text/csv";
break;
default:
ContentType = "application/octet-stream";
break;
}
if (CurrentPage != null)
{
CurrentPage.Response.ContentType = ContentType;
CurrentPage.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
CurrentPage.Response.TransmitFile(FilePath);
CurrentPage.Response.End();
}
else
throw new Exception("File transmission failed: cannot access current page");
}
else
throw new Exception("File transmission failed: file not found");
}