使用c#在word文檔中創(chuàng)建表格的方法詳解
更新時(shí)間:2013年05月20日 09:45:57 作者:
本篇文章是對(duì)使用c#在word文檔中創(chuàng)建表格的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
復(fù)制代碼 代碼如下:
public string CreateWordFile()
{
string message = "";
try
{
Object Nothing = System.Reflection.Missing.Value;
string name = "xiehuan.doc";
object filename = @"C:\Users\xiehuan\xxx\" + name; //文件保存路徑
//創(chuàng)建Word文檔
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//添加頁(yè)眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁(yè)眉內(nèi)容]");
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//設(shè)置右對(duì)齊
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁(yè)眉設(shè)置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設(shè)置文檔的行間距
//移動(dòng)焦點(diǎn)并換行
object count = 14;
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//換一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動(dòng)焦點(diǎn)
WordApp.Selection.TypeParagraph();//插入段落
//文檔中創(chuàng)建表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//設(shè)置表格樣式
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;
//填充表格內(nèi)容
newTable.Cell(1, 1).Range.Text = "產(chǎn)品詳細(xì)信息表";
newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體
//合并單元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//填充表格內(nèi)容
newTable.Cell(2, 1).Range.Text = "產(chǎn)品基本信息";
newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色
//合并單元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//填充表格內(nèi)容
newTable.Cell(3, 1).Range.Text = "品牌名稱:";
newTable.Cell(3, 2).Range.Text = "BrandName";
//縱向合并單元格
newTable.Cell(3, 3).Select();//選中一行
object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object moveCount = 5;
object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入圖片
string FileName = Picture;//圖片所在路徑
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 = 100f;//圖片寬度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//圖片高度
//將圖片設(shè)置為四周環(huán)繞型
Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
newTable.Cell(12, 1).Range.Text = "產(chǎn)品特殊屬性";
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
WordDoc.Paragraphs.Last.Range.Text = "文檔創(chuàng)建時(shí)間:" + DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//文件保存
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);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
message=name+"文檔生成成功,以保存到C:CNSI下";
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
return message;
}
您可能感興趣的文章:
- C# WORD操作實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)通過(guò)模板自動(dòng)創(chuàng)建Word文檔的方法
- C# Word 類庫(kù)的深入理解
- 比較全的一個(gè)C#操作word文檔示例
- C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
- asp.net(c#)下讀取word文檔的方法小結(jié)
- JavaScript打開(kāi)word文檔的實(shí)現(xiàn)代碼(c#)
- word ppt excel文檔轉(zhuǎn)換成pdf的C#實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- C#添加、讀取Word腳注尾注的方法
相關(guān)文章
C#中數(shù)組、ArrayList和List三者的區(qū)別詳解
這篇文章主要介紹了C#中數(shù)組、ArrayList和List三者的區(qū)別詳解,對(duì)于三者之間的區(qū)別想要了解的可以進(jìn)來(lái)了解一下。2016-12-12c# winform 關(guān)閉窗體時(shí)同時(shí)結(jié)束線程實(shí)現(xiàn)思路
th.IsBackground = true解決線程問(wèn)題,意思就是把線程設(shè)置為后臺(tái)線程,感興趣的朋友可以多了解下,如何有什么妙招還請(qǐng)多多指導(dǎo)哈2013-02-02在C#中List集合使用First()方法獲取第一個(gè)元素的操作
這篇文章主要介紹了在C#中List集合使用First()方法獲取第一個(gè)元素的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12Unity之Luaframework框架lua調(diào)用C#方法
這篇文章主要介紹了Unity之Luaframework框架lua調(diào)用C#方法,在這里需要寫(xiě)一個(gè)C#腳本,腳本里寫(xiě)方法需要在lua中調(diào)用,具體實(shí)例代碼參考下本文吧2021-09-09C#修改IIS站點(diǎn)framework版本號(hào)的方法
這篇文章主要介紹了C#修改IIS站點(diǎn)framework版本號(hào)的方法,涉及C#調(diào)用使用ASP.NET IIS注冊(cè)工具Aspnet_regiis.exe進(jìn)行IIS站點(diǎn)framework版本號(hào)修改的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10