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

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-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 "";
        }


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






//将文件转换为二进制,并保存到数据库中
            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;//其中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);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值