【问题标题】:Show progress bar while downloading excel下载excel时显示进度条
【发布时间】:2014-11-22 14:10:38
【问题描述】:

我们有一些时间来下载 excel 点击按钮,而在中间我们想显示进度条

谁能帮帮我

我们在按钮单击和导出 excel 代码时有以下代码

protected void ibtnExport_Click(object sender, ImageClickEventArgs e) { 尝试 { 数据集 ds = new DataSet();

        if (TabContainer1.ActiveTabIndex == 0)
        {
            ds = (DataSet)ViewState["DSCurrResponseSummary"];

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[2].Rows.Count > 0)
            {

                 string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,TOTAL,PENDING,RESPONDED CONTROL,COMPLIANT,NON COMPLIANT,NOT APPLICABLE,TOTAL RESPONDENTS,STATUS,TARGET CLOSURE DATE,REVIEWED";
                string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,TOTAL_RESPONSE,PENDING_RESPONSE,RESPONDED_CONTROL,COMPLIANT,NON_COMPLIANT,Non Applicable,Total Respondant,STATUS,RESPONSE_DUE_DATE,IS_REVIEWED";

                DataTable Exportdt = ds.Tables[2];
                ExportToExcelFunction("Current Assessment Response Summary", Exportdt, ColName, strBindCols);

            }
        }
        else if (TabContainer1.ActiveTabIndex == 1)
        {
            ds = (DataSet)ViewState["DSPastResponseSummary"];

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[3].Rows.Count > 0)
            {

                string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,TOTAL,PENDING,RESPONDED CONTROL,COMPLIANT,NON COMPLIANT,NOT APPLICABLE,TOTAL RESPONDENTS,STATUS,TARGET CLOSURE DATE,REVIEWED";
                string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,TOTAL_RESPONSE,PENDING_RESPONSE,RESPONDED_CONTROL,COMPLIANT,NON_COMPLIANT,Non Applicable,Total Respondant,STATUS,RESPONSE_DUE_DATE,IS_REVIEWED";

                DataTable Exportdt = ds.Tables[3];
                ExportToExcelFunction("Past Assessment Response Summary", Exportdt, ColName, strBindCols);

            }
        }
            //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{

            //    //string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,STATUS";
            //    //string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,STATUS";
            //    string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,TOTAL,PENDING,STATUS";
            //    string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,TOTAL_RESPONSE,PENDING_RESPONSE,STATUS";

            //    DataTable Exportdt = ds.Tables[0];
            //    ExportToExcelFunction("Assessment Response Summary", Exportdt, ColName, strBindCols);

            //}

        Response.Flush();
    }
    catch (Exception ex)
    {

    }
}

public void ExportToExcelFunction(string FlName, DataTable mydt, string DispColName, string BindCols)
{
    Excel.Application xlObj = new Excel.Application();
    object oMissing = System.Reflection.Missing.Value;
    xlObj.Visible = false;
    //vinod
    string filepath = Server.MapPath("Export");
    string strFlName = filepath + "\\Master.xlsx";

    Excel.Workbook xlWB = xlObj.Workbooks.Open(strFlName, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, true, 0, true);
    Excel.Worksheet xlSheet = (Excel.Worksheet)xlWB.ActiveSheet;

    int cols = mydt.Columns.Count;
    int rows = mydt.Rows.Count;

    //Added for export to excel
    try
    {
        //For Column
        string[] strCols = DispColName.Split(',');
        for (int i = 1; i <= strCols.Length; i++)
        {
            if (strCols[i - 1].Length > 0 && strCols[i - 1] != null)
                xlSheet.Cells[1, i] = Convert.ToString(strCols[i - 1]);
        }

        // for Row   
        string[] strColBind = BindCols.Split(',');
        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < strColBind.Length; c++)
            {
                if (mydt.Rows[r][strColBind[c]].ToString().ToUpper() == "RELEASED")
                    xlSheet.Cells[r + 2, c + 1] = "OPEN";
                else
                    xlSheet.Cells[r + 2, c + 1] = mydt.Rows[r][strColBind[c]];
            }
        }
    }
    catch (Exception ex)
    {
    }


    String newFlName = "\\" + DateTime.Now.Ticks.ToString() + "_" + FlName + ".xls";
    xlWB.SaveAs(filepath + newFlName, Excel.XlFileFormat.xlWorkbookNormal, "", "", false, false, Excel.XlSaveAsAccessMode.xlExclusive, true, false, "", true);

    xlWB.Close(true, oMissing, oMissing);
    xlObj.Quit();

    System.IO.FileInfo file = new System.IO.FileInfo(@"" + filepath + newFlName + "");
    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();
    Response.AppendHeader("Content-Disposition", "attachment; filename = " + FlName + ".xls");
    Response.AppendHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/download";
    Response.WriteFile(file.FullName);
    Response.Flush();
    Response.Close();
    Response.End();

}

【问题讨论】:

    标签: c# excel asp.net-3.5


    【解决方案1】:

    到进度条,你必须包含以下代码:-

    确保您有任何显示在此 div 上的图像

    <div class="loading" align="center">
        Loading. Please wait.<br />
        <br />
        <img src="loader.gif" alt="" />
    </div>
    

    模态背景 CSS 您需要将以下 CSS 放置在页面的 HEAD 部分作为模态背景

    <style type="text/css">
        .modal
        {
            position: fixed;
            top: 0;
            left: 0;
            background-color: black;
            z-index: 99;
            opacity: 0.8;
            filter: alpha(opacity=80);
            -moz-opacity: 0.8;
            min-height: 100%;
            width: 100%;
        }
        .loading
        {
            font-family: Arial;
            font-size: 10pt;
            border: 5px solid #67CFF5;
            width: 200px;
            height: 100px;
            display: none;
            position: fixed;
            background-color: White;
            z-index: 999;
        }
    </style>
    

    添加 Jquery 以显示进度条

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function ShowProgress() {
            setTimeout(function () {
                var modal = $('<div />');
                modal.addClass("modal");
                $('body').append(modal);
                var loading = $(".loading");
                loading.show();
                var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
                var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
                loading.css({ top: top, left: left });
            }, 200);
        }
        $('form').live("submit", function () {
            ShowProgress();
        });
    </script>
    

    在页面加载时添加以下代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string script = "$(document).ready(function () { $('[id*=btnSubmit]').click(); });";
            ClientScript.RegisterStartupScript(this.GetType(), "load", script, true);
        }
    }
    

    参考http://www.aspsnippets.com/Articles/Show-progress-bar-on-Button-Click-in-ASPNet.aspx

    在上面的示例代码中对您的控件名称进行一些更改。

    【讨论】:

    • 嗨 Piyush,它的作品,但文件下载后进度条没有结束。我们需要重新启动应用程序。你能帮我吗....
    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 2017-04-19
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多