急!! datagrid导出到excel

liguiman liguiman 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
??
有两种导出方法
Dim xlsheet As New SpreadsheetClass()
Dim i As Integer = 0
Dim j As Integer = 0
'Response.End()
' 输出标题
Dim oItem As DataGridColumn
For Each oItem In MyDataGrid.Columns
xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText
'xlsheet.ActiveSheet.Range(xlsheet.ActiveSheet.Cells(1, 1),_
'xlsheet.ActiveSheet.Cells(1, i + 1)).Font.Bold = True
'设置格式
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = True
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "red"
i = i + 1
Next

Dim numbercols As Integer = MyDataGrid.Items.Item(0).Cells.Count
' 输出字段内容
For j = 0 To MyDataGrid.Items.Count - 1
For i = 0 To numbercols - 1
xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue"
'xlsheet.Range("A2:B14").WrapText = True
xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns()
xlsheet.ActiveSheet.Cells(j + 2, i + 1) = MyDataGrid.Items.Item(j).Cells(i).Text.Replace(" ", " ")
xlsheet.Columns.AutoFitColumns()
xlsheet.Rows.AutoFitColumns()

Next
Next

和
Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""

' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
DataGrid1.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()


他们的区别是什么呢?
我的Datagrid中使用自定义列,使用第二种方法时 出错
怎么改阿??


...全文
评论 16
收藏 已收藏
分享
返回顶部
liguiman
.NET社区 62,272 成员 668,968 内容