Hie guys I am in a bit of a Problem here regarding Export to Excel in asp.net where I am Exporting the Webgrid data to the Excel Sheet and it is working fine on localHost but not on the main server even when it is deployed on the Main server.
I am using Microsoft.Office.Interop.Excel.Application to genrate excel sheet.
When I click on the button for genrating Excel on the main server I get the following error-
Failed to Export-Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the
following error: 80040154.**Class Not registered
I googled for the solution bbut couldnt found one which can solver my problem I read it somewhere that I need to install Excel on my Main server too to get this functionality working. Is this is the solution or is their any Plugin which can help In order to solve this problem and export to excel work on the MainServer..
Please help
I recommend you read server-side Automation of Office.
It is not good idea to acess excel data from main server. I would suggest you use Open XML instead.
You can also take a look at third parties tool's such as
EPPlus
Short answer is – yes it’s necessary.
Long answer is that this is not recommended practice. Your web server is run by network service or some other limited privileges account that doesn’t even have permissions to run excel.
Extending those privileges to running excel may result in reduced security and potential damage to your server is something bad gets into your excel document (such as malicious macro or something like that).
Link Kyle provided is an excellent resource to find out more about this.
If possible try to move to OpenXML or some other library.
Related
I frequently receive ok-only prompts while automating add-ins in Excel, stating that Microsoft Excel is waiting for another application to complete an OLE action Error. How to use Interop.dll to show a custom message and handle this error programmatically. Before receiving the promptenter link description here
While You possibly can catch these exceptions depending on your automation framework and do something with it - these type of errors typically comes from not closing the files properly when changing from one to another.
Make sure you dispose all that can be disposed and close all which can be closed in reverse order obviously and it should go away.
If You need more details do post your code for opening and closing looks like a bug there which will make it not coming up.
You do not technically have to use interop, could you consider using a more modern package for interacting with excel?
If you are using google drive / file stream then this may be the problem. Turn off "Real Time Presence in Microsoft Office" then the messages go away.
I am developing asp.net core application that downloads some excel files and executing macro on it.
Macro is performing some data validation and data formatting and generating new excel file. I need to save this file in a specific folder on the server.
I have hosted this application on centos 7 so obviously Microsoft interop service will not work. So, I have to execute macro using open office.
If anyone can help then it will be very much appreciated.
As you said, COM interop will not work on CentOS, so you probably want to use Java or Python instead.
However, it may work to do a C# system call and execute a Basic macro. An example from https://ask.libreoffice.org/en/question/150565/execute-macro-in-calc-from-terminal/:
soffice "macro:///Standard.Module1.Ora(2,3)" "/home/lim/Scrivania/Test.ods"
With this setup, much of the code will be in Basic. If you are not familiar with the language, a good place to start is http://www.pitonyak.org/oo.php.
We recently upgraded from Excel 2007 to Excel 2010, and we have found that existing code started failing.
Exception Message:
Office has detected a problem with this file. To help protect your
computer this file cannot be opened.
We have traced this to the line where we open the file
excelApp.Workbooks.Open
Even when opening the file manually, the Protected View Messagebox comes up.
How can we work arround this using C#.
Have a look at using Application.FileValidation Property (Excel) before your Open statement.
Returns or sets how Excel will validate files before opening them.
Read/write
Files that do not pass validation will be opened in a Protected View
window. If you set the FileValidation property, that setting will
remain in effect for the entire session the application is open.
You can set it to one of the enum values in MsoFileValidationMode Enumeration
msoFileValidationDefault
msoFileValidationSkip
if you set it to msoFileValidationSkip before the Open statement, it should bypass the check.
Something like
excelApp.FileValidation = MsoFileValidationMode.msoFileValidationSkip;
before the open statement.
Late to the game here, but this is a common annoyance: you need to define a 'Trusted Location'.
You're not the only developers encountering this problem when your code tries to open a spreadsheet file, and "Office has detected a problem with this file. To help protect your computer this file cannot be opened." is an extremely unhelpful error message.
Look up the Trusted Location code published by Daniel Pineault on DevHut.net in 2010:
DevHut code example: Trusted Location using VBScript
I'll get downvoted to hellandgone for posting VBA in a C# forum, so I'd better not post my implementation of Daniel's code (yes, I'm a VBA developer, bashing out VBA macros all day, not a real coder working with pointy things and curly braces). If you really want to see the VBA, it's in a reply to another post:
https://stackoverflow.com/questions/2962728/office-trusted-locations/28115700#28115700
I believe the proper salutation is 'Share and Enjoy'.
Do, please, put an acknowledgement of the original author, Daniel Pineault, if you reuse the code: it's been widely published without attribution on 'Expert' sites, and that's rather rude.
We had the same issue. Our SSIS package at SQL server uses Excel.Interop to parse files. One day we installed Office 2010 x64 on new server and for some files started getting error:
Office has detected a problem with this file. To help protect your computer this file cannot be opened.
At the same time, other servers work good. We found distinguish in versions of Excel: 14.04763.1000 doesn't work, but 14.0.7015.1000 works for us. The last version number belong to Office 2010 SP2. Eventually we downloaded SP2 and installed it,as result, the error has gone.
Is there any way to get the path of documents currently open in Microsoft Word and Microsoft Excel?
I know how I can fetch the PID and the window handle for the running instance of Excel or Word, but I have no idea how to get the path of the open document. Is it even possible?
I am thinking that an API would be nice, but I doubt it even exists. Perhaps some kind of plugin that sends information to my application is a possibility? (But if any other way is possible, I'd like to avoid having to install plugins everywhere.)
I would prefer to do it using C#, but anything that can be compiled into an executable is alright.
And just in case someone thinks this sounds fishy, it is not to be used for anything remotely illegal. It is for a data collection system at work.
There is an API called Office Interop... it is basically a COM based interface for MS Office... and can be used from C# .
For Excel you can get the current file including path via Application.ActiveWorkbook.FullName.
For Word you can get the current file including path via Application.ActiveDocument.FullName.
I'm trying to create an excel file from ASP.NET. I assume this file is created somewhere temporary right? How can I get this location?
What are you using to create the Excel file? Since you have an ASP.NET tag I hope you are not trying to do this with Office COM Automation, it is not supported and trust me it is very very unreliable. See this link from Microsoft http://support.microsoft.com/kb/257757
Key message from the link
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
I would suggest that you take a look at using NPOI library, it is very fast and works reliably on the server side. And you can then control where the file is generated.
If you are using a temp path or a temp file you should not care about where it is. If you need to care you should use a concrete path with a concrete file. You can also use a MemoryStream to do this all without HardDisk.
EDIT:
Additional to the comment I recommend you to use Open XML to create a Excel Sheet. If this is an option let me know than will I post more on that.
I must say I didn't know this article from Chris Taylors post and if I had found this one last year it had saved me a lot of time. But what I must say at this point; it is possible to get a serverside solution running which uses interop and I need it last year. But it was a pain till it work and if anyone want to give it a try here are some advices:
you should not use interop directly
from a ASP apllication
you can't use impersonation (it may
fail unexpected)
you will need a service which runs as
System
you need a executable that runs the
interop
you have to manage the the execution
through the service (and I mean
manage, not just starting)
And again; I don't recommend to do this. Use any other solution if you. (MS Supports automation services with SharePoint2010)