我有多个gridviews要导出到excel。每个工作表1个gridview。
怎么做? (或者一般如何将html字符串写入工作表)
这将在asp.net,服务器端完成。
我可以很好地通过epplus.codeplex.com创建多个工作表,但它可以在单元级别上工作。我可以导出DataTable,但是如何导出Gridview的HTML?
答案 0 :(得分:1)
我没有找到“clist”定义的位置和方式
For Each gView As GridView In cList
CreateWorkSheet(gView.ID.ToString, sw, gView, CellWidth)
Next
答案 1 :(得分:0)
我不确定这是否适用于GridView,但值得一试:
http://www.codeproject.com/KB/office/ExcelDataTable.aspx
编辑:它仍然有效,使用以下代码进行测试:
Dim data As New DataTable("Sample-Data")
data.Columns.Add("ID", GetType(Int32)).AutoIncrement = True
data.Columns.Add("Text", GetType(String))
For i As Int32 = 1 To 100
Dim newRow As DataRow = data.NewRow
newRow("Text") = i & ". Row"
data.Rows.Add(newRow)
Next
Dim grid As New System.Web.UI.WebControls.GridView
grid.HeaderStyle.Font.Bold = True
grid.DataSource = data
grid.DataMember = data.TableName
grid.DataBind()
' render the GridView control to a file '
Using sw As New IO.StreamWriter("c:\\Temp\test.xls")
Using hw As New HtmlTextWriter(sw)
grid.RenderControl(hw)
End Using
End Using
诀窍在于excel可以显示HTML表格,而GridView则呈现为表格。
但是,如果您要生成多个工作表,则无效。
答案 2 :(得分:0)
Excel完美适用于XML。
将一些工作簿从Excel保存为XML表,并在记事本中打开它以查看它是如何工作的。
答案 3 :(得分:0)
我刚刚完成了同样的事情。每个数据库talbe / gridview
的一个excel工作表你会在this blog post找到所有内容。如果您需要更多帮助,请告诉我。
Public Shared Sub CreateWorkBook(ByVal cList As Object, ByVal wbName As String, ByVal CellWidth As Integer)
Dim attachment As String = "attachment; filename=""" & wbName & ".xml"""
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("content-disposition", attachment)
HttpContext.Current.Response.ContentType = "application/ms-excel"
Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
sw.WriteLine("<?xml version=""1.0""?>")
sw.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
sw.WriteLine("<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""")
sw.WriteLine("xmlns:o=""urn:schemas-microsoft-com:office:office""")
sw.WriteLine("xmlns:x=""urn:schemas-microsoft-com:office:excel""")
sw.WriteLine("xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""")
sw.WriteLine("xmlns:html=""http://www.w3.org/TR/REC-html40"">")
sw.WriteLine("<DocumentProperties xmlns=""urn:schemas-microsoft-com:office:office"">")
sw.WriteLine("<LastAuthor>Try Not Catch</LastAuthor>")
sw.WriteLine("<Created>2010-05-15T19:14:19Z</Created>")
sw.WriteLine("<Version>11.9999</Version>")
sw.WriteLine("</DocumentProperties>")
sw.WriteLine("<ExcelWorkbook xmlns=""urn:schemas-microsoft-com:office:excel"">")
sw.WriteLine("<WindowHeight>9210</WindowHeight>")
sw.WriteLine("<WindowWidth>19035</WindowWidth>")
sw.WriteLine("<WindowTopX>0</WindowTopX>")
sw.WriteLine("<WindowTopY>90</WindowTopY>")
sw.WriteLine("<ProtectStructure>False</ProtectStructure>")
sw.WriteLine("<ProtectWindows>False</ProtectWindows>")
sw.WriteLine("</ExcelWorkbook>")
sw.WriteLine("<Styles>")
sw.WriteLine("<Style ss:ID=""Default"" ss:Name=""Normal"">")
sw.WriteLine("<Alignment ss:Vertical=""Bottom""/>")
sw.WriteLine("<Borders/>")
sw.WriteLine("<Font/>")
sw.WriteLine("<Interior/>")
sw.WriteLine("<NumberFormat/>")
sw.WriteLine("<Protection/>")
sw.WriteLine("</Style>")
sw.WriteLine("<Style ss:ID=""s22"">")
sw.WriteLine("<Alignment ss:Horizontal=""Center"" ss:Vertical=""Center"" ss:WrapText=""1""/>")
sw.WriteLine("<Borders>")
sw.WriteLine("<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("</Borders>")
sw.WriteLine("<Font ss:Bold=""1""/>")
sw.WriteLine("</Style>")
sw.WriteLine("<Style ss:ID=""s23"">")
sw.WriteLine("<Alignment ss:Vertical=""Bottom"" ss:WrapText=""1""/>")
sw.WriteLine("<Borders>")
sw.WriteLine("<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("</Borders>")
sw.WriteLine("</Style>")
sw.WriteLine("<Style ss:ID=""s24"">")
sw.WriteLine("<Alignment ss:Vertical=""Bottom"" ss:WrapText=""1""/>")
sw.WriteLine("<Borders>")
sw.WriteLine("<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
sw.WriteLine("ss:Color=""#000000""/>")
sw.WriteLine("</Borders>")
sw.WriteLine("<Font ss:Color=""#FFFFFF""/>")
sw.WriteLine("<Interior ss:Color=""#FF6A6A"" ss:Pattern=""Solid""/>") 'set header colour here
sw.WriteLine("</Style>")
sw.WriteLine("</Styles>")
For Each gView As GridView In cList
CreateWorkSheet(gView.ID.ToString, sw, gView, CellWidth)
Next
sw.WriteLine("</Workbook>")
HttpContext.Current.Response.Write(sw.ToString())
HttpContext.Current.Response.End()
End Sub
Private Shared Sub CreateWorkSheet(ByVal wsName As String, ByVal sw As System.IO.StringWriter, ByVal gv As GridView, ByVal cellwidth As Integer)
If IsNothing(gv.HeaderRow) = False Then
sw.WriteLine("<Worksheet ss:Name=""" & wsName & """>")
Dim cCount As Integer = gv.HeaderRow.Cells.Count
Dim rCount As Long = gv.Rows.Count + 1
sw.WriteLine("<Table ss:ExpandedColumnCount=""" & cCount & """ ss:ExpandedRowCount=""" & rCount & """ x:FullColumns=""1""")
sw.WriteLine("x:FullRows=""1"">")
For i As Integer = (cCount - cCount) To (cCount - 1)
sw.WriteLine("<Column ss:AutoFitWidth=""1"" ss:Width=""" & cellwidth & """/>")
Next
GridRowIterate(gv, sw)
sw.WriteLine("</Table>")
sw.WriteLine("<WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel"">")
sw.WriteLine("<Selected/>")
sw.WriteLine("<DoNotDisplayGridlines/>")
sw.WriteLine("<ProtectObjects>False</ProtectObjects>")
sw.WriteLine("<ProtectScenarios>False</ProtectScenarios>")
sw.WriteLine("</WorksheetOptions>")
sw.WriteLine("</Worksheet>")
End If
End Sub
Private Shared Sub GridRowIterate(ByVal gv As GridView, ByVal sw As System.IO.StringWriter)
sw.WriteLine("<Row>")
For Each tc As TableCell In gv.HeaderRow.Cells
Dim tcText As String = tc.Text
Dim tcWidth As String = gv.Width.Value
Dim dType As String = "String"
If IsNumeric(tcText) = True Then
dType = "Number"
End If
sw.WriteLine("<Cell ss:StyleID=""s24""><Data ss:Type=""String"">" & tcText & "</Data></Cell>")
Next
sw.WriteLine("</Row>")
For Each gr As GridViewRow In gv.Rows
sw.WriteLine("<Row>")
For Each gc As TableCell In gr.Cells
Dim gcText As String = gc.Text
Dim dType As String = "String"
If IsNumeric(gcText) = True Then
dType = "Number"
gcText = CDbl(gcText)
End If
sw.WriteLine("<Cell ss:StyleID=""s23""><Data ss:Type=""" & dType & """>" & gcText & "</Data></Cell>")
Next
sw.WriteLine("</Row>")
Next
End Sub