MVC Excel Interop - c#

I have an MVC app that uploads an XLS file and imports it. Ther main issue is that these will often be password protected.
Unfortunately we can't have Office on the server so interop is not an option server side.
I was thinking that when the user browses to the file via their browser and clicks import that I could some how transform the file into plain old CSV before it actually gets to the server at which point I could do the main import. It's an intranet app so the clients will have Office.
So the question is, is there anypoint between the users selecting the file annd clicking upload where the file will be available for me to process on the browser side (where interop would be available) to transform the file?

No. You can't interact with programs on the clients machine (except with browser addons like ActiveX, don't go there). And you should definitely NOT use Interop on the server.
I would suggest to use EPPlus. It can read/write Excel files, without actually the need of interop. It only works with XLSX documents, so I hope it suits you. Else you might need a third-party library that is capable of reading XLS files.

As per Patricks answer you can't interact with a program on a client machine. An alternative to EPPlus is NPOI which is similar in that it removes the need for interop however it works with either XLS or XLSX documents.

Ended up using Syncfusion which does the job nicely.

Related

What is the standard way for dealing with PowerPoint (.PPTX) files on the server?

I've been tasked with a feature that can generate PowerPoint files on the server using C#. I'd basically start with a template, and programmatically replace some text with live data from the database. I've been doing some research into this area for the past day and here's what I've found:
PowerPoint has this sort of thing built in, meaning it can connect to external data sources and pull in data. Most examples of this, I've found, either use PowerPoint automation done on the server (I've been advised against this) or assume a SQL Server backend. Our company uses Oracle for our RDMS needs. Oracle has a solution for this called Oracle BI, but it requires a whole new web server setup to run various Java EE components and what not. I didn't look at the price, but knowing Oracle it's not cheap. It also requires new software to be installed on the end user's machine, which we really want to avoid.
Generating PowerPoint files on the fly is possible. The company that is basically the go-to guys for this problem (every help forum points to them, and they get all the rave reviews) is Aspose. They have .NET components for dealing with just about any Office format you can think of. The problem is, they are astronomically expensive. Just the PowerPoint component (a site license for up to 10 developers) would cost $3,995.
The third possibility is generating a solution in-house. After all, a PPTX file is just xml, right? Well, looking closer, a PPTX appears to be a gzip archive. It contains many folders, each containing many XML files. Modifying a PPTX file would, correct me if I'm wrong, entail unzipping the file to a temporary directory, reading the XML file and modifying the contents, then packaging up everything again and write the file out to the response stream. Perhaps there are libraries that can work with gzip streams on the fly without extracting everything.
My Question: Are there easier ways to work with a PPTX file using .NET that don't require working with compressed XML files or buying very expensive software? Basically, we need to modify a PowerPoint file, change some text, and allow the user to download that generated file from a web server.
OpenXML is Microsoft's .Net library that lets you manipulate Office documents. It lets you open a PPTX file and provides an object model that wraps the XML contents.
Here's the link to the OpenXML SDK and the MSDN documentation.
I've used OpenXML to let a ASP.Net page dynamically generate Word documents from a database.
Don't use Office Interop on a web server. It's an all-around bad idea.
If you are only replacing text placeholders for files that will not change, the home grown solution that finds the placeholders in the xml files in the gzip archive should be doable. .Net has had zip support for some time, and it is greatly improved if you are able to use .Net 4.5, so you shouldn't need to extract the archive to a temporary location at all.
PowerPoint should also support connecting directly to Oracle in the same way it supports connecting to Sql Server (just play around with the connection options), without needing the special Oracle BI stuff. However, I'd still prefer the home-grown solution, as this will only work while the powerpoint file is able to reach your database directly, which is typically only possible in your local LAN environment or with an active VPN.
If you want anything fancier than a simple text replacement, perhaps looks for an Aspose competitor.

creating xls file in web server

I am creating an xls document using Microsoft.Office.Interop.Excel but it's not working in IIS7.
I am getting the error message below
Microsoft Office Excel cannot open or save any more documents because
there is not enough available memory or disk space. • To make more
memory available, close workbooks or programs you no longer need. • To
free disk space, delete files you no longer need from the disk you are
saving to.
Is there any other free tool or option that we can use to create xls in web server?
First of all it is not a good idea to use office interop API on a server as described in this link.
For free tools you could look at ExcelLibrary and Epplus that seems to be popular but I never used it. The other option would be to use the OOXML SDK 2.5. With this you can create only Xlsx files and has a learning curve.
For more info have look a this topic that, although it is closed, contains probably all the info you need.

Print command in EPPlus?

I'm using EPPLUS to allow users to download data to Excel.
However, I'd like to add a feature wherein they can just choose to print this data rather than download it. Can we issue a print command from EPPLus?
It is a web app
Ah, so there's the main problem... You know, even if EPPlus had a method for printing, it wouldn't help you, because it would try to trigger a printing dialog on the server-side, but you need to send the file to the client over the internet. So I'm afraid the answer is no, the client surely has to download the file either way to be able to print it.
Furthermore, xlsx can't be that easily opened in the web browser (compared to pdf for instance), so the solution to somehow trigger a printer dialog on the clients pc will be a tiny bit difficult. C# can't trigger anything over the internet with asp.net because of the security problems and I'm not really sure if javascript has enough permissions to open an application for Excel file and start the printing process..
Maybe your solution would be to create a PDF file instead of XLSX, send it to the client as a part of the website/response and use javascript to print the file. PDF can be opened in most browsers nowadays and we have a nice js function printWithDialog(). I believe this doesn't work for xls and it certainly doesn't fulfil your requirement "without download". The client has to have the access to the file before he can print it, you can't overcome this.

