The contents of a datagrid (which is actually the contents of a datatable) can be output and written to an Excel file using Oledb with no need of automating Excel.
|
|
Hi,
I'm providing a link button in my page(which contains a datagrid) so that when the user clicks on it they will be able to see the contents of the datagrid in a separate excel file.I provide the user with the option of attaching documents while they use my application.I would want that particular attachment to be embedded into excel document along with other contents.Is there a way to do this??
Note:I don't want to specify the link where the attachment is present.Instead I would need the attached document to be embedded into the excel file.
The contents of a datagrid (which is actually the contents of a datatable) can be output and written to an Excel file using Oledb with no need of automating Excel.
The easiest way to find such code is to start "record macro" in Excel and do the action manually, then look at the VBA code, the generated code will be much like the code you have to write in VB when automating the Office objects, at least you can in most cases figure out the "trick" about how it is done. In this case inserting an object from file into the Excel book will generate code like this:
Automating Excel from VB is pretty basic, add the reference to the Excel object library, import the namespace for easier coding, create Excel application object, create/open the book, manipulate it and close down. Here's a sample that does that, since I use Option Strict I do some type casts, but notice the similarities with the VBA code.Code:ActiveSheet.OLEObjects.Add(filename and some arguments...)
Code:Imports Excel = Microsoft.Office.Interop.ExcelIf you're a "late-binder" then you can reduce three of the code lines and the casting to this:Code:Dim app As New Excel.Application Dim book As Excel.Workbook = app.Workbooks.Add Dim sheet As Excel.Worksheet = CType(book.ActiveSheet, Excel.Worksheet) Dim oleobj As Excel.OLEObjects = CType(sheet.OLEObjects, Excel.OLEObjects) oleobj.Add(Link:=False, DisplayAsIcon:=False, Filename:="D:\path\Word.doc") book.SaveAs("D:\path\Excel.xls") book.Close() app.Quit()
That's really close to the starting point, right?Code:book.ActiveSheet.OLEObjects.Add(..same params)
Despite the naming I don't think you can add Ole objects with a OleDB data export, usually it is only for tabular string/numeric data and none of the formatting or application specific features.
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks