Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法将word文档升级到2010_C#_Vb.net_Ms Word_Upgrade_Docx - Fatal编程技术网

C# 有没有办法将word文档升级到2010

C# 有没有办法将word文档升级到2010,c#,vb.net,ms-word,upgrade,docx,C#,Vb.net,Ms Word,Upgrade,Docx,场景:我有大约14000个word文档需要从“Microsoft word 97-2003文档”转换为“Microsoft word文档”。换句话说,升级到2010格式(.docx) 问题:有没有一种简单的方法可以使用API或其他什么来实现这一点 注意:我只能找到一个将文档转换为.docx的microsoft程序,但它们仍然以兼容模式打开。如果能把它们转换成新的格式就好了。打开旧文档时获得的功能与打开旧文档时获得的功能相同,它为您提供了转换旧文档的选项 编辑:刚刚发现如何使用它 EDIT2:这是

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

问题:有没有一种简单的方法可以使用API或其他什么来实现这一点

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

编辑:刚刚发现如何使用它

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

此外,它会在大约2或3次转换后打开word,并开始闪烁,但会继续转换

EDIT3:将代码调整为略高于一点,运行起来更干净。但转换8个文件需要1分钟10秒。考虑到我有14000,我需要转换此方法将需要相当长的时间


EDIT4:再次更改代码。现在使用线程池。似乎跑得快一点。仍然需要在更好的计算机上运行以转换所有文档。或者按文件夹慢慢地做。有人能想出其他方法来优化此功能吗?

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

此处解释了设置:


有一个文件列表设置。

使用word automation打开它,并使用wdFormatDocumentDefault的WdSaveFormat枚举保存它,该枚举应为docx

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

编辑:如果丹尼尔发布的转换器工作正常,那就容易多了,他应该得到所有的赞扬:)

试试这个:

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);

我在本地运行了您的代码,只做了一些小的编辑以改进跟踪和计时,而“只”花了13.73秒就完成了12个文件。这将在大约4小时内解决你的14000个问题。我在Windows7x64上运行VisualStudio2010,并使用双核处理器。也许你可以用一台更快的电脑

这是我的完整代码,这只是一个表单,有一个按钮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

我尝试过使用该程序,但它只将文件转换为.docx,但它仍然以兼容模式打开,并提供转换选项(如果有意义的话),那么您在office 2007中打开它们时会遇到问题。试着在2010年打开一个2007年的文档。您将得到相同的提示。我甚至将FullUpgradeOnOpen设置为1,但它仍然只将文件重命名为.docx。实际上没有转换。请尝试将其重命名为.zip。试着打开它。根据technet的文章:“当用户在Word 2010中打开转换后的.docx文件时,该文件是在Word 2007兼容模式下打开的。OFC不支持将.doc文件转换为Word 2010.docx格式。”因此该程序不会进行完全转换,虽然听起来它不仅仅是一个简单的重命名。我想知道如何使用线程,但当我运行你的第一个版本的代码时,我发现它只用一个线程就消耗了我的两个内核的100%,所以我认为并行化不会像更快的计算机那样有助于解决这个问题。您使用的是什么类型的计算机?Windows XP x86,英特尔奔腾双CPU@2.00GHZ,3.25 GB内存。工作电脑…除了我的x64 Windows 7之外,我们是相当类似的。我想知道x86版本是否比x64慢那么多,或者我们使用的是不同版本的office库。我正在使用“Microsoft Office 12.0对象库”2.4.0.0版和“Microsoft Word 12.0对象库”8.4.0.0版。另外,您要转换的Word文档的平均大小是多少?我认为我的示例集中最大的是1MB左右。另一个想法是,Office库实际上会在后台加载winword.exe的副本来完成它的工作。也许Windows7在进程启动和/或进程间通信方面更好。你有可以运行Windows 7的计算机吗?@Sam Skuce,文档大小从60kb到120kb不等。所以一点也不大。我再过几年(下一次刷新)也不会有Windows7机器了。我使用线程是因为它限制了并发发生的转换数量。