ExcelHelper.cs

(开头部分) 18KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.IO;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Excel操作类
/// </summary>
/// Microsoft Excel 11.0 Object Library
public class ExcelHelper
{
    #region 数据导出至Excel文件
    /// </summary> 
    /// 导出Excel文件,自动返回可下载的文件流 
    /// </summary> 
    public static void DataTable1Excel(System.Data.DataTable dtData)
    {
        GridView gvExport = null;
        HttpContext curContext = HttpContext.Current;
        StringWriter strWriter = null;
        HtmlTextWriter htmlWriter = null;
        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel"; 
            curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            curContext.Response.Charset = "utf-8";
            strWriter = new StringWriter();  
            htmlWriter = new HtmlTextWriter(strWriter);
            gvExport = new GridView();
            gvExport.DataSource = dtData.DefaultView;
            gvExport.AllowPaging = false;
            gvExport.DataBind();
            gvExport.RenderControl(htmlWriter);
            curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"/>" + strWriter.ToString());
            curContext.Response.End();
        }
    }

    /// <summary>
    /// 导出Excel文件,转换为可读模式
    /// </summary>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        DataGrid dgExport = null;
        HttpContext curContext = HttpContext.Current;
        StringWriter strWriter = null;
        HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";
            strWriter = new StringWriter();
            htmlWriter = new HtmlTextWriter(strWriter);
            dgExport = new DataGrid();
后面还有 495 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。