C # mutual conversion between Word documents and binary data

Source: Internet
Author: User

Recently, the project used to save a Word document in binary format to a database, and read and retrieve the binary data again to convert it into a Word document. Finally, I have summarized the following methods:
 

Code /// <Summary>
/// Convert binary data to a word file
/// </Summary>
/// <Param name = "data"> binary data </param>
/// <Param name = "fileName"> word file name </param>
/// <Returns> relative path of word storage </returns>
Public string ByteConvertWord (byte [] data, string fileName)
{
String savePath = @ "SystemWord" + FormatNowTime (2) + @"";

If (! System. IO. Directory. Exists (GetPath () + savePath ))
{
Directory. CreateDirectory (GetPath () + savePath );
}
SavePath + = fileName + ". doc ";
String filePath = GetPath () + savePath;

FileStream fs;
If (System. IO. File. Exists (filePath ))
{
Fs = new FileStream (filePath, FileMode. Truncate );
}
Else
{
Fs = new FileStream (filePath, FileMode. CreateNew );
}
BinaryWriter br = new BinaryWriter (fs );
Br. Write (data, 0, data. Length );
Br. Close ();
Fs. Close ();
Return savePath;
}

/// <Summary>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.