Web应用Word编辑
本例采用的做法是:在后台调用服务器上一个已做好的word模板(含书签),使用word自带的功能对模板的内容进行编辑,完成后将改新的word保存在服务器上(比如MyNew1.doc),客户端再使用一个ocx控件打开服务器上的这个文件MyNew1.doc(也可以用respose方法输出)。
备注:本实例默认模板为:ProjectZJQualityFlow.doc。本使用了ajax方法获取服务器word所在路径,当然也可以使用隐藏域(input type="hidden" )在PageLoad中赋值路径,然后在js中获得隐藏域中的路径值。
cs的一个类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office;
using Microsoft.Office.Core;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
using PinMing.ProjectManage.BLL;
using System.IO;
/// <summary>
/// word模板创建工厂类
/// </summary>
public class ClsWordFactory
{
/// <summary>
/// 工程造价咨询word打印模板枚举
/// </summary>
public enum WordModelEnum
{
/// <summary>
/// 工程造价咨询质量控制流程单
/// </summary>
eProjectZJQualityFlow,
/// <summary>
/// 征求意见稿回执单
/// </summary>
eSolicitIdeaReturnReceipt
}
string path = "Document/"; //ConfigurationManager.AppSettings["UpFilePath"].ToString().Substring(1);
Microsoft.Office.Interop.Word.Document wDoc = null;
Microsoft.Office.Interop.Word.Application wApp = null;
public ClsWordFactory()
{
}
/// <summary>
/// 创建打印单
/// </summary>
/// <param name="wordTypeEnum">工程造价咨询word打印模板枚举</param>
/// <param name="expertid">项目关键字ID</param>
public string CreateWord(WordModelEnum wordTypeEnum, string expertid)
{
string docUrl = string.Empty;
switch (wordTypeEnum)
{
case WordModelEnum.eProjectZJQualityFlow:
BLLOrderXYXZ bll = new BLLOrderXYXZ();
DataTable dt = bll.GetProInfo(expertid).Tables[0];
docUrl = CreateProjectZJQualityFlow(dt, expertid);
break;
case WordModelEnum.eSolicitIdeaReturnReceipt:
return string.Empty;
break;
}
return docUrl;
}
#region 工程造价咨询质量控制流程单
/// <summary>
/// 工程造价咨询质量控制流程单
/// </summary>
/// <returns></returns>
private string CreateProjectZJQualityFlow(DataTable projectDt, string expertid)
{
string url = HttpContext.Current.Server.MapPath("Templet//ProjectZJQualityFlow.doc");
this.OpenWordDocument(url, ref wDoc, ref wApp);
object oEndOfDoc = "\\endofdoc";
object missing = System.Reflection.Missing.Value;
int wordEndRowIndex = 4;//添加行的首行号
//设定书签部分的数据
SetBookMarkData(wApp, projectDt);
//设定专业咨询人员
Microsoft.Office.Interop.Word.Table wordUserTable = wDoc.Tables[1];
SetUserTableView(wordUserTable, expertid, ref wDoc, ref wApp, ref wordEndRowIndex);
//收尾工作
SetEndPartTableView(wDoc, wApp, ref missing, expertid);
// return "../Document/OfficeBak" + expertid + ".doc";
return "WordReport/" + path + "WordPrintBak/" + expertid + ".doc";
}
/// <summary>
/// 设定专业咨询人员
/// </summary>
/// <param name="wordTable"></param>
/// <param name="wDoc"></param>
/// <param name="WApp"></param>
/// <returns></returns>
private bool SetUserTableView(Microsoft.Office.Interop.Word.Table wordTable, string expertid, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp, ref int startrow)
{
BLLOrderXYXZ bll = new BLLOrderXYXZ();
DataSet cpxx = bll.GetProjectSJRY(expertid);//获取审计人员列表
if (cpxx == null || cpxx.Tables[0].Rows.Count <= 0)
{
return false;
}
object Rownum = cpxx.Tables[0].Rows.Count;
object Columnnum = 1;
startrow = startrow + 1;//加上标题行
wordTable.Cell(startrow, 2).Split(ref Rownum, ref Columnnum);
wordTable.Cell(startrow, 3).Split(ref Rownum, ref Columnnum);
wordTable.Cell(startrow, 4).Split(ref Rownum, ref Columnnum);
for (int i = 0; i < cpxx.Tables[0].Rows.Count; i++)
{
&n
补充:Web开发 , 其他 ,