Export Datagrid to PDF

D

Dave

Okay, I have code here to export a datagrid to Excel. Can I modify it to
export it to a PDF? Thanks,

Function ExcelExport(ByVal DatagridID, ByVal ExportFilename)

Response.AddHeader("content-disposition", "attachment; filename=" &
ExportFilename & ".xls")

Response.ContentType = "application/vnd.ms-excel"

Me.EnableViewState = False

Response.Charset = String.Empty

Dim myTextWriter As New System.IO.StringWriter

Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)

DatagridID.RenderControl(myHtmlTextWriter)

Response.Write(myTextWriter.ToString())

Response.End()

End Function
 
A

Andrew L. Van Slaars

I've seen a lot of products out there... depends on what it is worth to you
I guess. ASP Turbine offers a .NET PDF solution for around $650, and I've
seen some as high as $7,000.

If anybody finds a low price or free solution, or the code to create one, I
would appreciate seeing it as well.
 
J

Jorge Aguirre

Dave
can you tell me how to use the Function ExcelExport.
How can I pass the DatagridID?

Jorge
 
J

Jorge Aguirre

Thanks Dave! Great!

Dave said:
Jorge,

Just take the function that I created below and put it in your code.
Whenever you want to export a datagrid to excel, just create a button and
write line of code like this

Private Sub Button1_Click
ExportToExcel(datagrid1, "My Export Filename.xls")
End Sub

where 'datagrid1' is the ID of your datagrid and 'My Export Filename.xls' is
the filename for your newly exported datagrid.
 
I

Igor Stavnitser

Outputting the datagrid to PDF maybe a challenge even for a commercial
product.

Currently I use similar solutions to dymanically assemble word docs on
the server and conver them to PDF. IN my solution out of security
concerns (and also for scalability) I dedicate own computer to the
task of using Word Objecl Library and generating PDF.

I have succesfully built an automated Word->PDF Conversion solution
using Word's COM Object Library. Here is how to implement DataGrid ->
PDF printing (through excel and the code that you have already wrote)
You must have Excel installed on your web server to implement solution
described below. You must configure Excel to be more secure than when
its out of the box on the server, use commonsense and usenet to do
that.
Ok Here it is:

1. Download free PDFCreator program (Acrobat Like PDF Printer for
windows - GPL, OpenSource)
http://sector7g.wurzel6.de/pdfcreator/index_en.htm

2. Configure it to output automatically to a specified directory

3. Modify your code as follows:
Chage: Response.Write(myTextWrite
to
NewCode: 'New code will take myTextWriter.ToString() and save it to
some directory as configured by web.config with .XLS extenstion. You
can use Guid.newGuid function to make the name of the file unique.

4. Further in your code you open file saved in step 3 with Excel
Object Library that you can include in your references. Excel is a COM
based lib and will have to be interoped.

5. Using Excel Object Library open the file and print to the PDF
Creator

6. When printing is finished: close excel file.
7. read the created PDF into memory(you will read PDF from dir
configured in step 2),

8. delete the source .xls,delete the .pdf you just read

9. Response.BinaryWrite() the file read in in step 7

10. Also in your existing code's header change attachment filename to
..pdf and change the ContentType to whatever the MIME type is for PDF
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top