Results 1 to 14 of 14

Thread: [RESOLVED] Download to Excel from ASP.NET page

  1. #1
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Resolved [RESOLVED] Download to Excel from ASP.NET page

    I have a button on my aspx page to download the gridview data to Excel. I am using SpreadsheetGear.dll. It worked nicely until the customer asked me to format the currency fields.

    So this is my loop to format:

    Code:
                System.Data.DataTable dtWarranties;
                dtWarranties = ds.Tables[0];
    
                // Format currency fields.
                double doubleOut;
                for (int i = 0; i < dtWarranties.Rows.Count; ++i)
                { 
                    for (int j = 3; j < 6; ++j)
                    {
                        if (Double.TryParse(Convert.ToString(dtWarranties.Rows[i].ItemArray[j]), out doubleOut))
                            dtWarranties.Rows[i].ItemArray[j] = String.Format("${0:0.00}", doubleOut);  
                    }
                }
    As I am stepping through debugging, I query this:
    ? String.Format("${0:0.00}", doubleOut)
    "$1569.00

    But I query the left side of the assignment, and it is still unformatted:
    ? dtWarranties.Rows[i].ItemArray[j]
    "1569"


    Why won't it "take" the format?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    Re: Download to Excel from ASP.NET page

    Try
    Code:
    dtWarranties.Rows[i][columnIndex]= String.Format("${0:0.00}", doubleOut);
    I would also check what's the data type for the column in question.

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,733

    Re: Download to Excel from ASP.NET page

    Quote Originally Posted by rjv_rnjn View Post
    I would also check what's the data type for the column in question.
    I think that this could be the issue.

    Assuming the datatype is a numeric, you can't provide it a string, without it first "converting" it for you, hence losing the formatting.

    I haven't used SpreadsheetGear before, but I would have thought that this is something that would be "baked" in. Is there not an option to provide formatting when the export happens?

    Gary

  4. #4
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: Download to Excel from ASP.NET page

    Shoot - it already is a string!

    I am getting so stressed out. This report is way over budget and I can't get this simplest last thing working.

    Here is my dataset when I create it. Every column is a string:
    Code:
                DataSet dsReport = new DataSet();
                DataTable dtReport = dsReport.Tables.Add();
                //writeLog(string.Format("WarrantyReport: datatable created"));
    
                dtReport.Columns.Add("Warranty Type", typeof(string));
                dtReport.Columns.Add("# of Warranties", typeof(string));
                dtReport.Columns.Add("# of Squares", typeof(string));
                dtReport.Columns.Add("Promo $", typeof(string));
                dtReport.Columns.Add("CC $", typeof(string));
                dtReport.Columns.Add("Total Warranty Value", typeof(string));
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    Re: Download to Excel from ASP.NET page

    So did you try my suggested code -- assigning to [row][column] instead of ItemArray?

  6. #6
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: Download to Excel from ASP.NET page

    And by the way, Gary said, Assuming the datatype is a numeric, you can't provide it a string, without it first "converting" it for you, hence losing the formatting. doesn't this show the datatype is NOT numeric?

    ? dtWarranties.Rows[i].ItemArray[j]
    "1569"

    Wouldn't it display in the debugger as 1569 without quotation marks if it were numeric?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  7. #7
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: Download to Excel from ASP.NET page

    Quote Originally Posted by rjv_rnjn View Post
    So did you try my suggested code -- assigning to [row][column] instead of ItemArray?
    No, I did not. I'll try that now!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  8. #8
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: Download to Excel from ASP.NET page

    Sorry I didn't jump at that right away! It worked. I don't understand why, what is the difference between that and the way I was doing it originally?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  9. #9
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    Re: Download to Excel from ASP.NET page

    From what I understand, I would have expected the compiler to throw an error or maybe a runtime error. MSDN (http://msdn.microsoft.com/en-us/libr...itemarray.aspx) says the setter value has to be an object array, it does not discuss about individually setting values.

    I know this is not a very correct/thorough technical explanation, but I am not good at that. I will try and see if I can find any blog post that has taken a swipe at this.

  10. #10
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: Download to Excel from ASP.NET page

    I am a VB.NET programmer and only very recently a C# programmer. Accessing elements in a dataset are different in the two languages. Did I just *mistakenly* stumble upon ItemArray and I should be double indexing my elements with [i][j]?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  11. #11
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    Re: Download to Excel from ASP.NET page

    Stumbled upon the 3.5 version of the same page (http://msdn.microsoft.com/en-us/libr...(v=VS.90).aspx) and saw a user comment that the ItemArray get gives you a copy of the ItemArray. So any modifications would not show up in the original. And from what you've reported that comment seems to be verifiable.

    Quote Originally Posted by MMock View Post
    Did I just *mistakenly* stumble upon ItemArray and I should be double indexing my elements with [i][j]?
    Well I use ItemArray in watch windows (at times I don't like loading the whole datatable while debugging), so even if you stumbled across it, it's good.

    You are not stuck accessing them through indices, you can use the column name too.

    While looking more into this, I found a few extension methods called SetField<T> (http://msdn.microsoft.com/en-us/library/bb353039) and I think those seem to be better as they can handle nullable types better
    If value is null, the SetField method converts the null value to Value in the underlying DataColumn.
    Thank you for this.

  12. #12
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: Download to Excel from ASP.NET page

    If you give me your address, I will send you my paycheck for today
    Last edited by MMock; Jul 19th, 2012 at 10:11 AM. Reason: typo
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  13. #13
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    Re: Download to Excel from ASP.NET page

    Quote Originally Posted by MMock View Post
    If you give me your address, I will send you my paycheck for today
    I had like that.

  14. #14
    PowerPoster MMock's Avatar
    Join Date
    Apr 07
    Location
    Sitting in an empty room, trying to forget the past
    Posts
    3,214

    Re: [RESOLVED] Download to Excel from ASP.NET page

    At least I added to your reputation! Thanks again.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •