Reputation:
I have developed a SharePoint WebPart using ASP.NET and C# (code behind).
One of the things that needs to be done is to launch the Excel Save As dialog so that the user can save the Excel file.
On the SharePoint Server box, this functionality works correctly. When I click "Export", as expected the Excel "Save As" dialog appears.
However, from a client machine (such as mine), this functionality does not work correctly. When I click "Export", the Excel "Save As" dialog box does NOT appear.
Code snippet where this dialog is being launched…
xlApp.DisplayAlerts = false;
//xlWorkBook.Save();
Microsoft.Office.Interop.Excel.Dialog dialog = xlApp.Dialogs[Microsoft.Office.Interop.Excel.XlBuiltInDialog.xlDialogSaveAs];
dialog.Show(Type.Missing, // document_text
Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, // type_num
Type.Missing, // prot_pwd
Type.Missing, // backup
Type.Missing, // write_res_pwd
Type.Missing, // read_only_rec
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp.DisplayAlerts = true;
Do you know what could be causing this difference? Are there some particular things I should investigate?
Thanks for your help, -Krishna
Upvotes: 1
Views: 2707
Reputation: 5160
You must understand that when you use the above code on a web server, you are running Excel on the web server, not on the client machine.
You cannot do what you are trying to do /w Interop in a client/server environment. Generally, you should not be using COM Interop /w Office components on a web server, ever.
You should build your Excel file using a method that is safer for servers than Interop. I've used XMLSS in the past (I bet there is an even more current XML format for Office):
http://msdn.microsoft.com/en-us/library/aa140066(v=office.10).aspx
but Excel can even open HTML tables so depending on what you're trying to do, you may be able to write HTML to the client and give it a .xls extension.
If you can't avoid using Interop (though I strongly advise that you avoid it), then what you need to do is make sure your Interop code is thread safe and reliable, and have it save the Excel file to a local spot on the server, then force the client browser to "Save As". You'll also want a mechanism to clean up the old files after some time. It is preferable to do it all in memory and stream the result to the client. The following link contains info on how to force a "Save As".
http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET
Upvotes: 1