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>
/// Convert the binary data of the Word file (used to save the database)
/// </Summary>
/// <Param name = "wordpath"> Word file path </param>
/// <Returns> binary </returns>
Private byte [] wordconvertbyte (string wordpath)
{
Byte [] bytcontent = NULL;
System. Io. filestream FS = NULL;
System. Io. binaryreader BR = NULL;
Try
{
FS = new filestream (wordpath, system. Io. filemode. Open );
}
Catch
{
}
BR = new binaryreader (Stream) FS );
Bytcontent = Br. readbytes (int32) fs. Length );

Return bytcontent;
}

/// <Summary>
/// Directory of the project
/// </Summary>
/// <Returns> </returns>
Public String getpath ()
{
Return application. startuppath;
}

/// <Summary>
/// Format the current time:
/// 1: yymmddhhmmss; 2: yyyy-mm \ dd \
/// </Summary>
/// <Returns> </returns>
Public String formatnowtime (INT num)
{
If (num = 1)
{
Return datetime. Now. tostring ("yymmddhhmmss ");
}
Else if (num = 2)
{
Return datetime. Now. tostring ("yyyy-mm") + @ "\" + datetime. Now. Day;
}
Return "";
}

// Test Method
Private void button#click (Object sender, eventargs E)
{
String newword = byteconvertword (wordconvertbyte (@ "D: \ test file .doc"), "test successful ");
}

 

Other methods: Source csdn:

Http://topic.csdn.net/u/20100412/17/fbdeb7a1-412f-4533-93c1-1a2516c3738a.html? Seed = 38035402 & R = 64626024 # r_64626024

// Convert the file to binary and save it to the database
String strresult = strpath + @ "\ result.doc ";
System. Io. filestream FS = new system. Io. filestream (@ strresult, system. Io. filemode. Open );
FS. Position = 0;
Byte [] content = new byte [fs. Length];
FS. Read (content, 0, (INT) fs. Length );
FS. Close ();
Fluiddesigndoc. qhse1 = content; // The type of qhse1 saved in the database is OLE object
Ifluiddesigndocmanager manager = new fluiddesigndocmanager (); manager. updatefluiddesigndoc (fluiddesigndoc );----

 

// Convert binary data into a file at a specified location
Private void GetFile (byte [] content, string filepath)
{
String filename = filepath;
If (system. Io. file. exists (filename ))
{
System. Io. file. Delete (filename );
}
// Filestream Sw = new filestream (@ filename, filemode. Create );
// Streamwriter FS = new streamwriter (SW, encoding. utf8 );
// Fs. Write (entity. content );
System. Io. filestream FS = new system. Io. filestream (filename, system. Io. filemode. Create );
FS. Write (content, 0, content. Length );
FS. Flush ();
FS. Close ();
Filename = system. Io. Path. Combine (application. startuppath, filename );
}

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.