哇,博客終於審批經過啦!趕忙不忘過來寫篇博文記念一下!!哈哈app
其實開通這個博客一方面是但願養成本身良好的學習習慣,另外一方面也是以往老是本身默默的單方面的獲得其它博友們的幫助,久而久之,內心過意不去,因此也想把本身平時遇到的難題或經驗仍是小方法也跟你們分享一下,哦啦!ide
費話很少說,正題!學習
最近在作一個網上報名考試網站,裏面涉及到准考證的打印,想說把准考證信息動態生成文本文檔,供考生下載這麼一個功能,走了一些彎路,下面把個人成果分享給你們,但願幫助到一些正在經歷相似難題的親們……字體
首先,要建立word對象網站
建立以前別忘了引用
using Microsoft.Office.Interop.Word;ui
Object Nothing = System.Reflection.Missing.Value;code
//建立Word文檔
Application WordApp = new ApplicationClass();
Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);orm
//添加頁眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁眉內容]");
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;//設置右對齊
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁眉設置對象
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設置文檔的行間距圖片
//移動焦點並換行
object count = 8;
object WdLine = WdUnits.wdLine;//換一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動焦點
WordApp.Selection.TypeParagraph();//插入段落
//文檔中建立表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 8, 3, ref Nothing, ref Nothing);
//設置表格樣式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;
//填充表格內容
newTable.Cell(1, 1).Range.Text = row["posname"].ToString() + "考試准考證";
newTable.Cell(1, 1).Range.Bold = 2;//設置單元格中字體爲粗體
newTable.Cell(1, 1).Height = 30;
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
newTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
//填充表格內容
newTable.Cell(2, 1).Range.Text = "姓名";
newTable.Cell(2, 2).Range.Text = "xxxx";
newTable.Cell(3, 1).Range.Text = "身份證號";
newTable.Cell(3, 2).Range.Text = "xxxx";
newTable.Cell(4, 1).Range.Text = "准考證號";
newTable.Cell(4, 2).Range.Text = "xxxx";
newTable.Cell(5, 1).Range.Text = "考點名稱";
newTable.Cell(5, 2).Range.Text = "xxxx";
newTable.Cell(6, 1).Range.Text = "考點地址";
newTable.Cell(6, 2).Range.Text = "xxxx";
newTable.Cell(7, 1).Range.Text = "考試座位";
newTable.Cell(7, 2).Range.Text = "xxxx";
newTable.Cell(8, 1).Range.Text = "考試時間";
newTable.Cell(8, 2).Range.Text = "xxxx";
到這裏爲此,一個簡單的文檔大體就成型了,若是須要在本文檔里加入圖片的話呢,請看下面:
//縱向合併單元格
newTable.Cell(2, 3).Select();//選中一行
object moveUnit = WdUnits.wdLine;
object moveCount = 6;
object moveExtend = WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
newTable.Cell(2, 3).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
//插入圖片
string FileName = "E:\\123.jpg";//圖片所在路徑
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 80f;//圖片寬度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 110f;//圖片高度
//將圖片設置爲四周環繞型
Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = WdWrapType.wdWrapSquare;
WordDoc.Paragraphs.Last.Range.Text = "文檔建立時間:" + DateTime.Now.ToString();//「落款」
WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
string name = "123.doc";
object filename = Server.MapPath("TicketFile") + "\\" + name; //文件保存路徑
//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
if(WordDoc != null)
{
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordDoc = null;
}
if (WordApp != null)
{
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
WordApp = null;
}
寫到這裏,整個文檔的生成就大功告成了,不過這其中我有個疑惑,就是在關閉文檔時,我以前的代碼是這樣的
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
這麼寫以後,總是在quit這裏報錯,後面改爲上面那種樣子以後,不知道是否是由於這個緣由,「莫名奇妙」地就行了 ,有哪一個高手給我解釋一下!!!嘿嘿
若是想把該 文檔直接下載到本地的話,就簡單了,相信你們都會,我把代碼粘在下面:
string path = Server.MapPath("TicketFile/") + name;
FileInfo fi = new FileInfo(path);
if (fi.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream;charset=gb2321";
Response.WriteFile(fi.FullName);
Response.Flush();
Response.Close();
}
}
好了好了,這就是我弄了兩天的東西,呵呵,菜鳥一個,但願能對你們有幫助,少走彎路!!!