2012-01-11 24 views
30

Tôi đang cố gắng mở tài liệu Word, thay đổi một số văn bản và sau đó lưu thay đổi vào tài liệu mới. Tôi có thể lấy bit đầu tiên được thực hiện bằng cách sử dụng mã dưới đây nhưng tôi không thể tìm ra cách để lưu các thay đổi vào một tài liệu MỚI (chỉ định đường dẫn và tên tệp).Lưu sửa đổi WordprocessingDocument thành tệp mới

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using DocumentFormat.OpenXml.Packaging; 
using System.IO; 

namespace WordTest 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string template = @"c:\data\hello.docx"; 
     string documentText; 

     using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(template, true)) 
     { 
      using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream())) 
      { 
       documentText = reader.ReadToEnd(); 
      } 


      documentText = documentText.Replace("##Name##", "Paul"); 
      documentText = documentText.Replace("##Make##", "Samsung"); 

      using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) 
      { 
       writer.Write(documentText); 
      } 
     } 
     } 
    } 
} 

Tôi là người mới bắt đầu hoàn chỉnh, vì vậy hãy tha thứ cho câu hỏi cơ bản!

Trả lời

28

Nếu bạn sử dụng một MemoryStream bạn có thể lưu các thay đổi vào một tập tin mới như này:

byte[] byteArray = File.ReadAllBytes("c:\\data\\hello.docx"); 
using (MemoryStream stream = new MemoryStream()) 
{ 
    stream.Write(byteArray, 0, (int)byteArray.Length); 
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true)) 
    { 
     // Do work here 
    } 
    // Save the file with the new name 
    File.WriteAllBytes("C:\\data\\newFileName.docx", stream.ToArray()); 
} 
-1

Đối với tôi this đã làm việc tốt:

// To search and replace content in a document part. 
public static void SearchAndReplace(string document) 
{ 
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) 
    { 
     string docText = null; 
     using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) 
     { 
      docText = sr.ReadToEnd(); 
     } 

     Regex regexText = new Regex("Hello world!"); 
     docText = regexText.Replace(docText, "Hi Everyone!"); 

     using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) 
     { 
      sw.Write(docText); 
     } 
    } 
} 
+0

Điều đó lưu nó vào tài liệu CÙNG và do đó không trả lời câu hỏi. –

3

Chỉ cần sao chép tệp nguồn đến đích và thực hiện thay đổi từ đó.

File.copy(source,destination); 
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destination, true)) 
    { 
     \\Make changes to the document and save it. 
     WordDoc.MainDocumentPart.Document.Save(); 
     WordDoc.Close(); 
    } 

Hy vọng công trình này sẽ hoạt động.

+2

1. Có vẻ như bạn muốn viết 'wordDoc.Save()'. 2. Không có phương thức 'Save()'. Vì vậy, điều này không có vẻ giống như một giải pháp hợp lệ cho tôi. Tôi có thiếu gì không ?? –

+0

Lưu ý rằng mã này 'MainDocumentPart.Document.Save()' sẽ không lưu toàn bộ gói/tài liệu từ. Có những phần khác cần được lưu lại (vấn đề của tôi là các chú thích không được lưu, mà không nằm trong 'MainDocumentPart'). Tôi đã kết thúc bằng cách sử dụng câu trả lời được chấp nhận thay vì cố gắng để làm việc ra những gì tôi cần thiết để gọi tiết kiệm trên. –

+0

Đối với những người bạn đã lưu riêng lẻ. –

4

Trong Open XML SDK 2.5:

File.Copy(originalFilePath, modifiedFilePath); 

    using (var wordprocessingDocument = WordprocessingDocument.Open(modifiedFilePath, isEditable: true)) 
    { 
     // Do changes here... 
    } 

wordprocessingDocument.AutoSave là đúng theo mặc định để Đóng và Vứt bỏ sẽ lưu các thay đổi. wordprocessingDocument.Close không cần thiết một cách rõ ràng vì khối sử dụng sẽ gọi nó.

Cách tiếp cận này không yêu cầu toàn bộ nội dung tệp được tải vào bộ nhớ như trong câu trả lời được chấp nhận. Nó không phải là một vấn đề đối với các tệp nhỏ, nhưng trong trường hợp của tôi, tôi phải xử lý nhiều tệp docx hơn với nội dung xlsx và pdf được nhúng cùng một lúc để việc sử dụng bộ nhớ sẽ khá cao.

Các vấn đề liên quan