0
A
回答
0
试试这个。它应该做的伎俩。 这会将所有内容从第一个文档复制到第二个文档。确保两个文件都存在。
using (WordprocessingDocument firstDocument = WordprocessingDocument.Open(@"E:\firstDocument.docx", false))
using (WordprocessingDocument secondDocument = WordprocessingDocument.Create(@"E:\secondDocument.docx", WordprocessingDocumentType.Document))
{
foreach (var part in firstDocument.Parts)
{
secondDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
}
}
0
下面的函数将告诉你如何打开 - 关闭和从word文档复制。
using MsWord = Microsoft.Office.Interop.Word;
private static void MsWordCopy()
{
var wordApp = new MsWord.Application();
MsWord.Document documentFrom = null, documentTo = null;
try
{
var fileNameFrom = @"C:\MyDocFile.docx";
wordApp.Visible = true;
documentFrom = wordApp.Documents.Open(fileNameFrom, Type.Missing, true);
MsWord.Range oRange = documentFrom.Content;
oRange.Copy();
var fileNameTo = @"C:\MyDocFile-Copy.docx";
documentTo = wordApp.Documents.Add();
documentTo.Content.PasteSpecial(DataType: MsWord.WdPasteOptions.wdKeepSourceFormatting);
documentTo.SaveAs(fileNameTo);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (documentFrom != null)
documentFrom.Close(false);
if (documentTo != null)
documentTo.Close();
if (wordApp != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;
documentFrom = null;
documentTo = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
0
请尝试下面的代码。这可能会帮助你。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var app = new Microsoft.Office.Interop.Word.Application();
var sourceDoc = app.Documents.Open(@"D:\test.docx");
sourceDoc.ActiveWindow.Selection.WholeStory();
sourceDoc.ActiveWindow.Selection.Copy();
var newDocument = new Microsoft.Office.Interop.Word.Document();
newDocument.ActiveWindow.Selection.Paste();
newDocument.SaveAs(@"D:\test1.docx");
sourceDoc.Close(false);
newDocument.Close();
app.Quit();
Marshal.ReleaseComObject(app);
Marshal.ReleaseComObject(sourceDoc);
Marshal.ReleaseComObject(newDocument);
}
}
}
相关问题
- 1. Python:将内容从一个单词文档复制到另一个单词文档并保持格式?
- 2. 将单词表复制到一个新的单词文档中
- 3. 将内容控制的内容导出到另一个单词文档
- 4. 将一个文档的内容复制到另一个
- 5. 如何从一个文件复制内容到另一个单词在C?
- 6. 将一个单词文档插入另一个文档时,VBA图像丢失
- 7. 将一个Xml文档内容复制到另一个C#中
- 8. 一次打开一个单词文档
- 9. 将div内容复制到另一个div并替换单词
- 10. 使用vba将word文档的内容复制到另一个word文档中
- 11. Groovy:将XML文档从一个文档复制到另一个文档
- 12. 将单词内容插入到VSTO文档级别定制中
- 13. 将内容从Word文档复制到另一个样式
- 14. 如何为单词文档中的单个词设置样式
- 15. 将单词表格单元格中的所有内容复制到单词文档的末尾
- 16. C#单词文档
- 17. Xpage - 将文档复制到另一个文档
- 18. 每个独特单元格的一个单词文档
- 19. 如何将xml的一部分复制到另一个文档
- 20. 复制Word文档的内容并粘贴到另一个
- 21. VB将宏从一个文档复制到另一个文档的宏
- 22. 将单词文档的内容导入到R中
- 23. 将一个Word文档的内容复制到另一个具有不同名称的文档中
- 24. 有没有一种简单的方法来复制单词文档。到另一个使用C#?
- 25. 如何自动将单词列表替换为单词文档?
- 26. PHP将xml节点从一个文档复制到另一个文档
- 27. 如何用CSS隐藏HTML文档中的一个单词
- 28. 如何用另一个单词替换文本正文中的每个单词?
- 29. 将单词文档解析为excel文件,每行单个单词
- 30. 如何在elasticsearch中将一个单词映射到另一个单词?
将文件复制到一个新文件? – kurakura88