Opening an Excel application client
I'm trying to use the COM interop assembly to export some data to an Excel workbook for an ASP.NET web application. This works fine in development, since the server and the client are the same machine. However, when deployed, this is not the case. The deployed version does not throw an exception, but it also does not open Excel on the local machine (since ASP.NET is executed server-side, this is pretty obvious).
How do I go about creating an Excel application on the client's machine?
You'll need to generate the excel file server side, then send the bytes to the client using the appropriate MIME type so that the client can figure out how to open it.
Something like this...
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.BinaryWrite(bytes);
where fileName
is the name you want for the file, and bytes
is the output from your excel generator in byte[]
form.
Note that this doesn't really force excel open or anything like that, it just presents the content to the client in the best format possible for the client to open it in excel. The user will have to click "open" and if they have excel installed, it will be the default application to open the file.
If the client doesn't have excel, or their mime types aren't mapped etc, it's not going to work. Without some crazy activex voodoo (which I don't recommend) you're not going to be able to "open excel for them".
DO NOT USE COM INTEROP!!!
There is a much better way: EPPlus
When opening excel on the server it will keep a copy of excel running. Horrible. EPPlus will let you create native excel 2007/2010 worksheets with charts and everything. And without having office installed on the server itself. We're using it, pretty slick.
If the clients don't have excel 2007 or better, they can download the compatibility pack from microsoft.
Sounds like a big potential security hole, if you ask me!
One approach might be to start an ActiveX Excel control? (ActiveX being just a marketing term for a COM object). I'm not familiar with ASP.NET, so I don't know how this would work over the server/client boundary.
A simpler, and safer approach would be to create a CSV file, and send it through standard http. The user could the open this in Excel. It would be fine for a single worksheet of data, but not for complex things with macros, formatting, etc.
链接地址: http://www.djcxy.com/p/35674.html下一篇: 打开Excel应用程序客户端