下载excel时显示进度条

时间:2014-11-22 05:37:12

标签: c# excel asp.net-3.5

我们有时间点击按钮点击excel,而在中间我们想要显示进度条

任何人都可以帮助我

我们在按钮点击和导出Excel代码

上有以下代码

protected void ibtnExport_Click(object sender,ImageClickEventArgs e)     {         尝试         {             DataSet 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();

}

1 个答案:

答案 0 :(得分:0)

要进度条,您必须包含以下代码: -

确保您拥有此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

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