返回首页

您好,
我想一个数据表导出到Excel文件密码保护,我已经写了一些代码去一个Excel文件,从我的数据表,但我不知道如何添加密码保护。请帮助我。

这里是我现有的代码:

Response.ClearContent();

Response.Buffer = true;

/ Response.Headers.Add("password", "asdasd");

Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ViewUserDetails.xls"));

Response.ContentType = "application/ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

int DealId = Convert.ToInt32(Request["DealID"]);

 

dtgManageUser.AllowPaging = false;

//int DealId = Convert.ToInt32(Request["DealID"]);

dtgManageUser.DataSource = LDUser.GetSearch(null, null, null, null, null, null, null, null, null, null, true, null, null, null, null, null).Tables[0];

dtgManageUser.DataBind();

if (dtgManageUser.Rows.Count > 0)

{

    //Change the Header Row back to white color

    dtgManageUser.HeaderRow.Style.Add("background-color", "#FFFFFF");

    //Applying stlye to gridview header cells

    for (int i = 0; i < dtgManageUser.HeaderRow.Cells.Count; i++)

    {

        dtgManageUser.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");

        if (i >= 4 || i == 0)

            dtgManageUser.Columns[i].Visible = false;

    }

    int j = 1;

    //This loop is used to apply stlye to cells based on particular row

    foreach (GridViewRow gvrow in dtgManageUser.Rows)

    {

        gvrow.BackColor = System.Drawing.Color.White;

        if (j <= dtgManageUser.Rows.Count)

        {

            if (j % 2 != 0)

            {

                for (int k = 0; k < gvrow.Cells.Count; k++)

                {

                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");

                }

            }

        }

        j++;

    }

    dtgManageUser.RenderControl(htw);

    Response.Write(sw.ToString());

 

    Response.End();

}

回答

评论会员: 时间:2