Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. (SOLVED) StringBuilder error

(SOLVED) StringBuilder error

Scheduled Pinned Locked Moved ASP.NET
helpquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    Hi all, We are currently having problem with data exported to excel. When we review the exported file, some show values with exponential format. To try to work around that, I have the code below with stringbuilder to fix the exponential format issue.

    Public Sub GetExcel(ByVal dt As DataTable)
        Dim fileName As String = "file" & DateTime.Now.ToString("MMddyyyy") & ".xls"
        Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
        Response.ContentType = "application/vnd.ms-excel"
        Dim stringWriter As StringWriter = New StringWriter()
        Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
        Dim dtExportExcel As DataGrid = New DataGrid()
        dtExportExcel.DataSource = dt
        dtExportExcel.DataBind()
        dtExportExcel.RenderControl(htmlWrite)
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
        sb.Append("  table { mso-number-format:'0'; }  ")
        sb.Append(stringWriter & "")
        Response.Write(sb.ToString())
        Response.\[End\]()
    End Sub
    

    When I run the code, I get an error on this line:

    sb.Append(stringWriter & "")

    The error says, Operator '&' is s not defined for types 'StringWriter' and 'String' Any ideas what this means? Thanks in advance

    L Richard DeemingR 2 Replies Last reply
    0
    • S samflex

      Hi all, We are currently having problem with data exported to excel. When we review the exported file, some show values with exponential format. To try to work around that, I have the code below with stringbuilder to fix the exponential format issue.

      Public Sub GetExcel(ByVal dt As DataTable)
          Dim fileName As String = "file" & DateTime.Now.ToString("MMddyyyy") & ".xls"
          Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
          Response.ContentType = "application/vnd.ms-excel"
          Dim stringWriter As StringWriter = New StringWriter()
          Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
          Dim dtExportExcel As DataGrid = New DataGrid()
          dtExportExcel.DataSource = dt
          dtExportExcel.DataBind()
          dtExportExcel.RenderControl(htmlWrite)
          Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
          sb.Append("  table { mso-number-format:'0'; }  ")
          sb.Append(stringWriter & "")
          Response.Write(sb.ToString())
          Response.\[End\]()
      End Sub
      

      When I run the code, I get an error on this line:

      sb.Append(stringWriter & "")

      The error says, Operator '&' is s not defined for types 'StringWriter' and 'String' Any ideas what this means? Thanks in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It means you cannot add a simple string to a StringWriter object, as it makes no sense. Furthermore you cannot append a StringWriter object to a StringBuilder. You can only append strings (or the string values of objects) to a StringBuilder. See StringWriter Constructor (System.IO) | Microsoft Learn[^] for the correct way to combine the two.

      S 1 Reply Last reply
      0
      • L Lost User

        It means you cannot add a simple string to a StringWriter object, as it makes no sense. Furthermore you cannot append a StringWriter object to a StringBuilder. You can only append strings (or the string values of objects) to a StringBuilder. See StringWriter Constructor (System.IO) | Microsoft Learn[^] for the correct way to combine the two.

        S Offline
        S Offline
        samflex
        wrote on last edited by
        #3

        Ok, thank you very much for your response. So, this would have been the correct way?

        sb.Append("")

        L 1 Reply Last reply
        0
        • S samflex

          Ok, thank you very much for your response. So, this would have been the correct way?

          sb.Append("")

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Yes, You can only add strings, or "things" that can be expressed as strings.

          S 1 Reply Last reply
          0
          • L Lost User

            Yes, You can only add strings, or "things" that can be expressed as strings.

            S Offline
            S Offline
            samflex
            wrote on last edited by
            #5

            Thank you. I saw a similar code but written in C# that has exact same code and users say it worked for them. Thank you for your help. I will try this and hopefully, it works.

            L 2 Replies Last reply
            0
            • S samflex

              Thank you. I saw a similar code but written in C# that has exact same code and users say it worked for them. Thank you for your help. I will try this and hopefully, it works.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You are writing in C#, or were you not aware?

              1 Reply Last reply
              0
              • S samflex

                Thank you. I saw a similar code but written in C# that has exact same code and users say it worked for them. Thank you for your help. I will try this and hopefully, it works.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Sorry about the other message. But looking at your code again I do not see why the StringWriter, or the HtmlTextWriter, are there, as apart from the following two lines:

                Dim stringWriter As StringWriter = New StringWriter()
                Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
                

                You never refer to either object (other than in the line with the error).

                S 1 Reply Last reply
                0
                • L Lost User

                  Sorry about the other message. But looking at your code again I do not see why the StringWriter, or the HtmlTextWriter, are there, as apart from the following two lines:

                  Dim stringWriter As StringWriter = New StringWriter()
                  Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
                  

                  You never refer to either object (other than in the line with the error).

                  S Offline
                  S Offline
                  samflex
                  wrote on last edited by
                  #8

                  Right, you are correct. I have actually removed them from there but added them some place else like:

                  dtExportExcel.RenderControl(htmlWrite)
                  Response.Write(stringWriter.ToString())

                  1 Reply Last reply
                  0
                  • S samflex

                    Hi all, We are currently having problem with data exported to excel. When we review the exported file, some show values with exponential format. To try to work around that, I have the code below with stringbuilder to fix the exponential format issue.

                    Public Sub GetExcel(ByVal dt As DataTable)
                        Dim fileName As String = "file" & DateTime.Now.ToString("MMddyyyy") & ".xls"
                        Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
                        Response.ContentType = "application/vnd.ms-excel"
                        Dim stringWriter As StringWriter = New StringWriter()
                        Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
                        Dim dtExportExcel As DataGrid = New DataGrid()
                        dtExportExcel.DataSource = dt
                        dtExportExcel.DataBind()
                        dtExportExcel.RenderControl(htmlWrite)
                        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
                        sb.Append("  table { mso-number-format:'0'; }  ")
                        sb.Append(stringWriter & "")
                        Response.Write(sb.ToString())
                        Response.\[End\]()
                    End Sub
                    

                    When I run the code, I get an error on this line:

                    sb.Append(stringWriter & "")

                    The error says, Operator '&' is s not defined for types 'StringWriter' and 'String' Any ideas what this means? Thanks in advance

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    As a simple fix:

                    Dim sb As New System.Text.StringBuilder()
                    sb.Append(" table { mso-number-format:'0'; } ")

                    Dim stringWriter As New StringWriter(sb)
                    Dim htmlWrite As New HtmlTextWriter(stringWriter)
                    Dim dtExportExcel As New DataGrid()
                    dtExportExcel.DataSource = dt
                    dtExportExcel.DataBind()
                    dtExportExcel.RenderControl(htmlWrite)

                    sb.Append("")
                    Response.Write(sb.ToString())
                    Response.[End]()

                    However, note that you are not "exporting an Excel file"; you are rendering HTML content, but lying to the browser and claiming that it's an Excel file. Excel will display a warning message, and then do its best to import that HTML into a new spreadsheet, but you'll get extremely limited control over the results. If you actually want to export an Excel file, with precise control over the formatting and layout, then use a library designed to do that - for example, ClosedXML[^] or the Open XML SDK[^].


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    S 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      As a simple fix:

                      Dim sb As New System.Text.StringBuilder()
                      sb.Append(" table { mso-number-format:'0'; } ")

                      Dim stringWriter As New StringWriter(sb)
                      Dim htmlWrite As New HtmlTextWriter(stringWriter)
                      Dim dtExportExcel As New DataGrid()
                      dtExportExcel.DataSource = dt
                      dtExportExcel.DataBind()
                      dtExportExcel.RenderControl(htmlWrite)

                      sb.Append("")
                      Response.Write(sb.ToString())
                      Response.[End]()

                      However, note that you are not "exporting an Excel file"; you are rendering HTML content, but lying to the browser and claiming that it's an Excel file. Excel will display a warning message, and then do its best to import that HTML into a new spreadsheet, but you'll get extremely limited control over the results. If you actually want to export an Excel file, with precise control over the formatting and layout, then use a library designed to do that - for example, ClosedXML[^] or the Open XML SDK[^].


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      S Offline
                      S Offline
                      samflex
                      wrote on last edited by
                      #10

                      WOW, knowledge is power. Thank you very much sir and great to hear from you again :) With your advise, I was able to successfully use ClosedXML and it is working great. I still have this solution you provided here as a back up. Thank you again.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups