473,782 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting Datagrid Export to Excel

Hello,
Below is my code for exporting a datagrid to Excel. It works fine, but
we're hoping to format the output as well - setting the font size and type,
and giving each column a specific width, possibly bolding some text.
Currently we are using a macro to do this, but we would like the report to be
formatted when it's generated so that a macro will be unnecessary.
Thanks!
John

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Dim objConn As New System.Data.Sql Client.SqlConne ction(strConnSt ring)
objConn.Open()

Dim strSQL As String
Dim objDataset As New DataSet
Dim objAdapter As New System.Data.Sql Client.SqlDataA dapter

strSQL = "exec " & sDBAllianceSync & ".dbo.spTrucker Planning
@StartDiv, @EndDiv, @StartTeam, @EndTeam, @StartETAPOE, @EndETAPOE "

objAdapter.Sele ctCommand = New
System.Data.Sql Client.SqlComma nd(strSQL, objConn)
objAdapter.Sele ctCommand.Param eters.Add("@Sta rtDiv",
Session("Starti ngDivision"))
objAdapter.Sele ctCommand.Param eters.Add("@End Div",
Session("Ending Division"))
objAdapter.Sele ctCommand.Param eters.Add("@Sta rtTeam",
Session("Starti ngTeam"))
objAdapter.Sele ctCommand.Param eters.Add("@End Team",
Session("Ending Team"))
objAdapter.Sele ctCommand.Param eters.Add("@Sta rtETAPOE",
Session("Starti ngETAPOE"))
objAdapter.Sele ctCommand.Param eters.Add("@End ETAPOE",
Session("Ending ETAPOE"))
objAdapter.Sele ctCommand.Comma ndTimeout = 120

' Fill the dataset.
objAdapter.Fill (objDataset)

' Create a new view.
Dim oView As New DataView(objDat aset.Tables(0))
' Set up the data grid and bind the data.
DataGrid1.DataS ource = oView
DataGrid1.DataB ind()

' Set the content type to Excel.
Response.Conten tType = "applicatio n/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charse t = ""
' Turn off the view state.
Me.EnableViewSt ate = False

Dim tw As New System.IO.Strin gWriter
Dim hw As New System.Web.UI.H tmlTextWriter(t w)

' Get the HTML for the control.
DataGrid1.Rende rControl(hw)
' Write the HTML back to the browser.
Response.Write( tw.ToString())
' End the response.
Response.End()
End Sub

May 10 '06 #1
2 1634
You'll need to add the styles used by the datagrid into the HTML stream
as inline style definitions.

John H.

May 10 '06 #2
Would you have sample code? I don't have an idea about how to add styles
into an HTML stream.
Thanks!
John

"JohnH" wrote:
You'll need to add the styles used by the datagrid into the HTML stream
as inline style definitions.

John H.

May 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5896
by: Eric | last post by:
Dear All, I want to export datagrid content to a Excel file, the code just like below: Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) gridList.Columns(0).Visible = False
2
2482
by: Bidarkota | last post by:
Hi, I have a DataGrid in which there are some images and Data are displayed and in the webform.asp page i am using some stylesheets. when i export the datagrid all the images are also exported to datagrid and it is giving an alert that following stylesheets are missing when i export it. can anyone help me about this problem. I have seen the sites and articles regarding the datagrid export to excel. but still i am getting the same...
3
4354
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 export only the contents of the Datagrid to excel. i don't need all other stuff which is present on the page. is there any way to achieve it. Thanks in Advance
3
1670
by: mattdaddym | last post by:
Hi all, I have been using the following code to export my datagrids to excel for quite some time. Very simple and very effective. <code> Sub btnExcelExport_Click ( s As Object, e As EventArgs ) Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
0
2935
by: kieran | last post by:
Hi, I have an 'export to excel' button on my Datagrid. All works well except that on my Datagrid, I have sub total rows. The subtotal rows use colspan so that the subtotals are given above certain columns When I export to excel, I do not have the colspan. As a result, the subtotals appear over the wrong columns in Excel.
6
3732
by: Sridhar | last post by:
Hi, I have to export data inside the datagrid into an excel worksheet. I have to do this on button click event. I am trying to do like this. First get the DataTable from the datagrid and iterate through each row to get the data. I am not able to get the DataTable from the datagrid. How could I do that? I tried like this but it is not working Dim dt as DataTable = CType(datagrid1.DataSource, DataTable)
4
3954
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair out at this point. What I have is this : Members.aspx file:
10
9695
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through each tab and apply autowidth to columns and apply autofilter to the first row of every tab in the workbook. I've this down
6
2738
by: slinky | last post by:
I found the following code to transfer datagrid data to an Excel file. Is this written in C#?... I'm a vb.netter. I'm just not sure where to place the code to experiment on it. Should I place it in the event handler for a form button. Is there other ways to accomplish this? I looked at some on the web and usergroups, but was confused as to the functioning. BTW my datagrid is enclosed in a <div></divfor ease of scrolling if that would...
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.