如何在 C# 中打开文件并更改其属性?

发布于 2024-07-08 08:31:03 字数 122 浏览 4 评论 0原文

我需要打开 Microsoft Word 2003 文件并更改其文件属性。 例如更改摘要选项卡中的主题。
替代文字

I need to open a Microsoft Word 2003 file and change its file properties. Such as changing the Subject in the Summary Tab.
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-07-15 08:31:03

Microsoft 提供了一个非常有用的小程序集,称为 DSOFile。 通过在项目中引用它,您可以修改 Office 文档属性。 它不一定会让您打开实际的 Office 文件的属性对话框,但您当然可以模拟它。

根据微软的说法:

Dsofile.dll 文件允许您编辑
Office 文档属性
没有安装Office

更多详细信息和下载链接可在 http://support.microsoft.com/kb 找到/224351

这是我很久以前使用过的一些(非常古老的)VB 代码的片段。 抱歉,我还没有转换为 C#,并且请注意它是类的一部分,因此存在对实例变量的引用。 尽管如此,它应该很容易理解并满足您自己的需求:

Private Sub ProcessOfficeDocument(ByVal fileName As String)
    Dim docDSO As New DSOFile.OleDocumentPropertiesClass
    Dim docTitle, docModified, docAuthor, docKeywords As String
    Try
        docDSO.Open(fileName, True)
        Dim docSummary As DSOFile.SummaryProperties = docDSO.SummaryProperties
        docTitle = docSummary.Title
        docAuthor = docSummary.Author
        docKeywords = docSummary.Keywords
        docModified = CStr(docSummary.DateLastSaved)

        If (Not String.IsNullOrEmpty(docTitle)) Then
            _Title = docTitle
        End If

        If (Not String.IsNullOrEmpty(docAuthor)) Then
            _Author = docAuthor
        End If

        If (Not String.IsNullOrEmpty(docModified)) Then
            _DateModified = DateTime.Parse(docModified)
        End If

    Catch ex As Exception
        'Do whatever you need to do here...'
    Finally
        If (Not docDSO Is Nothing) Then
            docDSO.Close()
        End If
    End Try
End Sub

Microsoft provides a very useful little assembly called DSOFile. With a reference to it in your project, you can modify Office document properties. It won't necessarily let you open the actual Office file's properties dialog, but you could certainly simulate it.

According to Microsoft:

The Dsofile.dll files lets you edit
Office document properties when you do
not have Office installed

More details and a download link can be found at http://support.microsoft.com/kb/224351

Here's a snippet some (very old) VB code I used ages ago. Sorry I haven't converted to C# and be aware that it's part of a class so there are references to instance variables. Still, it should be pretty easy to understand and covert to your own needs:

Private Sub ProcessOfficeDocument(ByVal fileName As String)
    Dim docDSO As New DSOFile.OleDocumentPropertiesClass
    Dim docTitle, docModified, docAuthor, docKeywords As String
    Try
        docDSO.Open(fileName, True)
        Dim docSummary As DSOFile.SummaryProperties = docDSO.SummaryProperties
        docTitle = docSummary.Title
        docAuthor = docSummary.Author
        docKeywords = docSummary.Keywords
        docModified = CStr(docSummary.DateLastSaved)

        If (Not String.IsNullOrEmpty(docTitle)) Then
            _Title = docTitle
        End If

        If (Not String.IsNullOrEmpty(docAuthor)) Then
            _Author = docAuthor
        End If

        If (Not String.IsNullOrEmpty(docModified)) Then
            _DateModified = DateTime.Parse(docModified)
        End If

    Catch ex As Exception
        'Do whatever you need to do here...'
    Finally
        If (Not docDSO Is Nothing) Then
            docDSO.Close()
        End If
    End Try
End Sub
久夏青 2024-07-15 08:31:03

我可以想到两种方法来做到这一点:

如果可以的话,我会选择第二个选项,因为这样您就不必依赖系统上安装的 Word。

I can think of 2 ways to do this:

I would go with the second option if you can, because that way you don't have to depend on Word being installed on the system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文