WEB转WORD详细说明
一、运用方法
主要运用 namespace Microsoft.Office.Interop.Word中的 Applicatio和Document二个类。
1.1 Application 类中:
1.1.1 Documents.Open打开WORD文档
1.1.2 Documents.Close关闭WORD文档
1.1.3 Selection.Find.Execute替代标签
1.2 Document 类中:
1.2.1 SavaAs别存为
二、实现步骤
引用WORD:在COM中引用 Microsoft Office 11.0 Object Libray 和 Microsoft Word 11.0 Object Library。
/// <summary>
/// 打开模板
/// </summary>
/// <param name="strFileName"></param>
public void Open( string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
oDoc = oWordApplic.Documents.Open(ref fileName, ref missing,ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing,ref missing);
oDoc.Activate();
}
/// <summary>
/// 关闭
/// </summary>
public void Close()
{
object saveChanges = WdSaveOptions.wdPromptToSaveChanges;
object originalFormat = WdOriginalFormat.wdWordDocument;
object routeDocument = true;
oWordApplic.Documents.Close(ref saveChanges, ref originalFormat, ref routeDocument);
}
/// <summary>
/// 另存为
/// </summary>
/// <param name="strFileName">模板路经</param>
public void SaveAs(string strFileName )
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
oDoc.SaveAs(ref fileName, ref missing,ref missing, ref missing,ref missing,ref missing,ref missing,
ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
/// <summary>
/// 替代
/// </summary>
/// <param name="findStr">WORD变量</param>
/// <param name="replaceStr">字符串</param>
/// <returns></returns>
public bool ReplaceText(string findStr, string replaceStr)
{
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object missing = System.Reflection.Missing.Value;
oWordApplic.Selection.Find.ClearFormatting();
object findText = findStr;
oWordApplic.Selection.Find.Replacement.ClearFormatting();
oWordApplic.Selection.Find.Replacement.Text = replaceStr;
if (oWordApplic.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
{
return true;
}
else
{
return false;
}
}
三、例子
首先创建WORD模板如图:
后台代码:
SqlConnection con = TO_DAL.DataConnection.GetConnection();
SqlCommand cmd = new SqlCommand("select * from Exercises_plan_matter", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];
string ExercisesMatter = dt.Rows[0].ItemArray[1].ToString();
string ExercisesName = dt.Rows[1].ItemArray[2].ToString();
string BeginTime = dt.Rows[2].ItemArray[4].ToString();
string EndTime = dt.Rows[1].ItemArray[5].ToString();
string SaveURL = HttpContext.Current.Server.MapPath("moban/模板1.doc");
test.Open(SaveURL);
//test.SetAlignment("Center");
//test.GotoBookMark("Name");
//test.SetFont("Bold");
//test.InsertText(txtTitle.Text);
//test.SetFont("nothing");
//test.GotoBookMark("Body");
test.SetAlignment("Center");
test.SetFont("Bold");
//test.InsertText(txtBody.Text);
//test.SetFont("nothing");
string strtext = "<$ExercisesMatter$>";
string ReplaceStr = ExercisesMatter;
test.ReplaceText(strtext, ReplaceStr);
string strtext1 = "<$ExercisesName$>";
string ReplaceStr1 = ExercisesName;
test.ReplaceText(strtext1, ReplaceStr1);
string strtext2 = "<$BeginTime$>";
string ReplaceStr2 = BeginTime;
test.ReplaceText(strtext2, ReplaceStr2);
string strtext3 = "<$EndTime$>";
string ReplaceStr3 = EndTime;
test.ReplaceText(strtext3, ReplaceStr3);
Session["URL"]=@"E:/qew2e.doc";
test.SaveAs(Session["URL"].ToString());
// test.Quit();
test.Close();
DownloadFile(Session["URL"].ToString());
}
/**/
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filename">文件物理地址</param>
protected void DownloadFile(string filename)
{
// string saveFileName = "test.doc";
// int intStart = filename.LastIndexOf("//") + 1;
// saveFileName = filename.Substring(intStart, filename.Length - intStart);
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.Buffer = true;
this.EnableViewState = false;
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream"; //控制下载
Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode("下载文件.doc", Encoding.UTF8));
Response.WriteFile(filename);
Response.Flush();
Response.Close();
Response.End();
}