当前位置:   article > 正文

asp.net中数据导出到电子表格excel_epanet 输出excel

epanet 输出excel

   
‘=====gird导出模式

Protected Sub Btn_daochu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_daochu.Click
        Dim filename As String = DateTime.Now.ToString("yyyyMMddhhmmss")
        filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) '//解决导出EXCEL时乱码的问题
        Response.ClearContent()
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
        Response.ContentType = "application/excel"
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls")


        Response.ContentType = "application/ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False
        Dim tw As New System.IO.StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        grid.RenderControl(hw)
        Response.Write(tw.ToString())
        Response.End()
    End Sub

 

 

’====datatable导出模式
 Public Shared Sub DataTable2Excel(ByVal dtData As System.Data.DataTable)
        Dim dgExport As System.Web.UI.WebControls.DataGrid = Nothing
        ' 当前对话
        Dim curContext As System.Web.HttpContext = System.Web.HttpContext.Current
        ' IO用于导出并返回excel文件
        Dim strWriter As System.IO.StringWriter = Nothing
        Dim htmlWriter As System.Web.UI.HtmlTextWriter = Nothing

        If Not dtData Is Nothing Then
            ' 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel"
            'curContext.Response.ContentEncoding = System.Text.Encoding.UTF8
            curContext.Response.Charset = ""
            Dim filename As String = DateTime.Now.ToString("yyyyMMddhhmmss")
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls")
            ' 导出excel文件
            strWriter = New System.IO.StringWriter()
            htmlWriter = New System.Web.UI.HtmlTextWriter(strWriter)

            ' 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
            dgExport = New System.Web.UI.WebControls.DataGrid()
            dgExport.DataSource = dtData.DefaultView
            dgExport.AllowPaging = False
            dgExport.DataBind()

            ' 返回客户端
            dgExport.RenderControl(htmlWriter)
            curContext.Response.Write(strWriter.ToString())
            curContext.Response.End()
        End If
    End Sub

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/216056
推荐阅读
相关标签
  

闽ICP备14008679号