How can I automate the checkout of an excel spreadsheet from a sharepoint document library, edit the spreadsheet, and check it back in?

I have a couple of hundred excel spreadsheets that are stored in a SharePoint 2010 document library.
A good many of these spreadsheets have hard coded URLs that need to be updated to new URLs based on some re-structuring of our SharePoint site.
I would like to iterate through all of the spreadsheets, open them and do a search and replace and then write them back into SharePoint.
For the record, I already know how to iterate the excel spreadsheets in SharePoint (creating reference to SPFile) and run the checkout and checking commands.
I am almost very comfortable with doing the OLE Automation of Excel to do the find and replace.
I am struggling with how to bridge the gap between the two. After I grab a reference to a file and check it out, how can I do ole automation on it? Normally I believe that to open a reference to a file I would need a file path but even if I check out with the "use local" option there is not a file created into the file system (and even if there is I am not convinced that it would work to work with the temp file.)
It seems that I should be able to load it from a stream into a VBA Excel Automation object but I am not sure how.
Any thoughts?
Let's say you build this tool as a Console Application, which you run on the SharePoint server. In the Console Application code:
Use the SharePoint object model to check-out the file
Use the .Net WebClient.DownloadFile method to download the file to your local system
Run your Automation code
Use the SharePoint object model to upload and check-in the modified file
I should point out that some server administrators will not allow Automation code to run on the server itself. In this case, run the Console Application on a client machine and use the SharePoint Lists webservice to do the check-in and check-out.

How can I programmatically create, read, write an excel without having office installed?

I'm confused as hell with all the bazillion ways to read/write/create excel files. VSTO, OLEDB, etc, but they all seem to have the requirement that office must be installed.
Here is my situation: I need to develop an app which will take an excel file as input, do some calculations and create a new excel file which will basically be a modification of the first excel file. All with the constraint that the machine that runs this may not have office installed. (Don't ask why...)
I need to support all excel formats. The only saving grace is that the formats spreadsheets themselves are really simple. Just a bunch of columns and values, nothing fancy. And unfortunately no CSV as the end user might not even know what a CSV file is.
write your excel in HTML table format:
<html>
<body>
<table>
<tr>
<td style="background-color:#acc3ff">Cell1</td>
<td style="font-weight:bold">Cell2</td>
</tr>
</table>
</body>
</html>
and give your file an xls extension. Excel will convert it automatically
Without Office installed you'll need something designed to understand the Excel binary file format (unless you only want to open Office 2007 .xlsx files).
The best I've found (and that I use) is SpreadsheetGear, which in addition to being .NET native, is much faster and more stable then the COM/OLE solutions (which I've used in the past)
read and write csv files instead. Excel reads them just fine and they're easier to use. If you need to work against .xls files then try having support for OpenOffice as well as Excel. OpenOffice can read and write excel files.
Did you consider way number bazillion and one: using the Open XML SDK? You can retain styles and tweak it to your liking. Anything you can do in an actual file is possible to achieve programatically. The SDK comes with a tool called Document Reflector that shows the underlying XML and even shows LINQ statements that can be used to generate them. That is key to playing around with it, seeing how the changes are made, then recreating that in code.
The only caveat is this will work for the new XML based formats (*.xlsx) not the older versions. There's a slight learning curve but more material is making its way on blogs and other sites.
If cost is not an issue, I'd suggest looking in Aspose's Excel product. I use their Word product and I've been satisfied.
Aspose.Cells
Excel XLSX files "just" XML files - more precisely ZIP files containing several XML files. Just rename a Excel file Test.xslx to Test.zip and open it with your favourit ZIP program. XML schemas are, afaik, standardized and availiable. But I think it might not be that easy to manipulate them only using primitive XML processiing tools and frameworks.
Excel files are in a proprietary format so (afaik) you're not going to be able to do this without having the office interop available. Some third party tools exist (which presumably licence the format from MS?) but I've not used them myself to comment on their usefulness.
I assume that you can't control the base file format, i.e. simple CSV or XML formats aren't going to be possible?
I used to use a very nice library called CarlosAg, which uses Excel XML format. It was great (and Excel recognizes the format), and also incredibly fast. Check it out here.
Oh, as a side note, we used to use this for the very same reason you need it. The servers that generated these files were not able to have Excel installed.
If you cannot work with CSV files as per #RHicke's suggestion, and assuming you are working on a web app, since a desktop app would be guaranteed to have XL installed as per requirements.
I'd say, create your processing app as a webservice, and build an XL addin which will interact with your webservice directly from XL.
For XLSX files, look at using http://www.codeplex.com/ExcelPackage. Otherwise, some paid 3rd party solutions are out there, like the one David suggested.
I can understand the requirement of not having office installed on a server machine.
There are many libraries like aspose being available, some of them requiring license though.
If you are targeting MS Excel formats, then a native, Interoperability library, ACE OLEDB data provider, from Microsoft is available which you can install on a machine and start reading, writing programmatically. You need to define a connection string and commands as per you needs. (Ref: This article #yoursandmyideas)talks about using this library along with setup and troubleshooting information.

Categories

Resources