Strategy in exporting to Excel with formatting from ASP.NET? - asp.net-mvc

So this is another exporting to Excel question.
I have a page that has a table with formatting by stylesheet.
When I export the page by setting the ContentType to application/excel and Content-Disposition to attachment, I can export the table to Excel (not CSV). However, it loses all formatting. I think it's because Excel does not load CSS and I guess that's reasonable.
So, in a scenario where I have to show the table on the web and also export to Excel, both with similar (even if not exact) formatting, what would be the best approach without using something like NPOI?
I am trying to minimize the work and keep the single template if possible. Is it necessary for me to create two separate templates: one with stylesheet, the other with embedded style in the table itself for Excel?
Having a single template with conditional formatting inside would be very messy.
Any ideas?

If you not yet solve the problem I'll recommend you to use Open XML SDK 2.0 for Microsoft Office (see http://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&displaylang=en). With this way you will be able create an XLSX file without installing Excel on the server. XLSX file is compressed (like ZIP file) collection of xml files. Open XML SDK 2.0 helps you create and change XLSX file as pure xml files. At the first time if you look at Open XML SDK a lot of things look like strange, but it's only at the beginning. There are so named "Open XML SDK 2.0 Productivity Tool" (a part of Open XML SDK 2.0) which can generate a lot of useful code for you. Moreover you can create a nice Excel document which you can use as a prototype (template) of the document which you will create. So you can solve the problem of complex formating without writing of a lot of code.
Look at http://msdn.microsoft.com/en-us/library/cc850837(v=office.14).aspx for some examples and on http://openxmldeveloper.org/default.aspx. See also Creating Excel document with OpenXml sdk 2.0 as a start example. You can find also a lot of good stuffs on http://www.codeplex.com about Open XML SDK

there are several aproach
you could instantiate a excel object on your server using VSTO, and then write the document on memory and write to response the native file, but this aproach could be a litle expensive if you create a excel object per request, so you could try to do a singleton object that wraps the excel object instance

You can create a report (rdlc file) with a similar look to the grid. Then, you can have an action where you instantiate a LocalReport, pass the data you want to it and call its Render method. You then return the byte array returned by the Render method.

Related

How to read pdf and extract text from pdf in symfony1.1?

I am working on Symfony-1.1 in an existing project. How can I read pdf files and extract text from them?
It's not a Symfony 1.1 related question, actually. It's a PHP one. There several libraries to handle PDFs in PHP. Following are some suggestions.
https://github.com/smalot/pdfparser
http://pastebin.com/dvwySU1a
http://www.pdflib.com/
If you just need to parse pdf in anyway and then process the text in PHP, you can also consider using a java library like the following.
http://pdfbox.apache.org/ (Is there a PDF parser for PHP?)

how to convert xls to csv using IOS library?

I need to read xls files in my IOS app. First of all, I want to convert xls files to csv format files, then my app parse csv files, but I can't find any ios library to convert xls to csv, please help me
If you have a .xls file, you can use the open source DHlibxls library to read the file into your app. This is an ObjectiveC framework that wraps a C-based library. The library is quite mature.
ios or any objecive-framework doesn't provide any thing for accesseing Microsoft's xls :(
To convert-xls to/fro csv is itself a project in it!!!
On top of this, there are different format of xls, now xlsx files. And writing a xls and reading it back in proper way is tooooo-cumbursome task to accomplish. However we have managed to read it but it is not 100% efficient :(
I guess in near future you may want to move to xlsx file then your task will be a lot more difficult. You can check yourself, change the file name extension to .zip and unzip you will see many files, one having row numbers, another columns, third with links, fourth with contents and so on. Mapping and getting in correct form in not impossible but needs a lot of work.
There can be many other ways to do, I can suggest to use java api to do, or even save you xls to csv directory from excel, then your work will be easy.

Export data to XLS (not via CSV) on iOS

I need to export some data to an .XLS file, pdf, and print.
I already tried the simple solution: exporting it to .CSV with CHCSVWriter. It works for printing and saving it to pdf (I open the CSV in a UIWebView and get the PDF or print from there). However, to use the CSV to be open in excel has two main problems:
1 - First, as the name says, in the CSV the values are separated by commas, and in some versions of Excel, it requires the user to separate 'manually' in cells.
2 - I have hebrew characters, and I already tried all the string encodings, and can't have both hebrew and latin characters.
So, after giving up after days of trying to use CSV to solve the issues above, I gave up. How can I export my data to XLS?
The LibXL library provides this functionality for both xls and xlsx formats. There is no iOS version, but people say the iOS version is coming. You may want to contact LibXL support to confirm this.
EDIT:
The iOS version is available now.
This article explains how to programmatically create an Excel (.xls) file without using any external library. It just opens a file stream and it writes XML contents straight to it.
It is written in C#, but the core information coming out of it is the XML formatting used to create nodes and fill attributes for corresponding cell values and formatting.
Please consider I have not tried this myself, I found it while doing a search. Please feel free to ask if some C# bits are not clear. HTH

Creating Microsoft Word (.docx) documents in Ruby

Is there an easy way to create Word documents (.docx) in a Ruby application? Actually, in my case it's a Rails application served from a Linux server.
A gem similar to Prawn but for DOCX instead of PDF would be great!
As has been noted, there don't appear to be any libraries to manipulate Open XML documents in Ruby, but OpenXML Developer has complete documentation on the format of Open XML documents.
If what you want is to send a copy of a standard document (like a form letter) customized for each user, it should be fairly simple given that a DOCX is a ZIP file that contains various parts in a directory hierarchy. Have a DOCX "template" that contains all the parts and tree structure that you want to send to all users (with no real content), then simply create new (or modify existing) pieces that contain the user-specific content you want and inject it into the ZIP (DOCX file) before sending it to the user.
For example: You could have document-template.xml that contains Dear [USER-PLACEHOLDER]:. When a user requests the document, you replace [USER-PLACEHOLDER] with the user's name, then add the resulting document.xml to the your-template.docx ZIP file (which would contain all the images and other parts you want in the Word document) and send that resulting document to the user.
Note that if you rename a .docx file to .zip it is trivial to explore the structure and format of the parts inside. You can remove or replace images or other parts very easily with any ZIP manipulation tools or programmatically with code.
Generating a brand new Word document with completely custom content from raw XML would be very difficult without access to an API to make the job easier. If you really need to do that, you might consider installing Mono, then use VB.NET, C# or IronRuby to create your Open XML documents using the Open XML Format SDK 1.0. Since you would just be using the Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespace to manipulate Open XML documents, it should work okay in Mono, which seems to support everything the SDK requires.
Maybe this gem is interesting for you.
https://github.com/trade-informatics/caracal/
It like prawn but with docx.
You can use Apache POI. It is written in Java, but integrates with Ruby as an extension
This is an old question but there's a new answer. If you'd like to turn an HTML doc into a Word (docx) doc, just use the 'htmltoword' gem:
https://github.com/karnov/htmltoword
I'm not sure why there was answer creep and everyone started posting templating solutions, but this answers the OP's question. Just like Prawn, except Word instead of PDF.
UPDATE:
There's also pandoc and an API wrapper for pandoc called docverter. Both have slightly complicated installs since pandoc is a haskell library.
I know if you serve a HTML document as a word document with the .doc extension, it will open in Word just fine. Just don't do anything fancy.
Edit: Here is an example using classic ASP. http://www.aspdev.org/asp/asp-export-word/
Using a technique very similar to that suggested by Grant Wagner I have created a Ruby html to word gem that should allow you to easily output Word docx files from your ruby app. You can check it out at http://github.com/nickfrandsen/htmltoword - Simply pass it a html string and it will create a corresponding word docx file.
def show
respond_to do |format|
format.docx do
file = Htmltoword::Document.create params[:docx_html_source], "file_name.docx"
send_file file.path, :disposition => "attachment"
end
end
end
Hope you find it useful. If you have any problems with it feel free to open a github issue.
Disclosure: I'm the leader of the docxtemplater project.
I know you're looking for a ruby solution, but because all other solutions only tell you how to do it globally, without giving you a library that does exactly what you want, here's a solution based on JS or NodeJS (works in both)
DocxTemplater Library
Demo of the library
You can also use it in the commandline:
npm install docxtemplater -g
docxtemplater <configFile>
----config.docxFile: The input file in docx format
----config.outputFile: The outputfile of the document
This is a way Doccy (doccyapp.com) has a api that does just that which you can use. Supports docx, odt and pages and converts to PDF as well if you like
Further to Grant's answer, you can also send Word a "Flat OPC" file, which is essentially the docx unzipped and concatenated to create a single xml file. This way, you can replace [USER-PLACEHOLDER] in one file and be done with it (ie no zipping or unzipping).
If anyone is still looking at this, this post explains how to use an XML data source. This works nicely for me.
http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/
Check out this github repo: https://github.com/jawspeak/ruby-docx-templater
It allows you to create a document from a word template.
If you're running on Windows, of course, it's a matter of WIN32OLE and some pain with the Word COM objects.
Chances are that your serving from a *nix environment, though. Word 2007 uses the "Microsoft Office Open XML" format (*.docx) which can be opened using the appropriate compatibility pack from Microsoft.
Some of the more recent Office apps (2002/XP and 2003 at least) had their own XML formats which may also be useable.
I'm not aware of any Ruby tools to make the process easier, sadly.
If it can be made acceptable, I think I'd be inclined to go down the renamed-html file route. I just saved a document as HTML from WordXP, renamed it to a .doc and opened it without problem.
I encountered the same problem. Unfortunately I could not manipulate the xml because my clients should themselves to fill in templates. And to do this is not always possible (for example, office for mac does not allow this).
As a solution to this problem, I made ​​a simple gem, which can be used as an rtf document template with embedded ruby: https://github.com/eicca/rtf-templater
I tested it and it works ok for filling reports and documents. However, formatting badly displays for complex loops and conditions.

Programmatically load CSV file into Excel Worksheet (Delphi 7)

I have a large amount of data to insert into an worksheet of an existing Excel workbook. The Excel workbook will have other worksheets containing calculations and a pivot tables. The data may have as many as 60,000 rows and more than 30 columns. This solution must work for both Excel 2003 and Excel 2007.
Using the Excel OLE object is way too slow so we are attempting to load the data from a CSV file. We have come up with a method to load the data by placing the data onto the clipboard and then pasting it into the worksheet. I feel this is a quite a kludge. Is there another way to programmatically load a CSV file into a worksheet? Or perhaps a different solution altogether?
Update: We got slammed with another task before we could fully investigate the answers. We should be able to get back to this in a couple of weeks. I'll be sure to update again when we get back to this task.
Thanks for all of the answers to date!
XLSReadWrite is a component that can read and write excel files from Delphi. It's fast and it has support for Excel 2003 and 2007. You can create new excel files as well as open existing ones and add/modify them.
Also you do not need to have Excel installed to be able to use it.
See http://www.axolot.com/components/xlsrwii20.htm
Any chance you can drop the requirement for this to work with Office 2003? I would have recommended the Open XML Format SDK. It lets you bind managed code assemblies to spreadsheet documents that can handle events such as Open or Close, and read and write to cells in the document, among other things. Alternatively, you can use it to manipulate XSLX documents from an application. Quite slick, actually.
Since this won't work for you, how about writing a macro that pulls in the CSV file when the spreadsheet is loaded?
you can load the csv into listview or usin OLEDB provider to load it on DBGrid, then export it into xls file format using TMxExport component from Max Components:
Max Components
Have you tried linking the csv file directly into the worksheet.
Go to Data -> Import External Data -> Import Data
change the file type to 'Text Files'
You can then refresh the worksheet when the csv is update.
NOTE: I have not done this with the volume of data you have indicated, so YMMV
Actually there is a way that is quite fast, pretty old tech (nowdays) but is probably the fastest.
It's ADO or for earlier versions DAO (note not ADO.NET)
You can read a CSV file using ADO and the JET Engine to get the data into a ADO recordset, then an Excel Range Object has a CopyFromRecordSet method that will copy (very fast) from the ADO (or DAO) recordset.
http://msdn.microsoft.com/en-us/library/aa165427(office.10).aspx
You can try to use Tab Separated Values instead of CSV - than you just paste this into Excel :)

Resources