c# word文档与二进制数据的相互转换

最近项目出使用到了将word文档以二进制的方法存到数据库中,并再次读取出二进制数据转换为word文档。最后总结了一下,不多说看示例方法:
 

代码
        /// <summary>
        
/// 二进制数据转换为word文件
        
/// </summary>
        
/// <param name="data">二进制数据</param>
        
/// <param name="fileName">word文件名</param>
        
/// <returns>word保存的相对路径</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>
        
/// word文件转换二进制数据(用于保存数据库)
        
/// </summary>
        
/// <param name="wordPath">word文件路径</param>
        
/// <returns>二进制</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>
        
/// 项目所在目录
        
/// </summary>
        
/// <returns></returns>
        public string GetPath()
        {
            
return Application.StartupPath;
        }

        
/// <summary>
        
/// 格式化当前时间: 
        
/// 1:yyMMddHHmmss; 2:yyyy-MMdd
        
/// </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 "";
        }

//测试方法
 private void button1_Click(object sender, EventArgs e)
        {
            
string newWord = ByteConvertWord(wordConvertByte(@"D:测试文件.doc"), "测试成功");
        }

其他方法:来源CSDN:

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


 

//将文件转换为二进制,并保存到数据库中
           
string strResult = strPath + @" esult.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;//其中qhse1在数据库中保存的类型为OLE对象
            IFluidDesignDocManager manager = new FluidDesignDocManager(); manager.UpdateFluidDesignDoc(fluidDesignDoc); ----

//将二进制数据转化为指定位置文件
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);
        }

原文地址:https://www.cnblogs.com/wwwzzg168/p/3570117.html