How to export data to pdf in asp.net MVC? - asp.net-mvc

I don't have grid view as I am working on asp.net MVC.
So can I create Response object which writes data into pdf format
Response.AddHeader("Content-Disposition", "attachment; filename=data.pdf");
Response.ContentType = "application/.pdf";
I use these two lines but I don't know in which format, I should write data?

you must use a pdf library like iTextSharp. just do a google search.
I usually generate my pdf reports on the fly using report viewer control (client-side mode).

LOL.
You need to get PDF converter to convert from whichever format you use to PDF. The last I checked there were no fully functional converters for free. Buy it, buddy.

Related

In Asp.net using C#, how can I convert ==head== __italic__ and others to html equivalents?

I was able to save the following in database
==heading===
Some __useful contents__ and **bold texts** here
I want to output ==heading== to <h1>heading</h1>
__useful contents__ to <i>useful contents</i>
**bold texts** to <b>bold texts</b>
Thanks
I'd recommend using Markdown for this purpose (same is used here, in SO).
You can get package that will convert markdown to HTML here in for C#. Or if you want client browser to render it (to save CPU on the server) you can use this JS plugin.

How to add an image to word document and download using grails?

I use the code below to download image directly but I need to put it in word doc and want to download the doc.
response.contentType = "application/octet-stream"
response.setHeader("Content-disposition", "attachment;filename=image.jpg")
response.outputStream << inputstream
response.outputStream.flush()
Can anyone know how to add an image to word document and download using grails?
Any help is appreciated.
Thanks
as suggested by Dónal, Apache POI might be a solution. But you also might want to try the following approach:
Create a word document with an image
save it as xml
create such an xml document in grails and replace the image with your own
deliver it through grails with content-type "application/msword"
at least this works fine for excel files...
I use http://blogs.bytecode.com.au/glen/2010/11/04/creating-word-docx-documents-dynamically-from-grails-or-java.html and http://blog.iprofs.nl/2012/10/22/adding-images-and-layout-to-your-docx4j-generated-word-documents-part-1/ to solve my code so I use docx4j for inserting image into word document using grails.
Thanks

CSV file with Italic values

Is there any way to create a csv file using c#, which can have/show few values in Italic format, when we open it in excel.
Its just not possible. Theres no markup in csv. Either export an xls(x) or rethink your problem/solution. Why csv? It's not really meant for people to read. Only to transfer data from one application to another.
A CSV file is a text file where Excel can only interpret the type of field content as best (text, numeric, date) but not within a field. So the short answer is no.
There are libraries available for the ASP.NET MVC environment which allow you to create true Excel files so you then have complete control over field formats etc. A quick Google will find these.
UPDATE
A possible solution, if you are using MVC, is to create an HTML 'file' and then download that:
this.Response.AddHeader("Content-Disposition", "Employees.xls");
this.Response.ContentType = "application/vnd.ms-excel";
return this.Content(sb.ToString());
I've never tried this but have seen that it might work.

How to export data to excel using mvc without using third party control

I want to export data to excel both in .xls and .xlsx format in asp.net
MVC without using any third party control.
I tried using following code but it doesn't support .xlsx format.
Response.AddHeader("Content-Disposition", "attachment;
filename=test.xlsx");
Response.ContentType = "application/ms-excel";
Response.Write(sw.ToString());
Response.End();
Pls give me solution.
For your lines maybe this work, but I'm not sure about what are you trying to do.
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AppendHeader("Content-Disposition", "attachment; filename=test.xlsx")
Response.Write(sw.ToString())
Response.End()
Besides Open XML SDK 2.0 described by #amurra, you can try OpenExcel. It is excellent library and is quite fast.
A quick and dirty option is to put all your data in a table and write that table only to response stream with the headers you are trying already, like:
Response.AddHeader("Content-Disposition", "attachment; filename=test.xlsx");
Response.ContentType = "application/ms-excel";
//NOW WRITE TABLE
Response.Write("<table><tr><td>SrNo</td></tr><tr><td>1</td></tr></table>");
Response.End();
I've not used this hack but have seen it used in some application. When opening it Excel gives user a warning that the data is not excel format, but opens it successfully.
You should use the Open XML SDK 2.0 to achieve that task since it is the supported Microsoft way of creating Office documents.
The other hacky way is to create a partial view and export the html to Excel. This would give a readonly view to the data since its not in the proper excel format, but works when you don't want to spend the time to write the Open XML.
Also the correct MIMEConentType for .xlsx is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and for .xls it is "application/vnd.ms-excel"

what are the other setting need to see a html table into excel sheet format in open office org?

I have generated a html table from my web application and save the table into .xls format(in a single word i am generating a .xls sheet from my web application ).
What other setting I have to show it in table form.
You are not producing an XLS file, you are producing a mal-formed HTML file with a name that ends in .xls.
Indeed, you aren't even doing that since there aren't files on the web (there are streams that may or may not end up in files).
Different versions of Open Office, with different settings, will differ in terms of how they deal with stuff that is wrong. The version on one of the machines you are doing is saying "eh, this isn't XLS, oh! it's HTML with a table, I know what to do", while the other is getting as far as "eh, this isn't XLS, it's a bunch of text with strange less-than and greater-than characters all over the place, what do I do".
What you want to do is to produce an actual stream that Open Office and other spreadsheets can deal with. XLS is possible, but pretty hard. Go for CSV instead.
If your table was going to be:
<table>
<tr>
<th>1 heading</th><th>2 & last heading</th>
</tr>
<tr>
<td>1st cell</td><td>This is the "ultimate" cell</td>
</tr>
</table>
Then it sould become:
"1 heading","2 & last heading"
"1st cell","This is the ""ultimate"" cell"
In otherwords newlines to indicate rows, commas to indicate cells, no HTML encoding, quotes around everything and quotes in your actual content doubled-up. (You don't need to always have quotes on your content, but it's never wrong so that's simpler than working out when you do need them).
Now, make your content type "text/csv".
You are now outputting a CSV stream that can be saved as a CSV file. Your spreadsheet software will have a much better idea about what to do with this (it may still ask about character ecodings on opening, but the preview will show you a spreadsheet of data, not a bunch of HTML source all over the place.
It's not really saving as a .xls file -- it appears to be saving as the HTML, but with a .xls extension. How are you generating the .xls? On the server-side, you can provide a button to generate .xls directly (different methods depending on your server platform -- using perl there is the Spreadsheet::WriteExcel module that writes .xls directly, using Java there is JExcel (http://jexcelapi.sourceforge.net/ and POI (http://poi.apache.org/)), other platforms will have their methods.
Okay Subodh, If you want to generate .xls or .csv files, You can't just change the extension of the file and have it open up correctly in that program.
2 Options you have at this point, both involve creating the file with the data on the server and then sending it to the user to download it.
.csv
CSV files are easier to generate from the server side. In a very basic way you can think of them as regular text files with commas(not necessarily only commas) separating individual cells that can be read by spreadsheet programs. For PHP there is an article Here that explains how to generate CSV files.
.xls
xls files are not as simple as simple to generate as CSV files. On the server-side you will need a solution to generate these. For PHP there is a resource Here.
Using xls over CSV has obvious advantage that you can specify formatting and can control visual representation of your data.
Edit :
Upon closely looking at the image you posted, I can see what you are trying to do. If you just want to get that file to open correctly in a spreadsheet program, then don't save it either as CSV or xls
hello.html
<table>
<tr><td>Hi</td><td>Hi</td><td>Hi</td><td>Hi</td></tr>
<tr><td>2</td><td>2</td><td>131</td><td>11312</td></tr>
</table>
Saved as an HTML file will open up correctly(as a proper table) in any spreadsheet program.
To narrow down the problem:
1) Are you opening the same .xls file on both machines?
- what version of OpenOffice is on Machine 1?
- what version of OpenOffice is on Machine 2?
2) How are you creating your .xls file?
- are you just using the response object to change the content-type, or some proprietary software?
- can you include a code sample?
3) Have you tried a pure HTML format?

Resources