表格处理(插入表格、设置格式、填充内容、表格中加图片)
#region 表格处理(插入表格、设置格式、填充内容)
/// <summary>
/// 表格处理
/// </summary>
/// <param name="filePath">word文件名</param>
/// <returns></returns>
public static bool AddTable(string filePath)
{
try
{
Object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application WordApp = new Application();
WordApp.Visible = true;
object filename = filePath;
Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//插入表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
//设置表格
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;
//填充表格内容
newTable.Cell(1, 1).Range.Text = "我的简历";
//设置单元格中字体为粗体
newTable.Cell(1, 1).Range.Bold = 2;
//合并单元格
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;
//填充表格内容
newTable.Cell(2, 1).Range.Text = "座右铭:";
//设置单元格内字体颜色
newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
//合并单元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//填充表格内容
newTable.Cell(3, 1).Range.Text = "姓名:";
newTable.Cell(3, 2).Range.Text = "雷鑫";
//纵向合并单元格
newTable.Cell(3, 3).Select();
//选中一行
object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object moveCount = 3;
object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//表格中插入图片
string pictureFileName = System.IO.Directory.GetCurrentDirectory() + @"\picture.jpg";
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
//图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;
//图片高度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;
//将图片设置为四周环绕型
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 = "备注:";
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(ref oMissing);
//保存
WordDoc.Save();
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return false;
}
}
#endregion #region 表格处理
把Word文档转化为Html文件
#region 把Word文档转化为Html文件
/// <summary>
/// 把Word文档转化为Html文件
/// </summary>
/// <param name="wordFileName">word文件名</param>
/// <param name="htmlFileName">要保存的html文件名</param>
/// <returns></returns>
public static bool WordToHtml(string wordFileName, string htmlFileName)
{
try
{
Object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application WordApp = new Application();
WordApp.Visible = true;
object filename = wordFileName;
Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Type wordType = WordApp.GetType();
// 打开文件
Type docsType = WordApp.Documents.GetType();
// 转换格式,另存为
Type docType = WordDoc.GetType();
object saveFileName = htmlFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc,
new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });
其它格式:
//保存
WordDoc.Save();
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return false;
}
}
#endregion 把Word文档转化为Html文件
分享到:
相关推荐
C#操作Word——C#向Word文档中写入文字
C#操作word文档 C#实现Word中表格信息读取
内容概要:1、运用 Range 对象处理 Word 文档内容;2、如何使用C#在Word文档中插入图片以及读取Word文档中的图片?3、提取word 文档中的图片信息;4、提取WORD中的所有InlineShape图片并保存成文件;5、C#编程实现...
C#操作Word文档(根据模板生成文档、替换内容、插入表格等)
分享C#操作Word表格的常用方法总结,c#操作word的代码比较少
此程序主要展示了如何把图片插入事先设定好的书签位置之中。 下载之后可以直接运行。
c#根据word模板生成word文档 附件中包含程序源码以及word模板文件 需要引用的DLL已经在源码中标出
通过C#调用NPOI组件生成Word文档,在生成Word文档过程中插入表格以及插入图片,使用到了C#,扩展等技术
c#打开word文档及读取word文档
一段C#操作word,如何进行查找和替换
C#操作Word表格.doc C#操作WORD辅助类.txt c#操作word模板插入文字、图片及表格.doc C#操作Word在Word文档中插入图片.docx C#用word模板做文档.doc 收集的C#操作Word的各种代码。直接复制粘贴到程序里面即可。
C#操作Word文档C#操作Word文档C#操作Word文档C#操作Word文档
学习c#的绝好实例——图像处理,一个完整的工程,对位图的简单处理,可以学到控件和简单图像处理。
C#得到word里面的书签,并向书签插入内容,将DateGridView内容作为表格插入到word书签中。
c#操作word文档提供c#操作word的源代码。
用C#动态生成Word文档并将数据填入Word表格中
VS2010下c#操作word进行批量查找和替换。选择路径后程序自动进行批量查找和替换,去持子目录搜索、大小写匹配、整字匹配、通配符匹配。
C#读取word文档实例浅析是在C#编程操作Word遇到问题的解决技巧。
C#操作word文档记录
批量的把html 文件转换成word,不需要电脑上有word软件!并且支持本地图片,html链接的图片必须在本地,可以把图片转换到word里!