Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 下载时如何保护Excel中的密码?_Asp.net_Asp.net Mvc - Fatal编程技术网

Asp.net 下载时如何保护Excel中的密码?

Asp.net 下载时如何保护Excel中的密码?,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,如何在下载excel文件时保护excel中的密码 this.Response.AddHeader(“内容处置”,“附件;文件名=具有历史记录的报告.xls”); this.Response.ContentType=“应用程序/vnd.ms excel”; byte[]buffer=System.Text.Encoding.UTF8.GetBytes(sb.ToString()) 最后我找到了代码: protected void Page_Load(object sender, Event

如何在下载excel文件时保护excel中的密码

this.Response.AddHeader(“内容处置”,“附件;文件名=具有历史记录的报告.xls”); this.Response.ContentType=“应用程序/vnd.ms excel”; byte[]buffer=System.Text.Encoding.UTF8.GetBytes(sb.ToString())

最后我找到了代码:

    protected void Page_Load(object sender, EventArgs e)
    {

        DataSetToExcel(GetDataSet(), false);
    }

    private static DataSet GetDataSet()
    {
        DataSet ds = new DataSet();
        System.Data.DataTable dt = new System.Data.DataTable("Table");

        dt.Columns.Add("Name", Type.GetType("System.String"));
        dt.Columns.Add("Address", Type.GetType("System.String"));
        dt.Columns.Add("Phone", Type.GetType("System.String"));

        DataRow dr = dt.NewRow();
        dr["Name"] = "Balaji Selvarajan";
        dr["Address"] = "Reddiyur";
        dr["Phone"] = "000-000-0000";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["Name"] = "Balaji S";
        dr["Address"] = "Kattumannar Koil";
        dr["Phone"] = "000-000-0000";
        dt.Rows.Add(dr);

        ds.Tables.Add(dt);
        return ds;
    }


    private static void DataSetToExcel(DataSet ds, Boolean generateIdentity)
    {
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlApp.Visible = false;
        Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        for (int k = 0; k < ds.Tables.Count; k++)
        {
            System.Data.DataTable dt = ds.Tables[k];
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Missing.Value,
                            Missing.Value, Missing.Value, Missing.Value);
            ws.Name = dt.TableName;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (i == 0)
                        ws.Cells[1, j + 1] = dt.Columns[j].ColumnName;

                    ws.Cells[i + 2, j + 1] = (j == 0 && generateIdentity) ?
                                             (i + 1).ToString() : dt.Rows[i][j].ToString();
                }
            }
            ws.Protect("1", true, true, true, true, true, true, true, true, true, true, true, true, true, true, true);
        }
        wb.Protect("my", true, true);
        wb.Password = "Test";            
        wb.Close();

        xlApp.Visible = true;
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
datasetoExcel(GetDataSet(),false);
}
私有静态数据集GetDataSet()
{
数据集ds=新数据集();
System.Data.DataTable dt=新的System.Data.DataTable(“表格”);
Add(“Name”,Type.GetType(“System.String”);
Add(“Address”,Type.GetType(“System.String”);
Add(“Phone”,Type.GetType(“System.String”);
DataRow dr=dt.NewRow();
dr[“Name”]=“Balaji Selvarajan”;
dr[“Address”]=“Reddiyur”;
dr[“电话”]=“000-000-0000”;
dt.Rows.Add(dr);
dr=dt.NewRow();
dr[“Name”]=“Balaji S”;
dr[“地址”]=“Kattumannar Koil”;
dr[“电话”]=“000-000-0000”;
dt.Rows.Add(dr);
ds.Tables.Add(dt);
返回ds;
}
私有静态void DataSetoExcel(数据集ds,布尔生成实体)
{
Microsoft.Office.Interop.Excel.Application xlApp=新的Microsoft.Office.Interop.Excel.Application();
xlApp.Visible=false;
Microsoft.Office.Interop.Excel.Workbook wb=xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
for(int k=0;k
在excel中保护密码是什么意思?如果试图打开excel工作表,则会询问密码以打开excel工作表