Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here some code we use to do it across lots of our apps. We have a special method to clean up "not exportable" column. Additionally, we don't export cols without headers but you can adjust that logic to your needs.</p> <p>Edit: The code formatter doesn't love vb.net - you can copy/paste into visual studio and it will be fine.</p> <pre><code> Public Overloads Shared Function BuildExcel(ByVal gView As System.Web.UI.WebControls.GridView) As String PrepareGridViewForExport(gView) Dim excelDoc As New StringBuilder Dim startExcelXML As String = " " + _ " " + _ " " + _ " " + _ " " + _ " " + _ " " + _ " " + _ " " Dim endExcelXML As String = "" Dim rowCount As Int64 = 0 Dim sheetCount As Int16 = 1 excelDoc.Append(startExcelXML) excelDoc.Append("") excelDoc.Append("") ' write out column headers excelDoc.Append("") For x As Int32 = 0 To gView.Columns.Count - 1 'Only write out columns that have column headers. If Not gView.Columns(x).HeaderText = String.Empty Then excelDoc.Append("") excelDoc.Append(gView.Columns(x).HeaderText.ToString) excelDoc.Append("") End If Next excelDoc.Append("") For r As Int32 = 0 To gView.Rows.Count - 1 rowCount += rowCount If rowCount = 64000 Then rowCount = 0 sheetCount += sheetCount excelDoc.Append("") excelDoc.Append(" ") excelDoc.Append("") excelDoc.Append("") End If excelDoc.Append("") For c As Int32 = 0 To gView.Rows(r).Cells.Count - 1 'Don't write out a column without a column header. If Not gView.Columns(c).HeaderText = String.Empty Then Dim XMLstring As String = gView.Rows(r).Cells(c).Text XMLstring = XMLstring.Trim() XMLstring = XMLstring.Replace("&", "&") XMLstring = XMLstring.Replace(">", ">") XMLstring = XMLstring.Replace("" + "") excelDoc.Append(XMLstring) excelDoc.Append("") End If Next excelDoc.Append("") Next excelDoc.Append("") excelDoc.Append(" ") excelDoc.Append(endExcelXML) Return excelDoc.ToString End Function Shared Sub PrepareGridViewForExport(ByVal gview As System.Web.UI.Control) ' Cleans up grid for exporting. Takes links and visual elements and turns them into text. Dim lb As New System.Web.UI.WebControls.LinkButton Dim l As New System.Web.UI.WebControls.Literal Dim name As String = String.Empty For i As Int32 = 0 To gview.Controls.Count - 1 If TypeOf gview.Controls(i) Is System.Web.UI.WebControls.LinkButton Then l.Text = CType(gview.Controls(i), System.Web.UI.WebControls.LinkButton).Text gview.Controls.Remove(gview.Controls(i)) gview.Controls.AddAt(i, l) ElseIf TypeOf gview.Controls(i) Is System.Web.UI.WebControls.DropDownList Then l.Text = CType(gview.Controls(i), System.Web.UI.WebControls.DropDownList).SelectedItem.Text gview.Controls.Remove(gview.Controls(i)) gview.Controls.AddAt(i, l) ElseIf TypeOf gview.Controls(i) Is System.Web.UI.WebControls.CheckBox Then l.Text = CType(gview.Controls(i), System.Web.UI.WebControls.CheckBox).Checked.ToString gview.Controls.Remove(gview.Controls(i)) gview.Controls.AddAt(i, l) End If If gview.Controls(i).HasControls() Then PrepareGridViewForExport(gview.Controls(i)) End If Next End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload