Html string in excel cell - asp.net

I am exporting a grid from my ASP.NET application. One of the columns in the grid (called Notes) has string that has html in it. For example, the Notes column can contain <Head> html tag. However when I export it to excel, I am not being able to see the string <Head>.

Tehnically speaking this is not export to excel, but you send a html with a wrong headers to trick browser to open this content with excel, among the other things downside of this approach is that you don't have control over process of exporting.
Use proper exporting and export data, not some UI view. There is a excellent and free .NET solutions for creating excel files :
NPOI (xls) or / and EPPlus (xlsx)

Related

How to convert a html table to downloadable excel file ASP.NET C#

I need a function to convert any HTML table to a excel file!
I like to add a button to my page and once that button is clicked I pass a complete HTML table in to such a function that will generate an excel file and past the html table into the file with the same layout and design!
I appreciate any help with this request.
I am using c# and asp.net
This is a nasty trick, but it works in recent version of excel.
You just save the html table as a normal html file, then just rename it to xls. If your application is a web application use response.setHeader("Content-Disposition", "attachment; filename=order.xls").
Recent version of excel support Microsoft Office XML Formats, so if the previous solution doesn't work, you may want to try to generate the xml from the html table.
See XML Spreadsheet Reference.
What I do, is to generate the xml from excel in the xml format, then hack into it. You can implement also advanced features like automatic filters, and cool layouts. I used a template engine to generate the xml (Freemarker, it's Java stuff, but I'm sure there are template engines also for .NET)

Exporting multiple Grid Views on a page to mutiple worksheets of a single excel file in ASP.Net

I know that there a quite a few techniques to export grid view to excel. Below are a few:-
HTML Rendering to HTMLTextWriter and Sending the Response Content Type as Excel.
Using XML technique and generating the XML String as Excel Sheets.
Using Office Interop dlls to create an object for excel document and work on it accordingly.
Now the requirement is, I have multiple Grid Views on a aspx page and these Grid views are loaded from different data sources. I have Export button, which have to export each grid view to separate work-sheet of a single excel file.
This can be done using second and third techniques mentioned above. Want to know the possibility of using the first technique i.e. using HTMLTextWriter
You can do using Open XML
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5124
http://openxmldeveloper.org/

Generate PDF file from HTML string in asp.net with vb

I want to generate PDF file from HTML string and that PDF file I have to attach in mail using asp.net. I try ghtmldoc.exe, it will generate corrupted pdf file.
I also use iTextsharp. It generate PDF without format though I don't use CSS in that HTML page. Only data in html file is converted in PDF.
I've always found Winnovative HTML to PDF converter very useful for doing things like this:
http://www.winnovative-software.com/Html-To-Pdf-Converter.aspx
You can either specify a URL to convert to PDF, or specify a HTML string to convert to PDF instead.
An open source solution, would be iTextSharp:
http://sourceforge.net/projects/itextsharp/
Exporting to pdf is really made simple by telerik. check this link. Telerik has a good look too. so if you are not late to change over to telerik, i suggest do that.

adding images to openxml doc created from altchunk

I need an automated process for creating docx files from xhtml source. The xhtml files contain images (<img> elements) whose "src" attributes point to an external reference. But the docx files need to be readable without a network connection, so I need to find a way to embed the images directly into the docx package (namely, in the /media folder).
So far I've used the altChunk method (as described by Eric White) to create the .docx file. I had hoped to use the OpenXML SDK to insert the image parts into the package. But to do that I need to insert paragraphs (<p> nodes) into the document. Unfortunately the document part contains nothing but a reference to the altChunk (stored separately in the docx package). Of course, once the docx is opened, edited and saved, the altChunk part is removed and it’s contents are embedded properly in the document.xml. But I don’t know of any way to do that programatically, so that doesn't help.
Other options I’ve considered:
Partitioning the xhtml into segments, separated between each image, then adding each altChunk one at a time, with the appropriate image reference between each one. (Tedious but seems possible)
Inserting the images into the media folder, and then find way to embed WordProcessingML directly into the xhtml so that the <img> references the packaged image file. (Questionable at best)
Can anyone think of a better approach?
Well, I sorta solved my own problem: I decided to convert the document to mHtml (which can contain images embedded directly in the file) and then use the altchunk to create the final docx file. However, I still wanted to do some post-processing on the file (to insert endnotes in the Word document), but as mentioned above, this is not possible until after the altchunk has been transformed into docx, which cannot be done programmatically.
So it dawned on me that I could bypass the altchunk path altogether and simply use mHtml as the "gateway" from xHtml to docx. I just transformed the xHtml into mHtml, complete with embedded images and endnotes, then renamed the file with a .doc extension. The resulting document can be opened directly by Word (and will be converted more properly on subsequent save). So far it works great (albeit with some bugs in Mac's version of Word, as well as Word2003).

response.ContentType="application/vnd.ms-excel" resizing spreadsheet columns width

Opening an excel spreadsheet with the response object and I am curious to know if there is a way, once all the data has been written to resize/format the columns widths so that all the data fits?
Thanks, Ron.
Did you mean, set a response header and then throw HTML to the client browser?
MS Excel can handle HTML, but you can't store a VBA macro inside HTML, so there is no simply way to achieve this.
A possible workaround is to generate the HTML and set the column width to a value enough to show all the cell content without wrapping.
To help you achieve this, try generating a workbook in your computer desktop putting text and format similar to the report being exported, then save the workbook as a html file and then take a look at the code generated. Behind all the messy CSS/HTML autogenerated code, you will find some tips to improve the html table. By example, look at this snippet:
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
With this you are helping MS Excel to identify your HTML file as a Sheet. Now look in the css inside the element, you will find some custom selectors like col {mso-width-source:auto;}. Try playing with its values until you get satisfied with the results, and then incorporate it on the HTML.

Resources