几种常见C#操作Word表格操作有哪些呢?让我们来看看具体的实例演示:

  1. bool saveChange = false;
  2. //C#操作Word表格操作
  3. object missing = System.Reflection.Missing.Value;
  4. object template = (object)templateFilePath;
  5. object filename = (object)saveFilePath;
  6. object isVisible = missing;
  7. object readOnly = missing;
  8. object breakType = Word.WdBreakType.wdSectionBreakNextPage;
  9. object isSaveChange = (object)saveChange;
  10. Word.Document doc = null;
  11. //定义一个Word.Application 对象
  12. Word.Application WordApp = new Word.ApplicationClass();
  13. //C#操作Word表格操作之打开文档
  14. doc = WordApp.Documents.Open(ref template,
  15. ref missing,ref readOnly,ref missing,
  16. ref missing, ref missing, ref missing,
  17. ref missing, ref missing,ref missing,
  18. ref missing, ref isVisible, ref missing,
  19. ref missing, ref missing, ref missing);
  20. //C#操作Word表格操作之设置页眉文本
  21. WordApp.ActiveWindow.ActivePane.View.SeekView =
  22. Word.WdSeekView.wdSeekCurrentPageHeader;
  23. WordApp.Selection.WholeStory();
  24. WordApp.Selection.TypeText( this.m_titleText );
  25. WordApp.ActiveWindow.ActivePane.View.SeekView =
  26. Word.WdSeekView.wdSeekMainDocument;
  27. //页面设置,设置页面为纵向布局,设置纸张类型为A4纸
  28. doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
  29. doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
  30. doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
  31. //C#操作Word表格操作之创建表格及设置表格和单元格属性
  32. object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
  33. doc.Content.Tables.Add(
  34. WordApp.Selection.Range, totalCount + 1,
  35. totalField - keyCount_1, ref missing,
  36. ref autoFitBehavior);
  37. //C#操作Word表格操作之合并单元隔
  38. doc.Content.Tables[1].Cell(i+1,j).Select();
  39. object moveUnit = Word.WdUnits.wdLine;
  40. object moveCount = 1;
  41. object moveExtend = Word.WdMovementType.wdExtend;
  42. WordApp.Selection.MoveUp(ref moveUnit,
  43. ref moveCount, ref moveExtend);
  44. WordApp.Selection.Cells.Merge();
  45. WordApp.Selection.Cells.VerticalAlignment =
  46. Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  47. doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”
  48. //添加表格行
  49. doc.Content.Tables[0].Rows.Add(ref beforeRow);
  50. //C#操作Word表格操作之添加表格列
  51. doc.Content.Tables[0].Columns.Add(ref beforeColumn);
  52. //文本居中
  53. WordApp.Selection.ParagraphFormat.Alignment =
  54. Word.WdParagraphAlignment.wdAlignParagraphCenter;
  55. WordApp.Selection.Cells.VerticalAlignment =
  56. Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  57. //C#操作Word表格操作之选中单元格文字垂直居中
  58. oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
  59. Word.Table newTable = oDoc.Tables[1];
  60. object beforeRow = newTable.Rows[1];
  61. newTable.Rows.Add(ref beforeRow);
  62. Word.Cell cell = newTable.Cell(1, 1);
  63. cell.Merge(newTable.Cell(1, 2));

最新文章

  1. vs2013中2.0类库提示是英文,解决方案
  2. 转载:《TypeScript 中文入门教程》 9、泛型
  3. (二)SQL Server分区创建过程
  4. 夺命雷公狗-----React---21--小案例之心情留言板
  5. EL和JSTL专题
  6. 微软开放WP开发者回复用户应用评论功能
  7. php解析一个url
  8. 第四十五课:MVC,MVP,MVVM的区别
  9. Google是如何做代码审查的?
  10. Tornado,了解一下
  11. Android-加载透明PNG图片变黑的问题
  12. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
  13. python修改txt文件内容
  14. jQuery图片轮播的具体实现
  15. php 分词
  16. Redis之(二)数据类型及存储结构
  17. Linux:从入门到放弃
  18. 微信h5支付出现“商家参数格式有误,请联系商家解决”
  19. pl/sql oracle数据库中文数据显示异常或者不能使用中文查询的解决办法
  20. 从rnn到lstm,再到seq2seq(一)

热门文章

  1. ExtJs4中的复选树级联选择
  2. Rhel6-cacti+nagios+ganglia(apache)配置文档
  3. js动画之简单运动二
  4. php超全局数组变量
  5. HiveQL(HiveSQL)跟普通SQL最大区别一直使用PIG,而今也需要兼顾HIVE
  6. MySQL表类型和存储引擎版本不一致解决方法
  7. Python框架、库以及软件资源汇总
  8. HDU 4251 --- 主席树(划分树是正解)
  9. I.MX6 eMMC 添加分区
  10. 任务太多,时间太少,GT凶猛,不留情面啊。。。