Try lbldc.Visible = True System.Windows.Forms.Application.DoEvents() Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim rowIndex, colIndex As Integer rowIndex = 1 colIndex = 0 xlBook = xlApp.Workbooks().Add xlSheet = xlBook.Worksheets("sheet1") '将所得到的表的列名,赋值给单元格 Dim Col As DataColumn Dim Row As DataRow For Each Col In dsfx.Tables(0).Columns colIndex = colIndex + 1 xlApp.Cells(1, colIndex) = Col.ColumnName Next '得到的表所有行,赋值给单元格 For Each Row In dsfx.Tables(0).Rows rowIndex = rowIndex + 1 colIndex = 0 For Each Col In dsfx.Tables(0).Columns colIndex = colIndex + 1 xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName) Next Next With xlSheet .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体" '设标题为黑体字 .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True '标题字体加粗 .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1 '设表格边框样式 End With With xlSheet.PageSetup .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc .CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:" .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:" .LeftFooter = "&""楷体_GB2312,常规""&10制表人:" .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:" .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页" End With lbldc.Visible = False xlApp.Visible = True Catch ex As Exception MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) lbldc.Visible = False End Try
Public Shared Sub DataGridToExcel(ByVal dgExport As DataGrid, ByVal response As HttpResponse, ByVal xlfilename As String, ByVal sContentType As String) 'clean up the response.object 'response.Clear() ' response.Charset = "" 'set the response mime type for excel 'Dim xlfilename As String = "DutyLog" 'response.AppendHeader("Content-Disposition", "attachment;filename=" & xlfilename & ".xls") response.AppendHeader("Content-Disposition", "attachment;filename=" & xlfilename) 'response.ContentType = "application/vnd.ms-excel" response.ContentType = sContentType 'create a string writer Dim stringWrite As New System.IO.StringWriter 'create an htmltextwriter which uses the stringwriter Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite) 'instantiate a datagrid Dim dg As New DataGrid ' just set the input datagrid = to the new dg grid dg = dgExport ' Make the header text bold dg.HeaderStyle.Font.Bold = True 'bind the modified datagrid dg.DataBind() 'tell the datagrid to render itself to our htmltextwriter dg.RenderControl(htmlWrite) 'output the html response.Write(stringWrite.ToString) response.End() End Sub
Imports System.IOPrivate Sub btnToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToExcel.Click Dim dtTable As DataTable = viewstate("table") Datagrid1.DataSource = dtTable Datagrid1.DataBind() Dim filename As String filename = Now.Year & Now.Month & Now.Day & "excel.xls" 'Response.Write(filename) '定義文檔類型 Response.Clear() Response.Buffer = True Response.Charset = "utf-8" Response.AppendHeader("Content-Disposition", "attachment;filename=" & filename) Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8") Response.ContentType = "application/ms-excel" Me.EnableViewState = False '定義輸入流 Dim oStringWriter As StringWriter = New StringWriter Dim oHtmlTextWriter As HtmlTextWriter = New HtmlTextWriter(oStringWriter) '將目標數據綁定到輸出流 Datagrid1.RenderControl(oHtmlTextWriter) Response.Write(oStringWriter.ToString()) Response.End() End Sub
lbldc.Visible = True
System.Windows.Forms.Application.DoEvents()
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In dsfx.Tables(0).Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next
'得到的表所有行,赋值给单元格
For Each Row In dsfx.Tables(0).Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In dsfx.Tables(0).Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)
Next
Next
With xlSheet
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体"
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1
'设表格边框样式
End With
With xlSheet.PageSetup
.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc
.CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"
.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With
lbldc.Visible = False
xlApp.Visible = True
Catch ex As Exception
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
lbldc.Visible = False
End Try
我看过孟子E章用C#写的,可是我改成VB以后出现问题了,我想谁能帮我一小段VB的。
DataGridToExcel(DataGrid1, Response, "filename.xls", "application/vnd.ms-excel")
Public Shared Sub DataGridToExcel(ByVal dgExport As DataGrid, ByVal response As HttpResponse, ByVal xlfilename As String, ByVal sContentType As String)
'clean up the response.object
'response.Clear()
' response.Charset = ""
'set the response mime type for excel
'Dim xlfilename As String = "DutyLog"
'response.AppendHeader("Content-Disposition", "attachment;filename=" & xlfilename & ".xls")
response.AppendHeader("Content-Disposition", "attachment;filename=" & xlfilename)
'response.ContentType = "application/vnd.ms-excel"
response.ContentType = sContentType
'create a string writer
Dim stringWrite As New System.IO.StringWriter 'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite) 'instantiate a datagrid
Dim dg As New DataGrid
' just set the input datagrid = to the new dg grid
dg = dgExport ' Make the header text bold
dg.HeaderStyle.Font.Bold = True 'bind the modified datagrid
dg.DataBind() 'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite) 'output the html
response.Write(stringWrite.ToString)
response.End()
End Sub
Dim dtTable As DataTable = viewstate("table")
Datagrid1.DataSource = dtTable
Datagrid1.DataBind() Dim filename As String
filename = Now.Year & Now.Month & Now.Day & "excel.xls"
'Response.Write(filename) '定義文檔類型
Response.Clear()
Response.Buffer = True
Response.Charset = "utf-8" Response.AppendHeader("Content-Disposition", "attachment;filename=" & filename)
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8") Response.ContentType = "application/ms-excel"
Me.EnableViewState = False '定義輸入流
Dim oStringWriter As StringWriter = New StringWriter
Dim oHtmlTextWriter As HtmlTextWriter = New HtmlTextWriter(oStringWriter) '將目標數據綁定到輸出流
Datagrid1.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()
End Sub
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 类型“DataGridLinkButton”的控件“DataGrid1__ctl3__ctl0”必须放在具有 runat=server 的窗体标记内。用以上的代码得出的错误结论怎么解决呀?好像说是DataGrid本身的问题,怎么解决?