Chuyển đổi Word (DOC, DOCX) sang Byte Array C# Java

Aspose.Words API cung cấp cách đơn giản nhất để chuyển đổi tài liệu Microsoft Word DOC hoặc DOCX thành một mảng byte trong C# và Java.

Việc chuyển đổi tài liệu Word sang mảng byte rất hữu ích khi lưu trữ tài liệu trong cơ sở dữ liệu và/hoặc sau đó truy xuất chúng từ cơ sở dữ liệu.

Aspose.Words API cung cấp chức năng thao tác với các tệp Microsoft Word mà không cần sử dụng Microsoft Word. Nếu bạn chưa cài đặt API Aspose.Words, vui lòng làm theo hướng dẫn trên trang cài đặt dành cho .NET và trang cài đặt để sử dụng với Java.

Chuyển đổi Tài liệu Word thành Mảng Byte bằng C#

Aspose.Words for .NET có thể được sử dụng để chuyển đổi một đối tượng Tài liệu để thu được một mảng byte đại diện cho Tài liệu trong bất kỳ ứng dụng .NET nào. Đoạn mã sau minh họa việc chuyển đổi tệp DOC thành một mảng byte.

// Load the document from disk.
Document doc = new Document("Sample.doc");

// Create a new memory stream.
MemoryStream outStream = new MemoryStream();
// Save the document to stream.
doc.Save(outStream, SaveFormat.Docx);

// Convert the document to byte form.
byte[] docBytes = outStream.ToArray();

// The bytes are now ready to be stored/transmitted.

// Now reverse the steps to load the bytes back into a document object.
MemoryStream inStream = new MemoryStream(docBytes);

// Load the stream into a new document object.
Document loadDoc = new Document(inStream);
// Save the document.
loadDoc.Save("loadDoc.docx",SaveFormat.Docx);

Chuyển đổi Tài liệu Word thành Mảng Byte bằng Java

Đoạn mã sau minh họa việc chuyển đổi tệp DOC thành một mảng byte bằng Aspose.Words for Java API.

// Load the document.
Document doc = new Document("Sample.doc");

// Create a new memory stream.
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// Save the document to stream.
doc.save(outStream, SaveFormat.DOCX);

// Convert the document to byte form.
byte[] docBytes = outStream.toByteArray();

// The bytes are now ready to be stored/transmitted.

// Now reverse the steps to load the bytes back into a document object.
ByteArrayInputStream inStream = new ByteArrayInputStream(docBytes);

// Load the stream into a new document object.
Document loadDoc = new Document(inStream);
// Save the document.
loadDoc.save("loadDoc.docx",SaveFormat.Docx);

Xem thêm