【问题标题】:Is there a way to upgrade word documents to 2010有没有办法将word文档升级到2010
【发布时间】:2011-09-21 21:33:29
【问题描述】:

场景:我有大约 14000 个 word 文档需要从“Microsoft Word 97 - 2003 Document”转换为“Microsoft Word Document”。换句话说,升级到 2010 格式 (.docx)。

问题:有没有使用 API 或其他东西的简单方法?

注意:我只能找到一个将文档转换为 .docx 的 Microsoft 程序,但它们仍以兼容模式打开。如果可以将它们转换为新格式,那就太好了。与打开旧文档时获得的功能相同,它为您提供了转换它的选项。

编辑:刚刚找到http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.convert.aspx,正在研究如何使用它。

EDIT2:这是我当前用于转换文档的功能

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
    FolderBrowserDialog1.ShowDialog()
    Dim mainThread As Thread
    If Not String.IsNullOrEmpty(FolderBrowserDialog1.SelectedPath) Then
        lstFiles.Clear()

        DirSearch(FolderBrowserDialog1.SelectedPath)
        ThreadPool.SetMaxThreads(1, 1)
        lstFiles.RemoveAll(Function(y) y.Contains(".docx"))
        TextBox1.Text += "Conversion started at " & DateTime.Now().ToString & Environment.NewLine
        For Each x In lstFiles
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ConvertDoc), x)
        Next

    End If
End Sub
Private Sub ConvertDoc(ByVal path As String)
    Dim word As New Microsoft.Office.Interop.Word.Application
    Dim doc As Microsoft.Office.Interop.Word.Document
    word.Visible = False

    Try
        Debug.Print(path)
        doc = word.Documents.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        doc.Convert()

    Catch ex As Exception
        ''do nothing
    Finally
        doc.Close()
        word.Quit()
    End Try

