C# VBA 填充Word表格 ,并随意转移单元格焦点或插入文本,及相关文字的颜色、粗体等相关属性。
Posted on 2009-08-01 14:23 且行且思 阅读(2713) 评论(1) 编辑 收藏 举报Word自带的宏,可以给我们在VBA调用时,给予足够的帮助与支持。如图
,方便,快捷。

using Microsoft.Office.Interop.Word;
using System.IO;
using System.Web;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Net;
生成以下表格的单元格:



using Microsoft.Office.Interop.Word;
using System.IO;
using System.Web;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Net;
/// <summary>
/// 把数据导入到.doc文件
/// </summary>
/// <param name="ds"></param>
public void ExportToWord()
{
object tempFileName = @"C:\eeeeee.doc";
object filename = @"C:\555555.doc";
object tableBehavior = Microsoft.Office.Interop.Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object autoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
object extend = System.Reflection.Missing.Value;
object breakType = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
object count = 7;
object character = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application myWord = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word._Document myDoc = new Microsoft.Office.Interop.Word.DocumentClass();
object oMissing = System.Reflection.Missing.Value;
object oReadOnly = false;
myDoc = myWord.Documents.Open(ref tempFileName, ref oMissing, ref oReadOnly, 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);
myDoc.Activate();
Table newTable = myDoc.Tables[1];
//设置表格样式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
////填充表格内容
//newTable.Cell(1, 1).Range.Text = "产品详细信息表 \n\r ssdfgdfg \n\r ";
//newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
////填充表格内容
//newTable.Cell(1, 1).Range.Text = "产品详细信息表 \n\r d \n\r ";
//newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
object dirc = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart;
object tend = Microsoft.Office.Interop.Word.WdMovementType.wdMove;
newTable.Cell(1, 1).Range.Select(); //获取焦点
count = 1;
myDoc.ActiveWindow.Selection.MoveRight(ref character, ref count, ref tend);
newTable.Cell(1, 1).Range.Text = "填充表格内容f";
newTable.Cell(1, 1).Range.Font.Color = WdColor.wdColorRed;
myDoc.ActiveWindow.Selection.Collapse(ref dirc);
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorRed;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myDoc.ActiveWindow.Selection.InsertAfter("第二次插入");
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorGreen;
myDoc.ActiveWindow.Selection.Font.Bold = 1;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myDoc.ActiveWindow.Selection.InsertAfter("第三次插入");
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorDarkBlue;
myDoc.ActiveWindow.Selection.Font.Bold = 1;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myWord.Application.Selection.TypeParagraph(); //重新起新行
myWord.Application.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
myDoc.ActiveWindow.Selection.InsertAfter("第4次插入");
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorDarkGreen;
myDoc.ActiveWindow.Selection.Font.Italic = 1;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myWord.Application.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
myDoc.ActiveWindow.Selection.InsertAfter(" 日期:" +DateTime.Today);
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorBlue;
myDoc.ActiveWindow.Selection.Font.Bold = 1;
//保存word文件到指定的目录下
try
{
myDoc.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);
myWord.Visible = true;
}
catch
{
return;
}
myWord.Visible = true;
}
/// 把数据导入到.doc文件
/// </summary>
/// <param name="ds"></param>
public void ExportToWord()
{
object tempFileName = @"C:\eeeeee.doc";
object filename = @"C:\555555.doc";
object tableBehavior = Microsoft.Office.Interop.Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object autoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
object extend = System.Reflection.Missing.Value;
object breakType = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
object count = 7;
object character = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application myWord = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word._Document myDoc = new Microsoft.Office.Interop.Word.DocumentClass();
object oMissing = System.Reflection.Missing.Value;
object oReadOnly = false;
myDoc = myWord.Documents.Open(ref tempFileName, ref oMissing, ref oReadOnly, 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);
myDoc.Activate();
Table newTable = myDoc.Tables[1];
//设置表格样式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
////填充表格内容
//newTable.Cell(1, 1).Range.Text = "产品详细信息表 \n\r ssdfgdfg \n\r ";
//newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
////填充表格内容
//newTable.Cell(1, 1).Range.Text = "产品详细信息表 \n\r d \n\r ";
//newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
object dirc = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart;
object tend = Microsoft.Office.Interop.Word.WdMovementType.wdMove;
newTable.Cell(1, 1).Range.Select(); //获取焦点
count = 1;
myDoc.ActiveWindow.Selection.MoveRight(ref character, ref count, ref tend);
newTable.Cell(1, 1).Range.Text = "填充表格内容f";
newTable.Cell(1, 1).Range.Font.Color = WdColor.wdColorRed;
myDoc.ActiveWindow.Selection.Collapse(ref dirc);
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorRed;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myDoc.ActiveWindow.Selection.InsertAfter("第二次插入");
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorGreen;
myDoc.ActiveWindow.Selection.Font.Bold = 1;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myDoc.ActiveWindow.Selection.InsertAfter("第三次插入");
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorDarkBlue;
myDoc.ActiveWindow.Selection.Font.Bold = 1;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myWord.Application.Selection.TypeParagraph(); //重新起新行
myWord.Application.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
myDoc.ActiveWindow.Selection.InsertAfter("第4次插入");
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorDarkGreen;
myDoc.ActiveWindow.Selection.Font.Italic = 1;
newTable.Cell(1, 1).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
myDoc.ActiveWindow.Selection.EndKey(ref unit, ref extend);
myWord.Application.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
myDoc.ActiveWindow.Selection.InsertAfter(" 日期:" +DateTime.Today);
myDoc.ActiveWindow.Selection.Font.Color = WdColor.wdColorBlue;
myDoc.ActiveWindow.Selection.Font.Bold = 1;
//保存word文件到指定的目录下
try
{
myDoc.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);
myWord.Visible = true;
}
catch
{
return;
}
myWord.Visible = true;
}
生成以下表格的单元格:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!