最近項目出使用到了將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>