End Sub`

它让我选择一个路径,然后在子文件夹中查找所有 doc 文件。该代码并不重要,所有要转换的文件都在 lstFiles 中。目前唯一的问题是即使只转换 10 个文档也需要很长时间。我应该为每个文档使用一个 word 应用程序而不是重复使用它吗?有什么建议吗?

它还会在大约 2 或 3 次转换后打开 word 并开始闪烁但一直在转换。

EDIT3:对上面的代码稍作调整,运行起来更干净。转换 8 个文件需要 1 分钟 10 秒。考虑到我有 14000,我需要转换此方法需要相当长的时间。

EDIT4:再次更改代码。现在使用线程池。好像跑的快了一些。仍然需要在更好的计算机上运行才能转换所有文档。或者按文件夹慢慢做。谁能想到任何其他方式来优化它?

【问题讨论】:

  • 我想知道使用线程,但是当我运行你的第一个版本的代码时,我发现它只用一个线程就用完了我的两个内核的 100%,所以我认为并行化不会像更快的计算机一样帮助解决问题。你用的是什么电脑?
  • Windows XP x86,Intel Pentium Dual CPU @ 2.00GHZ,3.25 GB RAM。工作电脑...
  • 除了我的是 x64 Windows 7 之外,我们是相当可比的。我想知道 x86 版本是否比 x64 慢得多,或者我们是否使用了不同版本的 office 库。我正在使用“Microsoft Office 12.0 对象库”版本 2.4.0.0 和“Microsoft Word 12.0 对象库”版本 8.4.0.0。另外,您要转换的 Word 文档的平均大小是多少?我认为我的样本集中最大的是 1 MB 左右。
  • 另一个想法 - Office 库实际上会在后台加载 winword.exe 的副本来完成它的工作。也许 Windows 7 在进程启动和/或进程间通信方面更好。你有任何可以运行它的 Windows 7 计算机吗?
  • @Sam Skuce,文档大小从 60kb 到 120kb。所以一点也不大。我不会有一台可以再运行几年的 Windows 7 机器(下一次刷新)。我使用线程是因为它限制了同时发生的转换数量。

标签: c# vb.net ms-word upgrade docx


【解决方案1】:

我在本地运行了您的代码,只进行了一些小的编辑以改进跟踪和计时,并且它“仅”花费了 13.73 秒来处理 12 个文件。这将在大约 4 小时内照顾您的 14,000 个。我在带有双核处理器的 Windows 7 x64 上运行 Visual Studio 2010。也许您可以使用速度更快的计算机?

这是我的完整代码,这只是一个带有单个按钮 Button1 和一个 FolderBrowserDialog FolderBrowserDialog1 的表单:

Imports System.IO

Public Class Form1

Dim lstFiles As List(Of String) = New List(Of String)

Private Sub DirSearch(path As String)


    Dim thingies = From file In Directory.GetFiles(path) Where file.EndsWith(".doc") Select file

    lstFiles.AddRange(thingies)

    For Each subdir As String In Directory.GetDirectories(path)
        DirSearch(subdir)
    Next
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    FolderBrowserDialog1.ShowDialog()

    If Not String.IsNullOrEmpty(FolderBrowserDialog1.SelectedPath) Then
        lstFiles.Clear()

        DirSearch(FolderBrowserDialog1.SelectedPath)
        Dim word As New Microsoft.Office.Interop.Word.Application
        Dim doc As Microsoft.Office.Interop.Word.Document
        lstFiles.RemoveAll(Function(y) y.Contains(".docx"))
        Dim startTime As DateTime = DateTime.Now
        Debug.Print("Timer started at " & DateTime.Now().ToString & Environment.NewLine)
        For Each x In lstFiles
            word.Visible = False
            Debug.Print(x + Environment.NewLine)
            doc = word.Documents.Open(x)
            doc.Convert()
            doc.Close()
        Next
        word.Quit()
        Dim endTime As DateTime = DateTime.Now
        Debug.Print("Took " & endTime.Subtract(startTime).TotalSeconds & " to process " & lstFiles.Count & " documents" & Environment.NewLine)
    End If

End Sub
End Class

【讨论】:

    【解决方案2】:

    使用 word 自动化并打开它并使用 wdFormatDocumentDefault 的 WdSaveFormat 枚举保存它,它应该是 docx

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat%28v=office.14%29.aspx

    或者试试你提到的转换方法。无论哪种方式都是 100% 可能的,而且应该相当容易。

    编辑:如果转换器 Daniel 发布了作品,那就容易多了,他值得所有的功劳:)

    【讨论】:

      【解决方案3】:

      您可以使用免费的 Office 文件转换器。

      这里解释设置:

      http://technet.microsoft.com/en-us/library/cc179019.aspx

      有一个文件列表设置。

      【讨论】:

      • 我尝试过使用该程序,但它只将文件转换为 .docx,但它仍以兼容模式打开,为我提供转换选项(如果有意义的话)
      • 那么你在office 2007中打开它们会遇到问题。尝试在2010中打开一个2007文档。你会得到同样的提示。
      • 我什至设置了 FullUpgradeOnOpen=1 但它仍然只是将文件重命名为 .docx。实际上并没有转换它。
      • 尝试将其重命名为 .zip。尝试打开它。
      • 根据 technet 文章:“当用户在 Word 2010 中打开转换后的 .docx 文件时,该文件以 Word 2007 兼容模式打开。OFC 不支持将 .doc 文件转换为Word 2010 .docx 格式。”所以该程序不会进行完全转换,尽管听起来它比简单的重命名稍微多一点。
      【解决方案4】:

      试试这个:

      using Microsoft.Office.Interop
      Microsoft.Office.Interop.Word.ApplicationClass word = new ApplicationClass();
      object nullvalue = Type.Missing;
      object filee = filename;
      object file2 = String.Format("{0}{1}", filepath, "convertedfile.doc");
      Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filee, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue);
              doc.SaveAs(ref file2, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多