asp.net 页面数据导入word模板

首先添加应用"Microsoft Word 11.0 Object library"的COM组件,从而来实现读取word文档的对象。

点击事件代码

代码
 
   
protected void LinkButton1_Click( object sender, EventArgs e)
{
MyWordClass test
= new MyWordClass();
LTP.Accounts.Bus.User user
= JMWMgr2.Utility.GetCurrentUser();

try
{
string SaveURL = HttpContext.Current.Server.MapPath( " ~/upFiles/ " + user.DepartmentID + " / " + user.UserID);

if ( ! System.IO.Directory.Exists(SaveURL))
{
System.IO.Directory.CreateDirectory(SaveURL);
SaveURL
= SaveURL + " /作业风险分析控制表.doc " ;
ToWordFile(SaveURL,test);
}
else
{
SaveURL
= SaveURL + " /作业风险分析控制表.doc " ;
if (System.IO.File.Exists(SaveURL))
{
// if (rp.NextAudit == "")
// {
System.IO.File.Delete(SaveURL);
ToWordFile(SaveURL,test);
// }
}
else
{
ToWordFile(SaveURL,test);
}
}
Response.Redirect(
" ../upFiles/ " + user.DepartmentID + " / " + user.UserID + " /作业风险分析控制表.doc " );


}
catch (Exception ex)
{
Tools.ShowMess(ex.Message.Replace(
@" \\ " , @" \\\\ " ), this );
}
finally
{
// test.Quit();
}

}
protected void ToWordFile( string SaveURL, MyWordClass test)
{
string id = Request[ " id " ];
RC.Model.ZuoYeFXFXKZB model
= RC.BLL.ZuoYeFXFXKZB.GetModelByID( int .Parse(id));
if (model == null )
{
return ;
}
else
{
try
{
FileInfo f
= new FileInfo(HttpContext.Current.Server.MapPath( " ~/js/print.doc " ));
try
{
f.CopyTo(SaveURL);
}
catch (Exception ex)
{
Tools.ShowMess(ex.Message.Replace(
@" \\ " , @" \\\\ " ), this );
return ;
}

test.Open(SaveURL);

test.ReplaceText(
" $zyxm$ " , model.ZuoYeXM.ToString());
test.ReplaceText(
" $jhgzsj$ " , model.JiHuaGZSJ.Value.ToString( " yyyy-MM-dd " ));
test.ReplaceText(
" $fxr$ " , model.FenXiR.ToString());
test.ReplaceText(
" $shr$ " , model.ShenHeR.ToString());
test.ReplaceText(
" $fxwcsj$ " , model.FenXiWCSJ.Value.ToString( " yyyy-MM-dd " ));


List
< RC.Model.ZuoYeFXFXKZB2 > zyfxlist = RC.BLL.ZuoYeFXFXKZB2.GetZuoYeFXFXKZB2ListByCondition( 0 , " ZuoYeFXFXKZB_ID=' " + id + " 'and TianJiaLX!=2 " );
int ssss = 0 ;
foreach (RC.Model.ZuoYeFXFXKZB2 r in zyfxlist)
{
if (r.ToString() != "" )
{
test.ReplaceText(
" $wxys$ " , r.WeiXianYS.ToString());
test.ReplaceText(
" $kzcs$ " , r.KongZhiCS.ToString());
test.ReplaceText(
" $zrr$ " , r.ZeRenR.ToString());
test.ReplaceText(
" $kzsd$ " , r.KongZhiSD.ToString());
test.ReplaceText(
" $lssj$ " , r.LuoShiSJ.Value.ToString( " yyyy-MM-dd " ));


}
else
{
ssss
+= 1 ;
}

}


test.Save();
}

catch (Exception ex)
{
Tools.ShowMess(ex.Message.Replace(
@" \\ " , @" \\\\ " ), this );
}
finally
{
test.Save();
test.Close();
test.Quit();
}

}
}

2.操作类代码

 

代码
 
   
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;

/// <summary>
/// MyWordClass 的摘要说明
/// </summary>
public class MyWordClass
{
private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic; // a reference to Word application
private Microsoft.Office.Interop.Word.Document oDoc; // a reference to the document
public MyWordClass()
{
// 构造与Microsoft Word的COM对象接口
oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
}



/// <summary>
/// 打开模板 ,打开一个文件(该文件必须存在),激活它
/// </summary>
/// <param name="strFileName"></param>
public void Open( string strFileName)
{
object fileName = strFileName;
object readOnly = false ;
object isVisible = false ;
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();
}


// Open a new document
public void Open()
{
object missing = System.Reflection.Missing.Value;
oDoc
= oWordApplic.Documents.Add( 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>
/// 退出wordapp组件对象
/// </summary>
public void Quit()
{
object missing = System.Reflection.Missing.Value;
oWordApplic.Application.Quit(
ref missing, ref missing, ref missing);

}

public void Save()
{
oDoc.Save();
}


/// <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;
return 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);
}

/// <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);
}

// 保存为HTML文件格式
public void SaveAsHtml( string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
object Format = ( int )Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
oDoc.SaveAs(
ref fileName, ref Format, 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);
}



public void InsertText( string strText)
{
oWordApplic.Selection.TypeText(strText);
}

