二进制数据如何转换为word文档 描述:先已将一个Wrod文档以二进制的形式保存到了 数据库 中请问如何可以实现将二进制直接转为一个word文档。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给点意见呗。 byte[] content = “二进制数据”; Response.ClearHeaders(); Response.ClearContent(); Response.Clear(); Response.ContentType = "Application/msword"; //Response.BinaryWrite(content); 下面该怎么写... HttpContext.Current.Response.BinaryWrite(content);HttpContext.Current.Response.Flush();HttpContext.Current.Response.End(); 以前我也遇到过这个问题,大致是这样的先将数据读取到drSqlDataReader dr = com.ExecuteReader(); 然后if (dr.Read()) { Page.Response.ContentType = dr["type"].ToString(); Page.Response.BinaryWrite(Convert.FromBase64String(dr["filedata"].ToString())); Page.Response.End(); } 数据以text类型存储 先谢谢两位,我现在想在Winform中实现,把从数据库中读取word的二进文件数据换为。请问还有没有其他的方法可用? 找到一个方法,不幸的是,打开是乱码: protected string ConvertWord(byte[] data) { string filepath = "d:/1.doc"; 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 ""; }引用地址:http://topic.csdn.net/u/20080415/11/048deb7e-b0c2-4a20-94a5-88be74ce47f0.html 搞定,这个方法可以。原来数据库中的二进制文件加密啦。解密后将二进制数据传入该测试方法即可。 protected void ConvertWord(byte[] data) { string filepath = "d:/a.doc"; FileStream fs = new FileStream(filepath, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(fs); bw.Write(data, 0, data.Length); bw.Close(); fs.Close(); } 接着问:--------如何跟word文件路径,将其转换为二进制保存到数据库。 ------------------------将文件转换为二进制,并保存到数据库中----------------------------------------------------- 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); } 谢谢,liuxing19870629 我也试出一种方法: 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; }通过上方法的测试可以protected void ConvertWord(byte[] data) { string filepath = "d:/a.doc"; FileStream fs = new FileStream(filepath, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(fs); bw.Write(data, 0, data.Length); bw.Close(); fs.Close(); } bw.Write(data, 0, data.Length); 中的0 指的不是"application/msword" ? 大家进来看看这个问题 请教一道有关系统资源的面试题 请问怎样将String 转换为 dword dataGridView两列自动相乘 .NET中操作EXCEL 请教各位大侠:关于VS.Net的问题 求助,运行时出错 C#实现监控软件“另存为”的操作 两个partial类折叠的问题 求购物车实现方法 Subversion如何解决两个文件冲突问题? winform子窗体无法移动
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType = "Application/msword";
//Response.BinaryWrite(content); 下面该怎么写...
HttpContext.Current.Response.BinaryWrite(content);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
SqlDataReader dr = com.ExecuteReader(); 然后if (dr.Read())
{
Page.Response.ContentType = dr["type"].ToString();
Page.Response.BinaryWrite(Convert.FromBase64String(dr["filedata"].ToString())); Page.Response.End();
} 数据以text类型存储
请问还有没有其他的方法可用?
{
string filepath = "d:/1.doc";
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 "";
}
引用地址:http://topic.csdn.net/u/20080415/11/048deb7e-b0c2-4a20-94a5-88be74ce47f0.html
原来数据库中的二进制文件加密啦。
解密后将二进制数据传入该测试方法即可。 protected void ConvertWord(byte[] data)
{
string filepath = "d:/a.doc";
FileStream fs = new FileStream(filepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(data, 0, data.Length);
bw.Close();
fs.Close(); }
--------
如何跟word文件路径,将其转换为二进制保存到数据库。
------------------------将文件转换为二进制,并保存到数据库中----------------------------------------------------- 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);
}
我也试出一种方法: 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;
}
通过上方法的测试可以protected void ConvertWord(byte[] data)
{
string filepath = "d:/a.doc";
FileStream fs = new FileStream(filepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(data, 0, data.Length);
bw.Close();
fs.Close(); }