using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using System.Reflection; namespace WordAndExcel.Word
{
public partial class makeWord : System.Web.UI.Page
{
Microsoft.Office.Interop.Word.Application appWord = null;
Microsoft.Office.Interop.Word.Document docWord = null;
Range range = null;
object missing = null; protected void Page_Load(object sender, EventArgs e)
{
Document wordDoc = OpenWord(Server.MapPath("./file/law.doc"));
MakeWord(wordDoc, Server.MapPath("./file/laws.doc"));
}
/// <summary>
/// 打开Word
/// </summary>
/// <param name="str_Path">文件路径</param>
/// <returns></returns>
protected Document OpenWord(object str_Path)
{
missing = Missing.Value;
appWord = new Microsoft.Office.Interop.Word.Application();
appWord.Visible = false;
docWord = appWord.Documents.Open(ref str_Path, 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);
return docWord;
}
/// <summary>
/// 操作Word
/// </summary>
/// <param name="docWord">文档对象</param>
protected void MakeWord(Document docWord, object str_Path)
{
try
{
object startPostion = (docWord.Characters.Count - ) as object;
Range tableLocation = docWord.Range(ref startPostion, ref startPostion); ///文檔對象 從頭開始
///
Microsoft.Office.Interop.Word.Table newTable = docWord.Tables.Add(tableLocation, , , ref missing, ref missing);
///table 樣式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
newTable.Rows[i + ].Cells[j + ].Width = (float)(255F);
newTable.Rows[i + ].Cells[j + ].Height = (float)(155F);
TableAddText(newTable.Rows[i + ].Cells[j + ].Range);
} }
}
catch (Exception ee)
{
Response.Write(ee.ToString());
}
finally
{ object saveChange = true;
docWord.SaveAs(ref str_Path, 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);
docWord.Close(ref saveChange, ref missing, ref missing);
appWord.Quit(ref saveChange, ref missing, ref missing);
}
} public void TableAddText(Range range)
{
range.Text = "John:(寄)";
// appWord.Selection.Text = "童新:(寄)";
// appWord.Selection.TypeParagraph();
// appWord.Selection.Text = "童新:(收)";
range.Text += "Jerry:(收)";
object Anchor = range; // 插入圖片
string FileName = Server.MapPath("./file/law.jpg");//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
// object Anchor = docWord.Application.Selection.Range;
docWord.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
docWord.Application.ActiveDocument.InlineShapes[].Width = 100f;//图片宽度
docWord.Application.ActiveDocument.InlineShapes[].Height = 20f;//图片高度 //插入Hyperlink
Microsoft.Office.Interop.Word.Selection mySelection = appWord.ActiveWindow.Selection;
mySelection.Start = ;
mySelection.End = ;
Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;
Microsoft.Office.Interop.Word.Hyperlinks myLinks = docWord.Hyperlinks;
object linkAddr = @"http://www.cnblogs.com/tx720/";
Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,ref missing);
appWord.ActiveWindow.Selection.InsertAfter("\n"); //添加舉行形狀 并且添加文字
Shape s = range.Document.Shapes.AddShape(, , , , , ref Anchor);
s.TextFrame.TextRange.Text = "dds"; }
}
}