473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to export DataGrid to an existing Excel sheet?

How can I export a DataGrid in ASP.NET to an existing Excel Workbook and place the gridview data in the first sheet?

Existing Excel workbook is found on c:/Project/Inm.xls

Below is my code to save the datagrid in excel:
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub btnreport_Click(sender As Object, e As System.EventArgs) Handles btnreport.Click
  2.  
  3.  
  4.         Response.Clear()
  5.         Response.AddHeader("content-disposition", "attachment;filename=Invoice" & Now.ToString("ddMMyyyy_hhmmss") & ".xls")
  6.         Dim mystringwriter As New IO.StringWriter()
  7.         Dim myhtmltextwrite As New HtmlTextWriter(mystringwriter)
  8.         GridView1.RenderControl(myhtmltextwrite)
  9.         Response.Write(mystringwriter.ToString())
  10.         Response.End()
  11.  
  12.     End Sub
Aug 13 '11 #1
1 5997
All experts and no one was able to reply ! Wow..

For anyone who needs this code, here it is, please note that I didn't add a new sheet to the workbook, I kept the first sheet empty...

Thanks for whoever tried though !

Expand|Select|Wrap|Line Numbers
  1. Protected Sub btnreport_Click(sender As Object, e As System.EventArgs) Handles btnreport.Click
  2.  
  3.         Dim Excelapp = New Excel.Application()
  4.  
  5.         Dim Book As New Object
  6.         Dim Sheet As New Object
  7. 'Open Existing Workbook
  8.         Book = Excelapp.Workbooks.Open("C:\Project\Invoices.xls")
  9. 'Select first sheet in the workbook and work on it
  10.         Book.Worksheets(1).Select()
  11. 'Get the rows in the datagrid but not the Header row
  12.         For i As Integer = 0 To GridView1.Rows.Count - 1
  13.             Dim row As GridViewRow = GridView1.Rows(i)
  14.             For j As Integer = 0 To row.Cells.Count - 1
  15.                 Dim s As Integer = 1 + i
  16.                 Excelapp.Cells(s + 1, j + 1) = row.Cells(j).Text
  17.             Next
  18.         Next
  19. 'Save new book with the name "Invoice" + today's date and time
  20.         Book.saveas("C:\Project" & "Invoice" & Now.ToString("ddMMyyyy_hhmmss") & ".xls")
  21.  
  22.         Excelapp.Quit()
  23.  
  24.  
  25.     End Sub
Aug 17 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ugo | last post by:
Hi, I am having a problem exporting a Datagrid to excel in asp.net. For some reason I am getting a blank excel page. I set up in IIS the Mime for ..xls. My code is below any help would be...
3
by: Bidarkota | last post by:
When i export DataGrid to Excel all the HTML contents are also exporting to excel. i am using stylesheets in the ASPX Page and i am getting an alert message that stylesheets are missing. i need to...
1
by: Ed Chiu | last post by:
Hi, I copied the following codes from Internet to export datagrid to Excel, it works with datagrid not supporting sorting or paging. Response.ContentType = "application/vnd.ms-excel"...
0
by: Sandeep Pulavarty via .NET 247 | last post by:
Hi, I am working on a project in VB.NET and Excel and i wouldlike to add an existing Excel sheet(which is not empty) into mycurrent workbook. I cant use SaveAs because the number of sheetsthat i...
1
by: friendlyrohit | last post by:
Hi All !!! Can anybody help me out, how to retrieve a desired column's data from an existing excel sheet through the vb code....... Dim strData As String Dim ts As TextStream ...
1
by: abhiab54 | last post by:
Hi, I want to open already existing excel sheet search for perticular string & write specific value below the cell containing that string. Please help I have limited knowledge of Vb, Perticularly...
1
by: ielamrani | last post by:
Hi, I have the following code which works fine. It export current access form record to an excel sheet: DoCmd.OutputTo acOutputForm, "frmsubformtest", acFormatXLS, "C:\Details.xls", True Is it...
2
by: amuven | last post by:
Hi All, I'm new to python I have written very basic and small code and I request to help for the following senario.For my project i want to write a python code which should open an already...
11
by: Blacky | last post by:
hi, I have a dataTable in Session.On the click of a button in a Modal Dialog window I need to export this dataTable to Excel sheet.How could i do this...?? i tried using a...
2
by: ram712 | last post by:
i need to create/add a copy of an excel sheet to an existing excel sheet consisting of table data so that the new excel sheet renamed automatically. also, the new excel sheet should be blank but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.