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>补充:软件开发 , C# ,
/// 二进制数据转换为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>补充:软件开发 , C# ,
上一个:C#利用反射动态调用类成员
下一个:c#控件攻略宝典之ListBox控件
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,
部分文章来自网络,