【问题标题】:How do I open a file in C# and change its properties?如何在 C# 中打开文件并更改其属性?
【发布时间】:2019-02-05 20:19:29
【问题描述】:

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

【问题讨论】:

    标签: c# ms-word


    【解决方案1】:

    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
    

    【讨论】:

    • 如果可以的话,我会 +5 这个。很好的答案。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多