CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2006
    Posts
    49

    Smile Multiline in a Excel Cell, When exporting data from datatable to Excel

    Hi All,

    I am trying to export datatable to excel. In the datatable in one of the column I am concatinating two strings. Ex: str1 and str2. When I export to excel. str1 should come in first line and str2 in second line in the same cell. Manually Alt+Enter works fine in Excel. I want to do it programatically. I tried vbCrLf. It is moving to next line. I want new line in the same Cell.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Multiline in a Excel Cell, When exporting data from datatable to Excel

    use vbLf or CHAR(10) , instead of vbcrlf. It will make it, as long as the cell allows to wrap text
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2006
    Posts
    49

    Re: Multiline in a Excel Cell, When exporting data from datatable to Excel

    I am not using interop. Can you post some sample code. Below is the code I am using for exporting datatable to Excel. In this case how can I wrap the cell.
    Code:
      Dim attachment As String = "attachment; filename=xyzReport" & Date.Now.Year & Date.Now.Month & Date.Now.Day & Date.Now.Ticks & ".xls"
            Response.ClearContent()
            Response.AddHeader("content-disposition", attachment)
            Response.ContentType = "application/vnd.ms-excel"
            Dim tab As String = ""
            For Each dc As DataColumn In dtExcel.Columns
                Response.Write(tab + dc.ColumnName)
                tab = vbTab
            Next
            Response.Write(vbLf)
            Dim j As Integer
            For Each dr As DataRow In dtExcel.Rows
                tab = ""
                For j = 0 To dtExcel.Columns.Count - 1
                    Response.Write(tab + dr(j).ToString())
                    tab = vbTab
                Next
                Response.Write(vbLf)
            Next
            Response.[End]()
    Last edited by Cimperiali; March 1st, 2010 at 06:58 PM. Reason: ...I added Code tags...

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Multiline in a Excel Cell, When exporting data from datatable to Excel

    ***EDIT**** Moved from vb Net Forum

    I am not able to see the two colums you're concatenating
    in any case:
    you're making it with tabs... try with a table, it might suite you a bit better.
    Ie: in an App_code Class
    Code:
    Imports Microsoft.VisualBasic
    Imports System.Data
    Imports System.Text
    Public Class DtToExcel
    
        Public Shared Function GenerateReport(ByVal dt As DataTable, ByVal Sv As System.Web.HttpServerUtility) As String
    
            Dim sb As New StringBuilder()
            sb.AppendLine("<table><tr>")
    
            For Each dc As DataColumn In dt.Columns
                sb.AppendLine("<td>" + Sv.HtmlEncode(dc.ColumnName) + "</td>")
    
            Next
            sb.AppendLine("</tr>")
    
            For Each dr As DataRow In dt.Rows
    
                sb.AppendLine("<tr>")
                For Each dc As DataColumn In dt.Columns
    
                    If dr(dc) IsNot Nothing AndAlso dr(dc) IsNot DBNull.Value Then
                        sb.AppendLine("<td>" + Sv.HtmlEncode(dr(dc).ToString()) & "</td>")
                    Else
    
                        sb.AppendLine("<td></td>")
                    End If
                Next
                sb.AppendLine("</tr>")
    
            Next
    
    
            sb.AppendLine("</table>")
            Return sb.ToString()
    
        End Function
    
    End Class
    in a web form
    Code:
     Private Sub showReport()
            '
    
            Response.AddHeader("Content-Disposition", "attachment;filename=Report_" & Now.Ticks & ".xls")
            Response.Buffer = True
            Response.Charset = String.Empty
            Response.ContentType = "application/vnd.ms-excel"
    
            'a method that give me back the datatable I need here
           'you should get your own dt...
            Dim dt As System.Data.DataTable = Manager.GetDt("Customers")
    
            Response.Write(DtToExcel.GenerateReport(dt, Server))
            Response.Flush()
            Response.End()
    
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            showReport()
        End Sub
    Last edited by Cimperiali; March 1st, 2010 at 08:29 AM. Reason: inform this is a moved thread from Vb Net forum
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Multiline in a Excel Cell, When exporting data from datatable to Excel

    and then, to get what you want, you can add the proper style to the <br /> you want to behave as alt+Enter:
    Code:
                    If dr(dc) IsNot Nothing AndAlso dr(dc) IsNot DBNull.Value Then
    
                        sb.AppendLine("<td>" & Sv.HtmlEncode(dr(dc).ToString()) & "<br style='mso-data-placement:same-cell;' />Second Line in single cell</td>")
                    Else
    
                        sb.AppendLine("<td></td>")
                    End If
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    May 2012
    Posts
    1

    Re: Multiline in a Excel Cell, When exporting data from datatable to Excel

    If you are using response.write to export the gridview to excel, then add the line
    string brstyle = @"<style>br { mso-data-placement:same-cell; }</style>";
    Response.Write(brstyle);
    just before exporting the gridview. Hope this helps somebody like myself..

  7. #7
    Join Date
    Mar 2012
    Posts
    6

    Re: Multiline in a Excel Cell, When exporting data from datatable to Excel

    Hi you can check this article on codeproject: http://www.codeproject.com/Articles/...el-for-ASP-NET

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured