如何将Excel文件下载到用户的下载文件夹中? [英] How can I download an Excel file to the user's download folder?

查看:196
本文介绍了如何将Excel文件下载到用户的下载文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#,asp.net 4.0项目中,我已经使用Microsoft.Office.Interop.Excel创建了一个Excel文件.正确创建了文件,并将其放置在服务器上的文件夹中.但是,我一直无法提出一种向客户端显示文件的方法.有人可以帮我吗?

In a C#, asp.net 4.0 project, I've created an Excel file using Microsoft.Office.Interop.Excel. The file gets created correctly and is placed in a folder on the server. However, I've been unable to come up with a way to display the file to the client. Can someone help me?

某些背景:我正在尝试解决阻止Excel文件打开的Microsoft更改.我们的Web应用程序使用Telerik网格,并使用ExportToExcel函数将该网格导出到Excel文件.该文件将下载(转到用户的下载文件),但是当用户尝试打开它时,他们将获得一个空白的Excel应用程序.有一些解决方法,例如卸载造成此问题的补丁程序,关闭Excel安全选项以及单击文件属性中的取消阻止;但是,我们的客户不想执行任何这些操作.因此,我正在重写40多个网格的导出.

Some background: I'm trying to work around the Microsoft change that is blocking Excel files from opening. Our web application uses a Telerik grid and that grid is exported to an Excel file using the ExportToExcel function. The file downloads (goes to the user's downloads file), but when the user tries to open it, they get a blank Excel application. There are work arounds such as uninstalling the patch that created this problem, turning off Excel security options, and clicking unblock in the file's properties; however, our client doesn't want to do any of these. So I'm rewriting exports for 40+ grids.

我将radGrid中的数据保存到数据表中,并使用以下有效代码将其写入Excel:

I'm got the data from the radGrid to a datatable and written it to Excel using this code which works:

            Microsoft.Office.Interop.Excel.Application m_objExcel = null;
            Microsoft.Office.Interop.Excel.Workbooks m_objBooks = null;
            Microsoft.Office.Interop.Excel._Workbook m_objBook = null;
            Microsoft.Office.Interop.Excel.Sheets m_objSheets = null;
            Microsoft.Office.Interop.Excel._Worksheet m_objSheet = null;
            object m_objOpt = System.Reflection.Missing.Value;

            m_objExcel = new Microsoft.Office.Interop.Excel.Application();
            m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks;
            m_objBook = (Microsoft.Office.Interop.Excel._Workbook)(m_objBooks.Add(m_objOpt));

            m_objSheets = (Microsoft.Office.Interop.Excel.Sheets)m_objBook.Worksheets;
            m_objSheet = (Microsoft.Office.Interop.Excel._Worksheet)(m_objSheets.get_Item(1));

            int colcount = 1;
            foreach (DataColumn col in dt.Columns)
            {
                m_objSheet.Cells[1, colcount] = col.ColumnName;
                colcount++;
            }

            int rowcount = 2;
            foreach (DataRow row in dt.Rows)
            {
                for (int i = 1; i < dt.Columns.Count; i++)
                {
                    m_objSheet.Cells[rowcount, i] = row[i - 1].ToString();
                }
                rowcount++;
            }

            string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");

            m_objBook.SaveAs("C:\\Temp\\MD" + currentDateTime + ".xlsx");
            m_objBook.Close();

            m_objExcel.DisplayAlerts = false;
            m_objExcel.Quit();

当我尝试让用户下载文件时,出现了我的问题.我尝试了下面的代码,但收到一条错误消息:该进程无法访问文件'C:\ Temp \ MD20160802161458.xlsx',因为该文件正在被另一个进程使用."谁能解释在Excel创建文件后如何取消阻止文件,或者向我展示将文件下载给用户的另一种方法?

My problem comes when I try to get the user to download the file. I tried the code below, but I get an error saying "The process cannot access the file 'C:\Temp\MD20160802161458.xlsx' because it is being used by another process." Can anyone explain how to either unblock the file after it's created by Excel or show me another way to download the file to the user?

 string fileName = "C:\\Temp\\MD" + currentDateTime + ".xlsx", myStringWebResource = null;
 WebClient myWebClient = new WebClient();
 myStringWebResource = fileName;
 myWebClient.DownloadFile(myStringWebResource, fileName);

我还尝试了下面的代码打开Excel文件.该文件是在服务器上创建的,但是永远不会打开.当我尝试在服务器上打开文件时,Excel崩溃.我怀疑这是因为我的开发机上有Excel 2013,服务器上有Excel 2007.这引起了另一个问题,因为我不能保证最终生产服务器上将使用哪个版本的Excel.任何建议,将不胜感激.

I also tried the code below to open the Excel file. The file is created on the server, but it never opens. When I try to open the file on the server, Excel crashes. I suspect this is because I have Excel 2013 on my development machine and Excel 2007 is on the server. This raises another issue because I can't guarantee what version of Excel would be on eventual production server. Any suggestions would be appreciated.

            var excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = true;
            excelApp.Workbooks.Open("C:\\Temp\\MD" + currentDateTime + ".xlsx");
            m_objExcel.Quit();

推荐答案

只是跟进了DVK的答案,除了使用Excel互操作之外,我什么都没有问题,但是如果您不需要生成较旧的.XLS文件,并且我建议使用OpenXML SDK(免费)来处理.XLSX文件.在线上有许多使用该示例制作Excel工作簿的示例.这是一个:

Just following up on DVKs answer, I had nothing but problems using the Excel interop stuff, but if you don't need to produce older .XLS files and are fine with the .XLSX files I suggest using the OpenXML SDK (its free). There are a number of examples online on producing Excel workbooks using it. Here is one:

http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK

这篇关于如何将Excel文件下载到用户的下载文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