有個GRIDVIEW控制元件查詢的資料匯出EXCEL表,現在有C#的代碼,想轉成VB.net請大神給轉下,C#代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
// <summary>
/// Excel資料匯出函式
/// </summary>
/// <param name="ctl">需要匯出資料的控制元件</param>
/// <param name="FileName">匯出的Excel檔案名</param>
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
gv.AllowPaging = false;//設定GridView控制元件不能分頁
gv.AllowSorting = false;//設定GridView控制元件不能排序
gv.DataBind();
ToExcel(gv, "JWTYBH.xls");
gv.AllowPaging = true;//恢復GridView控制元件分頁
gv.AllowSorting = true;//恢復GridView控制元件排序
gv.DataBind();//資料系結函式
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
}
uj5u.com熱心網友回復:
已經解決了Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub ToExcel(ByVal ctl As Control, ByVal FileName As String)
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
HttpContext.Current.Response.Charset = "GB2312"
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" & "" & FileName)
ctl.Page.EnableViewState = False
Dim tw As System.IO.StringWriter = New System.IO.StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(tw)
ctl.RenderControl(hw)
HttpContext.Current.Response.Write(tw.ToString())
HttpContext.Current.Response.[End]()
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
gv.AllowPaging = False
gv.AllowSorting = False
gv.DataBind()
ToExcel(gv, "JWTYBH.xls")
gv.AllowPaging = True
gv.AllowSorting = True
gv.DataBind()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
End Class
uj5u.com熱心網友回復:
http://converter.telerik.com/轉載請註明出處,本文鏈接:https://www.uj5u.com/net/128624.html
標籤:ASP.NET
