一、先註冊一下DSOFramer.ocx
操作:將DSOFramer.ocx複製到C:\windows\system32目錄下,
開始->運行->regsvr32 DSOFramer.ocx , 系統會提示DSOFramer.ocx中的DllRegisterServer成功。
二、添加DSOFramer.ocx到你的項目中
操作:先說明一下,我用VS 2005 ,其他VS版本可能操作會有不同,操作應該也類似自己試試,問題應該不大。
在你要訪問DSOFramer.ocx的目錄上點選右鍵菜單中的“添加現有項”,找到DSOFramer.ocx,確定。
三、在網頁中載入DSOFramer
建立Office.aspx
添加如下代碼:
<object id="MyOffice" name = "MyOffice" style="LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%"
classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57" codebase="dsoframer.ocx#version=2,2,0,0">
</object>
[注]:VS 2005對文法的要求賊多,什麼ID的值要用""括起來啦,<object>不能大寫啦,……
沒什麼大礙,但是很煩人,簡直就是微軟版的唐僧,我給大家提供的代碼是修改過的,VS不會有哪些廢話了。
然後再body中加入onload事件的處理函數
<body onload="show_word();">
再在<head></head>中間加入函數體
<script language="javascript" type="text/javascript">
<!--
function show_word() {
var str=window.location.search;
var pos_start=str.indexOf("id")+3;
if (pos_start == 2)
return ;
var id = "http://localhost/Getdc.aspx?id=" + str.substring(pos_start);
document.all. MyOffice.Open( id,false, "Word.Document");
}
// -->
</script>
四、編製Getdc.aspx.cs檔案
建立Getdc.aspx檔案,VS會同時建立與之關聯的Getdc.aspx.cs檔案
先加入命名空間
using System.Data.SqlClient;
using System.Data.SqlTypes;
編輯Getdc.aspx.cs的Page_Load函數;
protected void Page_Load(object sender, EventArgs e)
{
int pid = Convert.ToInt32(Request["id"]);
SqlConnection myConnection = new SqlConnection("Data Source=\"localhost\";Initial Catalog=\"demo\";Persist Security Info=True;User ID=demo;Password=demo");
//資料庫的相關設定自己改吧,我就不廢話了
SqlCommand mycommand = myConnection.CreateCommand();
myConnection.Open();
mycommand.CommandText = "SELECT filedata " + " FROM Table_word WHERE (ID = " + pid.ToString() + ") ";
//其中filedata的資料庫類型是varbinary(MAX)
SqlDataReader myReader = mycommand.ExecuteReader();
myReader.Read();
SqlBinary binaryStream = myReader.GetSqlBinary(0);
myReader.Close();
myConnection.Close();
Response.BinaryWrite(binaryStream.Value);
}
至此,只要你指定的ID沒問題,就應該可以載入Word文檔了。
五、建立儲存文檔的按鈕
在Office.aspx中添加按鈕
<input id="Button1" type="submit" value="儲存" onclick="return Submit_upload_onclick()" />
再建立一個input
<input type="file" name="File" id = "File"/>
然後添加Submit_upload_onclick()函數:
function Submit_upload_onclick()
{
var str=window.location.search;
var pos_start=str.indexOf("id")+3;
if (pos_start == 2)
return ;
document.all.MyOffice.HttpInit();
document.all.MyOffice.HttpAddPostCurrFile("File", "");
var id = "http://localhost/Savedc.aspx?id=" + str.substring(pos_start);
document.all.MyOffice.HttpPost(id);
}
六、編製Savedc.aspx.cs檔案
建立Savedc.aspx檔案,VS會同時建立與之關聯的Savedc.aspx.cs檔案
先加入命名空間
using System.Data.SqlClient;
using System.Data.SqlTypes;
編輯Savedc.aspx.cs的Page_Load函數;
protected void Page_Load(object sender, EventArgs e)
{
int pid = Convert.ToInt32(Request["id"]);
Byte[] source_bin = Request.BinaryRead(Request.TotalBytes);
//---------------------------------------------------------------------------------------
int i, loop, again = -1, file_begin = -1;
for (i = 0; i < Request.TotalBytes; i++)
{
if (source_bin[i] == 13)
if (source_bin[i + 1] == 10)
break;
}
Byte[] MyHeader = new Byte[i];
for (loop = 0; loop < i; loop++)
MyHeader[loop] = source_bin[loop];
for (i += 2; i < Request.TotalBytes; i++)
{
if (source_bin[i] == 13)
if (source_bin[i + 1] == 10)
if (source_bin[i + 2] == 13)
if (source_bin[i + 3] == 10)
break;
}
file_begin = i + 4;
for (i = file_begin; i < Request.TotalBytes; i++)
{
for (loop = 0; loop < MyHeader.Length; loop++)
if (source_bin[i + loop] != MyHeader[loop])
break;
if (loop >= MyHeader.Length)
{
break;
}
}
Byte[] result = new Byte[i - file_begin];
//這是個不得已的辦法,用迴圈肯定會慢,但我不會其他方法
//希望高手完善
for (loop = file_begin; loop < i - file_begin; loop++)
result[loop - file_begin] = source_bin[loop];
//---------------------------------------------------------------------------------------
//以上代碼將word文檔從二進位流中提取出來,儲存在result[]中,方法雖笨,肯定好使
//本人不懂VB,更不懂Java,
//上面代碼是我楞從梁無懼用VB寫的無懼上傳類V2.0裡挑選有用的部分翻譯成C#的,淚ing……
//看看人家梁兄,多無私,給同行提供這麼好的東東
//我在網上找了N + 1天,就TM沒找到C#版的,再淚ing……
//現在提供給大家,希望我是最後一個為此鬱悶的人
SqlConnection myConnection = new SqlConnection("Data Source=\"localhost\";Initial Catalog=\"demo\";Persist Security Info=True;User ID=demo;Password=demo");//資料庫的相關設定自己改吧
SqlCommand mycommand = myConnection.CreateCommand();
mycommand.CommandText = "UPDATE Table_word SET filedata = @myfiledata , b_finished = 1 WHERE (ID = @myID)";
mycommand.Parameters.Add("@myfiledata", SqlDbType.VarBinary, 0, "filedata");
mycommand.Parameters.Add("@myID", SqlDbType.Int, 4, "ID");
SqlDataAdapter mydpt;
DataSet myds = new DataSet();
mydpt = new SqlDataAdapter("SELECT ID ,filedata , b_finished FROM Table_copy WHERE (ID = " + Request["id"] + ")", myConnection);
myConnection.Open();
mydpt.UpdateCommand = mycommand;
mydpt.Fill(myds);
DataTable mydt = myds.Tables[0];
DataRow myrow;
myrow = mydt.Rows[0];
myrow.BeginEdit();
myrow["filedata"] = result;
myrow.EndEdit();
mydpt.Update(myds);
myConnection.Close();/**/
}
至此,只要你指定的ID沒問題,就應該可以儲存Word文檔了。
本文轉載!