1

技术:

C#

ASP.NET 核心 2.0

Excel(主要是 OpenXml,但偶尔也有遗留文档)

当前使用 ExcelDataReader 读取文件。

设想:

我需要在服务器上存储一些文件并稍后访问它们以完成工作。用户通过 Web API 上传文件并以相同的方式发出请求工作的调用。

一些用户使用密码保护他们的 Excel 文件,并且要求这些文件在服务器上时保持密码保护。

问题:

我需要检测文档是否受密码保护,以便我可以使用适当的配置来访问它。

我已经用谷歌搜索了很多,但它主要是 Workbook.HasPassword (我认为这是 Excel.Interop 的一部分,我想避免添加)。我还发现此链接指向有关 Office 文档的类似问题,其中解决方案是通过文件流针对各种标题数据的长清单。

理想情况下,我想要一个不需要我打开文件的对核心友好的解决方案。我不经常处理文档,所以我不知道下一步该往哪里看。

有什么建议么?

4

3 回答 3

3

由于您已经在使用 ExcelDataReader,请在 try-catch 子句中打开文件并处理InvalidPasswordException

bool ExcelFileRequiresPassword(Stream stream) {
    try {
        using (ExcelReaderFactory.CreateReader(stream)) { }
    } catch (InvalidPasswordException) {
        return true;
    }
    return false;
}

这不是太糟糕的表现明智。在幕后,它只检查文件中的相关标题,并处理所有血腥的 BIFF、ZIP 和版本控制细节。如果没有密码,则不会读取任何数据。

于 2018-07-10T06:47:05.680 回答
1

如果不打开文件,您不会知道文件是否受密码保护。该信息在文件中(据我所知);它没有暴露在文件系统中。

您应该能够使用 OpenXml SDK 快速掌握它。创建一个不受保护的文件。复制它。打开副本并设置密码并保存。然后使用openXml“生产力工具”来比较这两个文件(“比较文件”,在工具栏上)。它应该快速归零在哪里看。

于 2018-07-09T21:50:53.880 回答
0

这是我添加的旧帖子,但最近我想确定电子表格是否在 C# 中受到密码保护而不使用外部库,我最终创建了以下代码。

可能有一些我没有找到的案例和文件格式,但我希望这会让你走上正轨。

注意:比较文本中的空格数很重要,因此在剪切和粘贴时要小心。

static bool IsExcelPasswordProtected(string strSource)
{
    bool blResult = false;

    if (File.Exists(strSource))
    {
        char[] chBuffer = new char[4096];   // The character strings usually occur within the first 2K in my testing, but just in case
        TextReader trReader = new StreamReader(strSource, Encoding.UTF8, true);

        // Read the buffer
        trReader.ReadBlock(chBuffer, 0, chBuffer.Length);
        trReader.Close();

        // Remove non-printable and unicode characters, we're only interested in ASCII character set
        for (int i = 0; i < chBuffer.Length; i++)
        {
            if ((chBuffer[i] < ' ') || (chBuffer[i] > '~')) chBuffer[i] = ' ';
        }

        string strBuffer = new string(chBuffer);

        // .xls format files, version 97 to 2003 contains this text when password protected
        if (strBuffer.Contains("M i c r o s o f t   E n h a n c e d   C r y p t o g r a p h i c   P r o v i d e r"))
        {
            blResult = true;
        }
        // .xlsx format files contain this text when password protected
        else if (strBuffer.Contains("E n c r y p t e d P a c k a g e"))
        {
            blResult = true;
        }
        // .xlsx format files contain this text when not password protected
        else if (strBuffer.Contains("[Content_Types]"))
        {
            blResult = false;
        }
        // .xlsx format files contain this text when not password protected
        else
        {
            blResult = false;
        }
    }
    else
    {
        // File not found...deal with as you wish
    }

    return (blResult);
}
于 2021-07-26T11:18:47.913 回答