将数据表导出到具有密码保护的Excel文件 [英] Export a data-table to an Excel file with password protected

查看:141
本文介绍了将数据表导出到具有密码保护的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想将数据表导出到具有密码保护的Excel文件中,我已经编写了一些代码以将数据表从数据表导出到Excel文件中,但是我不知道如何添加密码保护.请帮帮我.

这是我现有的代码:

Hi,
I want to Export a data-table to an Excel file with password protection, I have written some code to go from my Data table to an Excel file but I do not know how to add the password protection. Please help me.

Here is my existing code:

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();
}

推荐答案

这可以使用工作簿对象的password属性来完成.
请参见使用Visual Studio .NET在Excel工作簿上实现密码安全 [
This can be done using the password property of the workbook object.
See Implementing Password Security on Excel Workbooks Using Visual Studio .NET[^].


这篇关于将数据表导出到具有密码保护的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