public void InsertLineBreak()
{
oWordApplic.Selection.TypeParagraph();
}
public void InsertLineBreak( int nline)
{
for ( int i = 0 ; i < nline; i ++ )
oWordApplic.Selection.TypeParagraph();
}

// Change the paragraph alignement
public void SetAlignment( string strType)
{
switch (strType)
{
case " Center " :
oWordApplic.Selection.ParagraphFormat.Alignment
= Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
break ;
case " Left " :
oWordApplic.Selection.ParagraphFormat.Alignment
= Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
break ;
case " Right " :
oWordApplic.Selection.ParagraphFormat.Alignment
= Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
break ;
case " Justify " :
oWordApplic.Selection.ParagraphFormat.Alignment
= Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
break ;
}

}


// 如果您使用thif函数来更改字体您应该再次调用它
// 没有参数没有特定的格式,以设置字体
public void SetFont( string strType)
{
switch (strType)
{
case " Bold " :
oWordApplic.Selection.Font.Bold
= 1 ;
break ;
case " Italic " :
oWordApplic.Selection.Font.Italic
= 1 ;
break ;
case " Underlined " :
oWordApplic.Selection.Font.Subscript
= 0 ;
break ;
}

}

// disable all the style
public void SetFont()
{
oWordApplic.Selection.Font.Bold
= 0 ;
oWordApplic.Selection.Font.Italic
= 0 ;
oWordApplic.Selection.Font.Subscript
= 0 ;

}

public void SetFontName( string strType)
{
oWordApplic.Selection.Font.Name
= strType;

}

public void SetFontSize( int nSize)
{
oWordApplic.Selection.Font.Size
= nSize;

}

public void InsertPagebreak()
{
// VB : Selection.InsertBreak Type:=wdPageBreak
object pBreak = ( int )Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWordApplic.Selection.InsertBreak(
ref pBreak);
}

// Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error

public void GotoBookMark( string strBookMarkName)
{
// VB : Selection.GoTo What:=wdGoToBookmark, Name:="nome"
object missing = System.Reflection.Missing.Value;

object Bookmark = ( int )Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(
ref Bookmark, ref missing, ref missing, ref NameBookMark);
}

public void GoToTheEnd()
{
// VB : Selection.EndKey Unit:=wdStory
object missing = System.Reflection.Missing.Value;
object unit;
unit
= Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(
ref unit, ref missing);

}
public void GoToTheBeginning()
{
// VB : Selection.HomeKey Unit:=wdStory
object missing = System.Reflection.Missing.Value;
object unit;
unit
= Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.HomeKey(
ref unit, ref missing);

}

public void GoToTheTable( int ntable)
{

object missing = System.Reflection.Missing.Value;
object what;
what
= Microsoft.Office.Interop.Word.WdUnits.wdTable;
object which;
which
= Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count;
count
= ntable;
Range r
= oWordApplic.Selection.GoTo( ref what, ref which, ref count, ref missing);
oWordApplic.Selection.Find.ClearFormatting();

oWordApplic.Selection.Text
= "" ;



}

public void GoToRightCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction
= Microsoft.Office.Interop.Word.WdUnits.wdCell;
oWordApplic.Selection.MoveRight(
ref direction, ref missing, ref missing);
}

public void GoToLeftCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction
= Microsoft.Office.Interop.Word.WdUnits.wdCell;
oWordApplic.Selection.MoveLeft(
ref direction, ref missing, ref missing);
}

public void GoToDownCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction
= Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveDown(
ref direction, ref missing, ref missing);
}

public void GoToUpCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction
= Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(
ref direction, ref missing, ref missing);
}


// this function doesn't work
public void InsertPageNumber( string strType, bool bHeader)
{
object missing = System.Reflection.Missing.Value;
object alignment;
object bFirstPage = false ;
object bF = true ;
// if (bHeader == true)
// WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber = bF;
switch (strType)
{
case " Center " :
alignment
= Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
// WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage);
// Word.Selection objSelection = WordApplic.pSelection;

// oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break ;
case " Right " :
alignment
= Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;
// oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break ;
case " Left " :
alignment
= Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(
ref alignment, ref bFirstPage);
break ;
}

}

public void selectionTable()
{

Object Nothing
= System.Reflection.Missing.Value;
// oDoc.Content.Tables[1].Rows.Add(ref Nothing);

// oDoc.Content.Tables[2].Rows.Add(ref Nothing);
// oDoc.Content.Tables[2].Rows.Add(ref Nothing);
// oDoc.Content.Tables[2].Rows.Add(ref Nothing);

object missing = System.Reflection.Missing.Value;
object what;
what
= Microsoft.Office.Interop.Word.WdUnits.wdWord;
object which;
which
= Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count;
count
= 3 ;


// object s1=Microsoft.Office.Interop.Word.
oDoc.Content.Tables[ 2 ].Select();

object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine; // 换一行;

// oWordApplic.Selection.Find.Text = "$ssss$";
// oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
// oWordApplic.Selection.Find.ClearFormatting();


oWordApplic.Selection.MoveDown(
ref WdLine, ref count, ref Nothing);
Microsoft.Office.Interop.Word.Table newTable
= oDoc.Tables.Add(oWordApplic.Selection.Range, 12 , 3 , ref Nothing, ref Nothing);


}












需求虽然实现了,但个人能力太欠缺,实现的效果很差!

转载于:https://www.cnblogs.com/wulongwen/archive/2010/09/29/1838310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值