如何从 ASP.NET 应用程序下载生成的 Excel 文件
我正在构建一个 asp.net 网站,并且正在基于一些 gridview 数据构建一个 Excel 文档。我正在使用 Microsoft.Office.Interop.Excel 来构建它。我尝试使用 System.Windows.Forms
来使用 saveFileDialog
框。通过我的在线研究,我了解到实际上不能在 ASP.NET 应用程序中执行此操作?在调试模式下一切都工作得很好,但是将其上传到站点时,该页面根本无法工作。所以我的问题是,是否可以为 ASP.NET 应用程序使用 saveFileDialog
框?有谁知道一个好的解决方法吗?我将发布我的代码,该代码在调试模式下工作得很好,但当我将其上传到我的网站时却不起作用。预先感谢您的任何帮助。
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
public void someEvent()
{
var t = new Thread(SaveFolder);
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public void SaveFolder()
{
saveFileDialog1.Filter = "Sources (*.xls, *.xlsx)|*.xls*;*.xlsx";
saveFileDialog1.ShowDialog();
exportReport();
}
public void exportReport()
{
xlWorkBook.SaveAs(@saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
}
public void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
I am building an asp.net website and I am constructing a Excel document based on some gridview data. I am using Microsoft.Office.Interop.Excel
to construct it. I have tried to use a saveFileDialog
box using System.Windows.Forms
. Through my research online, I have learned that you can’t actually do this in an asp.net application? Everything works great in debugging mode, but when uploading it to the site, the page doesn't work at all. So my man question is, is it possible to to use a saveFileDialog
box for an asp.net application? Does anyone know a good workaround for this? I will post my code that works great in debugging mode, but doesn't work when I upload it to my site. Thanks in advance for any help.
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
public void someEvent()
{
var t = new Thread(SaveFolder);
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public void SaveFolder()
{
saveFileDialog1.Filter = "Sources (*.xls, *.xlsx)|*.xls*;*.xlsx";
saveFileDialog1.ShowDialog();
exportReport();
}
public void exportReport()
{
xlWorkBook.SaveAs(@saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
}
public void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法在服务器上显示对话框。那里没有人能看到它。摆脱整个 saveFileDialog 对象,只需使用服务器文件系统中生成的文件名调用 SaveAs,该文件名保证是唯一的。然后将该文件传输给您的用户。
You can't show a dialog box on the server. There's nobody there to see it. Get rid of the whole saveFileDialog object and just call SaveAs with a generated filename in the server's file system that is guaranteed to be unique. Then transmit that file to your user.
您无法使用保存对话框执行您想要的操作。您应该将文件保存到服务器上的临时位置。将文件保存到某处后,您可以强制用户下载该文件。我通常遵循此代码(似乎在所有浏览器中工作得最一致)。您必须提供
filename
和yourFileByteArray
变量。有多种方法可以将文件作为字节数组获取,但我将其作为练习留给您。
You can't do what you want to with the save dialog. You should just save the file to a temporary location on your server. Once you have the file saved somewhere, you can force the file at the user for download. I usually follow this code (seems to work in all browsers the most consistently). You have to supply the
filename
andyourFileByteArray
variables.There are several ways to get your file as a byte array, but I'll leave that as an exercise for you.
不要使用互操作。建议混合使用 HtmlTextWriter、StringWriter 和 GridView。
Don't use Interop. Suggest using a mix of HtmlTextWriter, StringWriter, and your GridView.