Change column width during Excel export

L

Lester Mosley

I am having trouble adjusting the column width when exporting from an
ASP.net web page.

I don't really want to have a sotred formated excel page and prefer to
do it by code within the display page. (When user enters the data and
requests report to run, it brings back a new page, then the user can
clcik on a button to export to excel)

I have achieved in formatting the header to a different color, as well
as adding a header with what report they have run, or add dates if it
was a custom date range. using the lower code

Response.Write("<font size=4><center>" & "Report: " & lbl_title.Text &
" " & Now.Date & "</center></font>") ' Adds title to Excel Sheet

dg_Report.HeaderStyle.BackColor = Drawing.Color.LightSkyBlue ' Colors
header row in grid and not sheet.

dg_Report.ItemStyle.VerticalAlign = VerticalAlign.Top


However I am unable to make a column width wider at all.. is there any
suggestions?
I have tried the below code with out success: (and yes I know they are
commented out at the moment, I just copied direct from code..

'dg_Report.Columns(9).ItemStyle.Width.Pixel(450) ' -- Won't work
'dg_Report.HeaderStyle.Width.Equals(20) ' -- Won't Work
'dg_Report.Columns("Description").HeaderStyle.Width =
System.Web.UI.WebControls.Unit.Pixel(100) ' -- Won't Work
'dg_Report.Columns(0).ItemStyle.Wrap = True ' -- Won't work
'dg_Report.Columns(9).ItemStyle.Width.Pixel(90) ' --Won't work
 
M

Masudur

I am having trouble adjusting the column width when exporting from an
ASP.net web page.

I don't really want to have a sotred formated excel page and prefer to
do it by code within the display page. (When user enters the data and
requests report to run, it brings back a new page, then the user can
clcik on a button to export to excel)

I have achieved in formatting the header to a different color, as well
as adding a header with what report they have run, or add dates if it
was a custom date range. using the lower code

Response.Write("<font size=4><center>" & "Report: " & lbl_title.Text &
" " & Now.Date & "</center></font>")    ' Adds title to Excel Sheet

dg_Report.HeaderStyle.BackColor = Drawing.Color.LightSkyBlue  ' Colors
header row in grid and not sheet.

dg_Report.ItemStyle.VerticalAlign = VerticalAlign.Top

However I am unable to make a column width wider at all.. is there any
suggestions?
I have tried the below code with out success: (and yes I know they are
commented out at the moment, I just copied direct from code..

'dg_Report.Columns(9).ItemStyle.Width.Pixel(450) ' -- Won't work
'dg_Report.HeaderStyle.Width.Equals(20) ' -- Won't Work
'dg_Report.Columns("Description").HeaderStyle.Width =
System.Web.UI.WebControls.Unit.Pixel(100) ' -- Won't Work
 'dg_Report.Columns(0).ItemStyle.Wrap = True ' -- Won't work
 'dg_Report.Columns(9).ItemStyle.Width.Pixel(90) ' --Won't work

Hi..

how about traverse the rows of datagrid and manually generate the
report using a html table...

http://aspalliance.com/725_CodeSnip_Export_a_DataGrid_to_a_Formatted_Excel_Spreadsheet

and set the html table's cell that is <TD> is width to a specific
width...

Not sure though cause i found a post that says its not going to
happen...

http://www.velocityreviews.com/forums/t112076-export-to-excel-from-aspnet.html

Thanks
Masudur
 
B

bruce barker

if you want to specify columns withds, multiple sheets, sheet name, etc. you
shoudl be producing an xml work book. the xml is easy, and you can specifiy
all the properties. a simple wookbook:

<?xml version="1.0"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet">
<ss:Worksheet ss:Name="MySheet">
<ss:Table>
<ss:Column ss:Width="80"/>
<ss:Column ss:Width="80"/>
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">Hello</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">World</ss:Data>
</ss:Cell>
</ss:Row>
</ss:Table>
</ss:Worksheet>
</ss:Workbook>


-- bruce (sqlwork.com)
 
R

rote

Bruce i'm just curious i would you tell excel to use this xml when
exporting?
Thanks in advance
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top