我想把页面上控件中的数据保存到word模板中。并且把word模板保存到客户端。
例如:
页面上:姓名 textboxword模板: 姓名 (空白)我想把网页上的textbox内容保存到word模板姓名后的空白的地方。
并且在客户端保存这样一个word文档。求实现思路、方法,有例子最好。
例如:
页面上:姓名 textboxword模板: 姓名 (空白)我想把网页上的textbox内容保存到word模板姓名后的空白的地方。
并且在客户端保存这样一个word文档。求实现思路、方法,有例子最好。
2,在word模版上修改安全添加everyone 可读,以防文件无法打开.
3,在工程里添加"引用"找到"Microsoft Word 10.0 Object Library"或"Microsoft Word 11.0 Object Library"点确定.
4,新建类叫WordOp.cs 意思是操作word的类.
内容如下://///////////////////////////////////////////////////////////////////////////
using System;
using System.Web.Security;
using Microsoft.Office.Interop.Word;
using System.IO;
/// <summary>
/// Word 的摘要说明
/// </summary>
public class WordOp
{
public WordOp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private ApplicationClass WordApp ;
private Document WordDoc;
private static bool isOpened=false;//判断word模版是否被占用
public void SaveAs(string strFileName,bool isReplace)
{
if (isReplace && File.Exists(strFileName))
{
File.Delete(strFileName);
}
object missing = Type.Missing;
object fileName = strFileName;
WordDoc.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);
}
//定义一个Word.Application 对象
public void activeWordApp()
{
WordApp = new ApplicationClass();
} public void Quit()
{
object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit(ref missing, ref missing, ref missing);
isOpened = false;
}
//基于模版新建Word文件
public void OpenTempelte(string strTemppath)
{
object Missing = Type.Missing;
//object Missing = System.Reflection.Missing.Value; activeWordApp();
WordApp.Visible = false; object oTemplate = (object)strTemppath;
try
{
while (isOpened)
{
System.Threading.Thread.Sleep(500);
}
WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
isOpened = true;
WordDoc.Activate();
}
catch (Exception Ex)
{
Quit();
isOpened = false;
throw new Exception(Ex.Message);
}
}
public void FillLable(string LabelId,string Content)
{ //打开Word模版
// OpenTempelte(tempName); //对LabelId的标签进行填充内容Content,即函件题目项
object bkmC = LabelId;
if (WordApp.ActiveDocument.Books.Exists(LabelId) == true)
{
WordApp.ActiveDocument.Books.get_Item(ref bkmC).Select(); }
WordApp.Selection.TypeText(Content);
//SaveAs(saveAsFileName);
//Quit(); }
}
/////////////////////////////////////////////////////////////////////////////5,在需要使用的页面后台调用既可,举例如下: string path = Server.MapPath("download");
string templatePath = path + "file://downloadczql.doc/";
WordOp wop = new WordOp();
wop.OpenTempelte(templatePath);
wop.FillLable("id", "1");
wop.FillLable("usr_name", "测试");
wop.SaveAs(path + "file://savetest.doc",true/);
wop.Quit();
Response.redirect(@"/download/savetest.doc");//做个跳转用于下载.
Private Sub changstr(ByVal wrdApp As Microsoft.Office.Interop.Word.Application, ByVal findstr As String, ByVal changstr As String)
wrdApp.Selection.Find.ClearFormatting()
wrdApp.Selection.Find.Replacement.ClearFormatting()
With wrdApp.Selection.Find
.Text = findstr
.Replacement.Text = changstr
.Forward = True
.Wrap = 1
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrdApp.Selection.Find.Execute(Replace:=2)
End Sub
{
object b="a";
Word.Book bk=Doc.Books.Item(ref b);
bk.Range.Text="";
}下载
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")) );
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
这个函数报错啊,说是拒绝访问,word的权限我都弄过了
WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
这句,报
Word 无法读取文档,文档可能损坏。
请尝试下列方法:
* 打开并修复文件。
* 用文本恢复转换器打开文件