如何把数据库以二进制存储的excel,word文档写入excel,word

来源:互联网 发布:怎么在ubuntu上安装qq 编辑:程序博客网 时间:2024/05/20 07:37
Excel.Application   excel   =   new   Excel.Application   (); 
excel.Application.Workbooks.Add   (   true   ); 
string   ConnString= "workstation   id=WEIXIONG;packet   size=4096;integrated   security=SSPI;data   source=WEIXIONG;persist   security   info=False;initial   catalog=web "; 
SqlConnection   cn=new   SqlConnection(ConnString); 
cn.Open(); 
string   s= "select   *   from   ImageStore "; 
SqlCommand   cmd=new   SqlCommand(s,cn); 
SqlDataReader   dr=cmd.ExecuteReader(); 
Excel.Application   xlApp=new   Excel.ApplicationClass(); 
if(xlApp==null) 

        MessageBox.Show( "Excel錯誤 "); 
        return; 

xlApp.Application.Workbooks.Add(true); 
while(dr.Read()) 

數據庫裡以二進制存儲了一個excel文檔``我用SqlDataReader讀取了這個文檔```現在要寫入excel裡面```要怎麼搞; 

xlApp.Visible=true;//打開excel 
xlApp=null;   


------解决方案--------------------------------------------------------
是不是以流的形式写入数据库的,那就应该以流的形式输出.数据库可不知道你是什么WORD,还是EXCEL的... 

string strSQL = "Select [filecontent] from T_autoupdateprogram where filename = ' " + filename + " ' "; 
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(strSQL,sqlcon); 
//reader data from table 
System.Data.SqlClient.SqlDataReader sdr = command.ExecuteReader(); 
//sdr gets data 
sdr.Read(); 
//write io to memory 
System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])sdr[ "filecontent "]); 
//record 
this.listBox1.Items.Add(filename+ " has been downloaded! "); 
//close reader 
sdr.Close(); 
//check file 
FileInfo file = new FileInfo(target); 
if (file.Exists) 
file.Delete(); 
//file stream 
FileStream fs = file.OpenWrite(); 
//create file 
ms.WriteTo(fs); 
//close file stream 
fs.Close(); 
//close memorystream 
ms.Close(); 
------解决方案--------------------------------------------------------
将数据读取后,保存成文件。格式嘛,你只要按照二进制格式存储就好。然后存储文件名用.doc或者.xls。然后直接打开。 
------解决方案--------------------------------------------------------
以流的方式写进去,用一个字段去表示它是什么格式的,读出来保存成一个文件再加上扩展名 
------解决方案--------------------------------------------------------
/// <summary> 
/// 将文件转换成二进制流数据 
/// </summary> 
/// <param name= "path "> 文件的完整路径(包括文件名) </param> 
/// <returns> </returns> 
public static byte[] GetByteOfFile(string path) 

FileInfo fi = new FileInfo(path); 
FileStream fs = fi.OpenRead(); 
byte[] bytes = new byte[fs.Length]; 
fs.Read(bytes, 0, Convert.ToInt32(fs.Length)); 
return bytes; 

/// <summary> 
/// 将二进制流数据保存为本地文件,如果文件已经存在将删除并重新创建 
/// </summary> 
/// <param name= "fileName "> 文件的完整路径 </param> 
/// <param name= "file "> 二进制流数据 </param> 
public static void SaveFile(string fileName, byte[] file) 

if (File.Exists(fileName)) 

File.Delete(fileName); 

FileStream fs = new FileStream(fileName, FileMode.CreateNew); 
BinaryWriter bw = new BinaryWriter(fs); 
bw.Write(file, 0, file.Length); 
bw.Close(); 
fs.Close(); 
}