Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2003, 11:31 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel export using datagrid

Hi ..
I am using datagrid to render my data in excel format.

following is the code
gridExcel.DataSource = viewExcel;
gridExcel.DataBind();
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
gridExcel.RenderControl(textWriter);
Response.Write(stringWriter.ToString());
Response.End();

This code is rendering it as excel but the problem is ..in my query there is field called serial number, this is a varchar field, but contains numeric data ie 000123456..etc..when rendering it is considering it as a numeric field and truncating the trailing zeros..
Can anybody tell me what should I do to solve this problem ?

Thanks,
Gibi

 
Old August 21st, 2003, 11:59 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

No guarantee's but you might try prefixing text numbers with a single appostrophy ('). I know that if you are actually in Excel and you type "'0012345" (with that leading ' you get a string. I don't know if this would also work when you generate the data from HTML, but it's worth a shot.

If you don't want to change your source data (which you probably won't) use the datagrid's ItemDataBound event to modify each datagriditem's column value.

Peter

 
Old September 4th, 2003, 06:16 PM
Registered User
 
Join Date: Sep 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The single quote does not work.

After much plowing around, there is a work around for this.

You need to set a style and style attribute for the cells to be formatted in the grid.

The only way I could seem to pass the style through to the Excel sheet was to force it with a string append.

Here is my code to format all of the cells to a "String" format in Excel.

            response.Clear();
            response.Charset = "";
            response.ContentType = "application/vnd.ms-excel";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            DataGrid dg = new DataGrid();
            dg.DataSource = ds.Tables[0];
            dg.DataBind();
            for (int rowPos = 0; rowPos < dg.Items.Count; ++rowPos)
            {
                for (int colPos = 0; colPos < dg.Items[rowPos].Cells.Count; ++colPos)
                {
                    dg.Items[rowPos].Cells[colPos].Attributes.Add("class","NumberString");
                }
            }
            dg.RenderControl(htmlWrite);
            string styleInfo = @"<style>.NumberString {mso-number-format:\@;}</style>";
            response.Write(styleInfo + stringWrite.ToString());
            response.End();

PG
 
Old November 19th, 2003, 07:16 AM
FBA FBA is offline
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am working with JSPs and tried this to display the trailing zeros.

For example:

<td STYLE="vnd.ms-excel.numberformat:@"> 000001111 </td>
 
Old November 19th, 2003, 08:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

Check out http://www.greggriffiths.org/webdev/both/excel/ while aimed at Classic ASP, the code and basics presented should be of assistance.
 
Old September 23rd, 2004, 07:57 AM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just wanted to say thanks. This came in handy for a similiar problem I was having. Just curious, do you know of a good resource for the "keys" parameters for the Attribute.Add function?

Thanks again,


Quote:
quote:Originally posted by pgoth
 The single quote does not work.

After much plowing around, there is a work around for this.

You need to set a style and style attribute for the cells to be formatted in the grid.

The only way I could seem to pass the style through to the Excel sheet was to force it with a string append.

Here is my code to format all of the cells to a "String" format in Excel.

            response.Clear();
            response.Charset = "";
            response.ContentType = "application/vnd.ms-excel";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            DataGrid dg = new DataGrid();
            dg.DataSource = ds.Tables[0];
            dg.DataBind();
            for (int rowPos = 0; rowPos < dg.Items.Count; ++rowPos)
            {
                for (int colPos = 0; colPos < dg.Items[rowPos].Cells.Count; ++colPos)
                {
                    dg.Items[rowPos].Cells[colPos].Attributes.Add("class","NumberString");
                }
            }
            dg.RenderControl(htmlWrite);
            string styleInfo = @"<style>.NumberString {mso-number-format:\@;}</style>";
            response.Write(styleInfo + stringWrite.ToString());
            response.End();

PG
 
Old September 24th, 2004, 08:18 AM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, I have a related problem with exporting to Excel using a datagrid. I'm currently supporting pagination and sorting on my datagrid using caching on the underlying dataset. When I export to Excel, I first disable paging and sorting, and for the most part the resulting Excel export works fine. The only minor glitch is that if the user has previously sorted the datagrid, my column header in Excel is replaced by the up or down arrow I've added to indicate sort direction. Note that this isn't a problem on the datagrid itself, only on the resulting Excel output.

Here's the code that adds the sort arrow:

For i = 0 To dgTestExportDataGrid.Columns.Count - 1
      Dim columnSortExpression As String = dgTestExportDataGrid.Columns(i).SortExpression
      If simpleSortExpression = columnSortExpression And columnSortExpression <> "" Then
        Dim cell As TableCell = e.Item.Cells(i)
        Dim lblSorted As New Label
        lblSorted.ID = "lblArrow"
        lblSorted.Font.Name = "webdings"
        lblSorted.Font.Size = FontUnit.XSmall
        If isDescending = True Then
          lblSorted.Text = " 6"
        Else
          lblSorted.Text = " 5"
        End If
        cell.Controls.Add(lblSorted)
      End If
    Next

Also note that my actual Excel export code is very similar to those posted earlier in the thread (using an HTMLTextWriter, etc.) Any help greatly appreciated.

Thanks,
Jeremy

 
Old January 5th, 2005, 02:33 AM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
 No guarantee's but you might try prefixing text numbers with a single appostrophy ('). I know that if you are actually in Excel and you type "'0012345" (with that leading ' you get a string. I don't know if this would also work when you generate the data from HTML, but it's worth a shot.

If you don't want to change your source data (which you probably won't) use the datagrid's ItemDataBound event to modify each datagriditem's column value.

Peter

 
Old January 5th, 2005, 03:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you have control over Excel and can run VBA then you can set the number format of the columns, if however you are just posting the file vai the browser using the Content Type then you will need to prefix the fields with a single apostrophe '
 
Old June 27th, 2005, 08:12 AM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good postings, very useful






Similar Threads
Thread Thread Starter Forum Replies Last Post
Export DataGrid To Excel in vb.net jayakumar.cj ASP.NET 1.0 and 1.1 Basics 3 September 1st, 2010 03:09 AM
EXPORT DATAGRID TO EXCEL FORMATTING PROBLEM akshay144 VS.NET 2002/2003 2 October 31st, 2006 01:10 PM
export the contents of Datagrid to Excel file softsea VS.NET 2002/2003 0 October 4th, 2006 02:38 PM
export datagrid to excel with configured width sn00pyg4rfi3ld ASP.NET 1.x and 2.0 Application Design 0 October 25th, 2005 05:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.