【问题标题】:Making exported excel sheet as readonly将导出的 Excel 工作表设为只读
【发布时间】:2011-06-15 11:24:33
【问题描述】:

在我的应用程序中,我正在将 gridview 数据导出到 excel 并将其存储到特定文件夹中,现在我想要将此 excel 文件设置为只读,这样任何人都不应对其进行编辑。

我写过这样的代码:

protected void Button5_Click(object sender, EventArgs e) {

        this.GridView1.Page.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        hw.WriteLine("<b><font size='5'> Report</font></b>");
        this.GridView1.RenderControl(hw);
        string HtmlInfo = tw.ToString();
        string DocFileName = "Report" + ".xls";
        string FilePathName = Request.PhysicalPath;
        FilePathName = FilePathName.Substring(0, FilePathName.LastIndexOf("\\"));
        FilePathName = @"C:\Excel" + "\\" + DocFileName;
        FileStream Fs = new FileStream(FilePathName, FileMode.Create);
        BinaryWriter BWriter = new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("UTF-8"));
        BWriter.Write(HtmlInfo);
        BWriter.Close();
        Fs.Close();

   }

any1 帮我解决这个问题...

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您想在文件写入后将其设为只读吗?

    var fileInfo = new FileInfo(FilePathName );
    fileInfo.IsReadOnly = true;
    

    编辑

    这是一个网站?我不知道有什么方法可以阻止某人下载然后修改您的文件。您可以使用 API 来代替。我们在此处使用Aspose.Cells 将表格设置为只读,然后再将其发送给客户端。

    【讨论】:

    • @Jeff-我已经在我的本地机器上创建了文件夹,我正在将这个 excel 表保存在那里,现在我想要的是它不应该是可编辑的,我该怎么做..
    • 使用 fileinfo 在我的机器上工作。你有什么例外吗?
    • @Jeff-no 我没有得到任何异常,但 excel 文件可以编辑我不希望发生这种情况,用户不应该能够编辑它..如果我删除文件流和二进制写入器它抛出这样的错误找不到文件'C:\ Excel \ Report.xls'。
    • 真的不知道该告诉你什么,在这里工作正常。我仔细检查了它,我看到文件上的只读标志发生了变化。
    • 这是完整的代码。在您写出文件并关闭流之后,该代码会将其翻转为只读。没有别的了。
    【解决方案2】:

    我想你想要类似的东西

     FileStream Fs = new FileStream(FilePathName, FileMode.Create, Fileaccess.Read);
    

    查看此链接了解更多信息 http://msdn.microsoft.com/en-us/library/4z36sx0f.aspx#Y494

    【讨论】:

    • @Dean-它在“/WebSite31”应用程序中抛出类似服务器错误的错误。结合 FileMode: Create with FileAccess: Read is invalid
    【解决方案3】:

    你不能一起工作,首先你必须创建文件流,然后定义它的访问模式

    看看这个链接

    http://msdn.microsoft.com/en-us/library/y973b725.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2016-08-04
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多