简体   繁体   English

下载生成的Excel文件

[英]Download generated excel file

Following code given: 给出以下代码:

Microsoft.Office.Interop.Excel.Application excelFile = new Microsoft.Office.Interop.Excel.Application();
excelFile.Visible = false;
Workbook wb = excelFile.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet sheet1 = wb.ActiveSheet as Worksheet;
sheet1.Name = "Test";
sheet1.Cells[1, 1] = "Test";

string fileName = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory) + "\\tickets.xlsx";
wb.SaveAs(Filename: fileName, FileFormat: XlFileFormat.xlOpenXMLWorkbook, AccessMode: XlSaveAsAccessMode.xlNoChange);
wb.Close();
excelFile.UserControl = true;
excelFile.Quit();

This generates an excelfile and saves it to the desktop. 这将生成一个excelfile并将其保存到桌面。 What do I have to change to ask for a save location? 要更改保存位置,我需要更改什么?

Using excel on the server is not supported and opens a whole can of worms, especially the is a high risk that excel pops up a dialog, which cannot be dismissed because no one sees the server desktop. 不支持在服务器上使用excel并打开一大堆蠕虫,尤其是excel弹出对话框的风险很高,由于没有人看到服务器桌面,因此无法消除该对话框。 Also, excel is very slow, generating a critical bottleneck. 而且,excel非常慢,产生了严重的瓶颈。 Last, debugging this is nearly impossible - this solution works, but will never work well. 最后,调试几乎是不可能的-此解决方案有效,但永远无法正常工作。

The solution: use a library like epplus which can read / write xlsx files easily, is faster to develop, magnitudes faster in building the file and free. 解决方案:使用诸如epplus之类的库,该库可以轻松读取/写入xlsx文件,开发速度更快,构建文件的速度更快并且免费。 There are other libraries out there which can read xls files, if needed. 如果需要,还有其他库可以读取xls文件。

在设置文件名之前,您可以打开SaveFileDialog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM