Enabling Macros in Excel using GemBox 3.1 API - c#

Well recently having a problem with GemBox.SpreadSheet 3.1. Right now the program will write an excel file, but when opening the file it will attempt to open the file saying "Excel found unreadable content" then asks yes or no if you wanna try to recover the document. Click yes, it then says "The file is a macro-free file, but contains macro-enabled content".
However, when the program makes different files without the Macros, it does not say it is corrupt and works just fine.
So my question is, How do you enable the Macro content using GemBox API? Or is there just bad coding somewhere?
EDIT:
I figured out that the program was saving the wrong extension.

I figured out that that in order for Macro-enabled content, the file needs to be stored in XLSM, NOT XLSX!

Related

Report in .rdlc format exported to PDF as read only C#

I'm working in an app in .NET where i'm using a report (.rdlc format) that i export to PDF. My client needs the PDF to be read only for security purposes. I already tried with FileAttributes and FileInfo libraries.
My biggest problem is that when i see my PDF properties, the checkbox for read-only is ticked but then i try to edit the fields with Adobe Acrobat and i am able to do it.
Any ideas?
I think you need create PDF/A files. It is separate archive standard of PDF (not "readonly" property). So when you open PDF/A files you get notification that it is archive file. End-user could edit any file, but in such case "archive" property will lost.
Unfortunately SSRS could not make PDF/A files.
May be, you should look for another report generator. Something like Crystal or FastReport. I checked FR and ensured that it can create PDF/A.
This can be done by importing iTextSharp library and using the following code:
I couldn't solve my problem without rendering and saving the PDF first in the file system as a dummy file, i.e., with all permissions. As you can see in the code, afterwards, i refer to the same dummy file and change it's permissions with static method Encrypt() from the iTextSharp library. Then, i save my finished and restricted PDF to the file system and delete the dummy one.

Two files that are binary identical, yet exhibit different behavior

I'm posting with tags asp.net and excel because that is the origination of my problem, but I'm not really sure this is the right place - ultimately, my problem is that I have two files (served by an ASP.Net application) which are identical based on a binary file compare using
fc /B A.xls B.xls
However, they exhibit different behavior: the first one opens fine in Excel; the second one does not. I conclude, then, that there is something different about the files beyond what the FC utility checks.
I have tried sending these two files to a friend to ask for his help, but discovered that when I do so, the problem file gets "fixed". In fact, if I do just about anything with this file, it gets "fixed". By fixed, I mean that it then opens fine in Excel. For example, if I zip it, then extract it from the zip, it is fine. If I open in Notepad++ and "Save As", it is fine. Same with Wordpad. Using plain old Notepad does NOT fix it.
So, obviously, there is some difference about these two files that I am missing.
I'm not sure if I will have any luck asking people to visit a random website, but if you want to see an example of the behavior, I have created a minimal page to duplicate the problem at http://rodj.me/ExcelTest
Click on the link for "MinimalHtml.aspx", and the app will serve an HTML based xls file using the following in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "filename=MinimalHtml.xls");
}
Depending on your browser and browser settings (my tests have been in Chrome), you may get Excel opened with a blank page. Regardless, you should get the file MinimalHtml.xls downloaded. It is a plain text file. You should find that this file will NOT open in Excel. However, if you zip the file, then extract it from zip, it WILL open.
I'm curious about what other file differences I'm missing when just doing an FC compare, but ultimately, I need to get the ASP.Net application corrected to serve the HTML version of the Excel file correctly. Interestingly, if I create an XML version of the spreadsheet, it downloads/opens fine. That is what the "MinimalXml.aspx" link does.
Can anyone help with either 1) how to figure out what is different about the two files; or 2) what must change in the ASP.Net application to get it to serve the file correctly?
I think your problem might be a Microsoft security patch. See this article:
Infoworld article
When you open the file directly, the patch causes the issue which results in a blank page because the file contents is HTML not Excel. When you download the file in a Zip file and unzip it, it is deemed safe and opens correctly.

Upload file to server that is open

So I'm using WebClient to upload a file to a server. It works great except for one problem. If the file is opened by another program then it will not upload. For instance, if it's a word document that's been saved but is still opened by word then it fails to upload. Is there a way to force it to read whatever is there and upload it?
If i understand your question right then you can use this solution to test if the file is open and then close it if it is. It's a useful helper class i've used in the past.
https://stackoverflow.com/a/1247326/4612655

Reading non-standard excel file with C#

What do i mean by 'non-standard'?
Take a look at these images: http://imgur.com/a/tFqHQ
The first one is the non-standard excel file. I'm pretty sure it's not an excel file, but the file's extension is .xls and for some reason Excel can open it, and understand it's structure.
The second image is the same file after it was opened in excel, and saved out to .xls (97-2003).
If excel can open it, and view it correctly, i should be able to do as well. Any tips how to approach this?
I have to mention that, my app have to use and read the non-standard excel files, because otherwise the user have to open the files one-by-one in (excel/libre office) and save it out in a correct format, which i would like to avoid for convenience.

Strip Excel file of Macros with C#

I've been asked to strip an Excel file of macros, leaving only the data. I've been asked to do this by converting the Excel file to XML and then reading that file back into Excel using C#. This seems a bit inefficient to me and I was thinking that it would be easier to simply load the source Excel file into C# and then create a new target Excel file and add the sheets from the source back into the target.
I don't know where macros live inside an Excel file, so I'm not sure if this would accomplish the task or not. So, will this work? Will simply copying the sheets from one file to another strip it of it's macros or are they actually stored at the worksheet level?
As always, any and all suggestions are welcome, including alternate suggestions or even "why are you even doing this???". :)
To do this programmatically, you can use the ZipFile class from the System.IO.Compression library in .NET from C#. (.NET Framework 4.5)
Rename the file to add a ".zip" extension, and then open the file as a ZIP archive. Look for an element in the resultant "xl" folder called "vbproject.bin", and delete it. Remove the .zip extension. Macros gone.
Your best bet is to save the workbook as an xlsx, close it, open it, then save as a format of your choice.
This will strip the macros and is robust. It will also work if the VBA is locked for viewing.
Closing and reopening the workbook is necessary otherwise the macros are retained.
If you're needing to use C# to do this, I agree that it would be easier to load the source Excel file into C# and create a new target file only copying over the cells and sheets you need. Especially if you're doing this for a large amount of excel files I would recommend just creating a small console app that, when given an excel sheet, will automatically generate a new excel sheet with just the data for you.
One tool that I've found extremely useful and easy to use for such tasks is EPPlus.

Categories