要实现你的想法,labview是可以做到的,去查example中的robot, 那个和你要的相似,但是复杂度很高
提供给你一个建议:
将你要转换的 按键, 颜色灰掉(在属性中把颜色变为背景颜色), 然后网上下个 倾斜的 按键
, 把之前的按键 覆盖。
其实这也就是一个 替换 按钮 表面 的方法而已。 同样可以做到你需要的。
1、在Word文档中插入一个书签,书签名称为“tl”;
2、新建一个C#项目,然后在引用中添加Word类库;由于我使用的是Office2007,因此选择的是"Microsoft Word 120 Object Library",如果你使用的是Office2003,就应该选择110;
3、在代码顶部添加对Word类库的引用;
using Word = MicrosoftOfficeInteropWord;
4、打开Word文档
object missingValue = SystemReflectionMissingValue;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1doc";
Word_Application oWord = new WordApplicationClass();
Word_Document oDoc;
oDoc = oWordDocumentsOpen(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
5、找到刚才添加的书签(注释部分为跳转至标签位置并添加文本的三种方法,做其他参考)
/方法一:使用Word应用程序变量,使用这种方法,wordAppDocumentsOpen()中确保isVisible的值为true
if (wordAppActiveDocumentBookmarksExists("BM_TEST"))
{
wordAppActiveDocumentBookmarksget_Item(ref bk)Select();
wordAppSelectionTypeText("insert text"); // 插入文本
}
方法二:使用Word文档变量
if (wordDocBookmarksExists("BM_TEST"))
{
wordDocBookmarksget_Item(ref bk)RangeText = "insert text"; // 插入文本
}
方法三:使用Goto函数,跳转到指定书签
object BookMarkName = "BM_TEST";
object what = MSWordWdGoToItemwdGoToBookmark;
wordDocActiveWindowSelectionGoTo(ref what, ref Nothing, ref Nothing, ref BookMarkName);
wordDocActiveWindowSelectionTypeText("Hello!");
/
object tmp = "t1";
WordRange startRange = oWordActiveDocumentBookmarksget_Item(ref tmp)Range;
6、删除在该位置的表格
WordTable tbl = startRangeTables[1];
tblDelete();
如果书签所在的位置并没有插入表格,程序并不会删除该位置下面的表格,而是会抛出异常,报错。
7、插入表格,并划线
//添加表格
oDocTablesAdd(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRangeTables[1]Borders[WdBorderTypewdBorderTop]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderLeft]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderRight]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderBottom]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderHorizontal]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderVertical]LineStyle = WdLineStylewdLineStyleSingle;
全部的代码如下:
object missingValue = SystemReflectionMissingValue;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1doc";
Word_Application oWord = new WordApplicationClass();
Word_Document oDoc;
oDoc = oWordDocumentsOpen(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
try
{
object tmp = "t1";
WordRange startRange = oWordActiveDocumentBookmarksget_Item(ref tmp)Range;
//删除指定书签位置后的第一个表格
WordTable tbl = startRangeTables[1];
tblDelete();
//添加表格
oDocTablesAdd(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRangeTables[1]Borders[WdBorderTypewdBorderTop]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderLeft]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderRight]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderBottom]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderHorizontal]LineStyle = WdLineStylewdLineStyleSingle;
startRangeTables[1]Borders[WdBorderTypewdBorderVertical]LineStyle = WdLineStylewdLineStyleSingle;
}
catch
{
//异常处理
}
object bSaveChange = true;
oDocClose(ref bSaveChange, ref missingValue, ref missingValue);
oDoc = null;
oWord = null;
有就没试过哦!
下面是关键性代码,但只是生成文字!
希望对您有用
添加引用:
using MicrosoftOfficeInteropWord;
//============================
private unsafe void ThreadWork()
{
try
{
thisSetBtnDisable();
string str = "数据库名:" + thisdbname;
int count = thislistTable2ItemsCount;
thisSetprogressBar1Max(count);
thisSetprogressBar1Val(1);
thisSetlblStatuText("0");
object template = MissingValue;
object obj3 = @"\endofdoc";
MicrosoftOfficeInteropWord_Application application = new MicrosoftOfficeInteropWordApplicationClass();
applicationVisible = false;
MicrosoftOfficeInteropWord_Document document = applicationDocumentsAdd(ref template, ref template, ref template, ref template);
applicationActiveWindowViewType = MicrosoftOfficeInteropWordWdViewTypewdOutlineView;
applicationActiveWindowViewSeekView = MicrosoftOfficeInteropWordWdSeekViewwdSeekPrimaryHeader;
applicationActiveWindowActivePaneSelectionInsertAfter("导出到Word");
applicationSelectionParagraphFormatAlignment = MicrosoftOfficeInteropWordWdParagraphAlignmentwdAlignParagraphRight;
applicationActiveWindowViewSeekView = MicrosoftOfficeInteropWordWdSeekViewwdSeekMainDocument;
MicrosoftOfficeInteropWordParagraph paragraph = documentContentParagraphsAdd(ref template);
paragraphRangeText = str;
paragraphRangeFontBold = 1;
paragraphRangeFontName = "宋体";
paragraphRangeFontSize = 12f;
paragraphRangeParagraphFormatAlignment = MicrosoftOfficeInteropWordWdParagraphAlignmentwdAlignParagraphCenter;
paragraphFormatSpaceAfter = 5f;
paragraphRangeInsertParagraphAfter();
for (int i = 0; i < count; i++)
{
string tableName = thislistTable2Items[i]ToString();
string str3 = "表名:" + tableName;
List<ColumnInfo> columnInfoList = thisdbobjGetColumnInfoList(thisdbname, tableName);
int num3 = columnInfoListCount;
if ((columnInfoList != null) && (columnInfoListCount > 0))
{
object obj4 = documentBookmarksget_Item(ref obj3)Range;
MicrosoftOfficeInteropWordParagraph paragraph2 = documentContentParagraphsAdd(ref obj4);
paragraph2RangeText = str3;
paragraph2RangeFontBold = 1;
paragraph2RangeFontName = "宋体";
paragraph2RangeFontSize = 10f;
paragraph2RangeParagraphFormatAlignment = MicrosoftOfficeInteropWordWdParagraphAlignmentwdAlignParagraphCenter;
paragraph2FormatSpaceBefore = 15f;
paragraph2FormatSpaceAfter = 1f;
paragraph2RangeInsertParagraphAfter();
obj4 = documentBookmarksget_Item(ref obj3)Range;
MicrosoftOfficeInteropWordParagraph paragraph3 = documentContentParagraphsAdd(ref obj4);
paragraph3RangeText = "";
paragraph3RangeFontBold = 0;
paragraph3RangeFontName = "宋体";
paragraph3RangeFontSize = 9f;
paragraph3RangeParagraphFormatAlignment = MicrosoftOfficeInteropWordWdParagraphAlignmentwdAlignParagraphCenter;
paragraph3FormatSpaceBefore = 1f;
paragraph3FormatSpaceAfter = 1f;
paragraph3RangeInsertParagraphAfter();
MicrosoftOfficeInteropWordRange range = documentBookmarksget_Item(ref obj3)Range;
MicrosoftOfficeInteropWordTable table = documentTablesAdd(range, num3 + 1, 10, ref template, ref template);
tableRangeFontName = "宋体";
tableRangeFontSize = 9f;
tableBordersEnable = 1;
tableRowsHeight = 10f;
tableAllowAutoFit = true;
tableCell(1, 1)RangeText = "序号";
tableCell(1, 2)RangeText = "列名";
tableCell(1, 3)RangeText = "数据类型";
tableCell(1, 4)RangeText = "长度";
tableCell(1, 5)RangeText = "小数位";
tableCell(1, 6)RangeText = "标识";
tableCell(1, 7)RangeText = "主键";
tableCell(1, 8)RangeText = "允许空";
tableCell(1, 9)RangeText = "默认值";
tableCell(1, 10)RangeText = "说明";
tableColumns[1]Width = 33f;
tableColumns[3]Width = 60f;
tableColumns[4]Width = 33f;
tableColumns[5]Width = 43f;
tableColumns[6]Width = 33f;
tableColumns[7]Width = 33f;
tableColumns[8]Width = 43f;
for (int j = 0; j < num3; j++)
{
ColumnInfo info = columnInfoList[j];
string colorder = infoColorder;
string columnName = infoColumnName;
string typeName = infoTypeName;
string length = infoLength;
string scale = infoScale;
string str9 = (infoIsIdentityToString()ToLower() == "true") "是" : "";
string str10 = (infoIsPKToString()ToLower() == "true") "是" : "";
string str11 = (infocisNullToString()ToLower() == "true") "是" : "否";
string str12 = infoDefaultValToString();
string str13 = infoDeTextToString();
if (lengthTrim() == "-1")
{
length = "MAX";
}
tableCell(j + 2, 1)RangeText = colorder;
tableCell(j + 2, 2)RangeText = columnName;
tableCell(j + 2, 3)RangeText = typeName;
tableCell(j + 2, 4)RangeText = length;
tableCell(j + 2, 5)RangeText = scale;
tableCell(j + 2, 6)RangeText = str9;
tableCell(j + 2, 7)RangeText = str10;
tableCell(j + 2, 8)RangeText = str11;
tableCell(j + 2, 9)RangeText = str12;
tableCell(j + 2, 10)RangeText = str13;
}
tableRows[1]RangeFontBold = 1;
tableRows[1]RangeParagraphFormatAlignment = MicrosoftOfficeInteropWordWdParagraphAlignmentwdAlignParagraphCenter;
tableRowsFirstShadingTexture = MicrosoftOfficeInteropWordWdTextureIndexwdTexture25Percent;
}
thisSetprogressBar1Val(i + 1);
thisSetlblStatuText((i + 1)ToString());
}
applicationVisible = true;
documentActivate();
thisSetBtnEnable();
MessageBoxShow("文档生成成功!", "完成", MessageBoxButtonsOK, MessageBoxIconAsterisk);
}
catch (Exception exception)
{
LogInfoWriteLog(exception);
MessageBoxShow("文档生成失败!(" + exceptionMessage + ")。\r\n请关闭重试。", "提示", MessageBoxButtonsOK, MessageBoxIconExclamation);
}
}
CODE:
//生成WORD程序对象和WORD文档对象
MicrosoftOfficeInteropWordApplication appWord = new Application();
MicrosoftOfficeInteropWordDocument doc = new Document();
object oMissing = SystemReflectionMissingValue;//这个是什么东西,我始终没搞明白-_-
//打开模板文档,并指定doc的文档类型
object objTemplate = ServerMapPath(p_TemplatePath);
object objDocType = WdDocumentTypewdTypeDocument;
doc = (Document)appWordDocumentsAdd(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//获取模板中所有的书签
Bookmarks odf = docBookmarks;
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"};
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",};
//循环所有的书签,并给书签赋值
for (int oIndex = 0; oIndex < testTableremarksLength; oIndex++)
{
obDD_Name = WD + testTableremarks[oIndex];
docBookmarksget_Item(ref obDD_Name)RangeText = p_TestReportTableRows[0][testTablevalues [oIndex]]ToString();//此处Range也是WORD中很重要的一个对象,就是当前 *** 作参数所在的区域
}
//第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序
object filename = ServerMapPath(p_SavePath) + "\\Testing_" + DateTimeNowToShortDateString() + "doc";
object miss = SystemReflectionMissingValue;
docSaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = TypeMissing;
object doNotSaveChanges = WdSaveOptionswdDoNotSaveChanges;
docClose(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWordApplicationQuit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
thisHid_ShowMessageValue = "生成成功!";
上述代码就是一个通过模板文件生成WORD的过程。其实也就是一个替换书签内容的过程。
在开发的过程中,有些数据是动态增加的,假如我要向一个表格中动态的添加几行数据,就无法用替换书签的方式来进行 *** 作,需要通过程序在文档页面的表格中添加行。
向表格中添加行,有两种 *** 作形式:一种是在WORD模板中已经存在了一个表格。一种是我们在程序中直接添加一个表格对象。
第一种情况下,需要注意:在WORD模板中要 *** 作的表格中,不能有纵向合并的单元格,不然程序无法获取到当前要 *** 作对象导致程序报错单元格的合并,我们可以在程序中控制。
第二种情况下就需要我们通过程序去直接添加表格了。
生成表格的代码具体如下:
1获取文档中已存在的表格:
MicrosoftOfficeInteropWordTable characterTable = docTables[2];//在document对象的集合 *** 作中,起始点是从1开始,并不是从0开始的,此处需要注意。
2在文档中直接生成表格,首先要获取插入表格的位置,然后添加表格对象:
object oEndOfDoc = "\\endofdoc";//WORD中预定义的书签,还有很多,此处就不一一列举。
object oMissing = SystemReflectionMissingValue;
Range wrdRng = docBookmarksget_Item(ref oEndOfDoc)Range;//获取当前文档的末尾位置。
wrdRngInsertAfter(" ");//插入一行,此处不能用 wrdRngInsertAfter(""),如果用这个,就不能换行,我也不知道为什么。
object oCollapseEnd = MicrosoftOfficeInteropWordWdCollapseDirectionwdCollapseEnd;
object oPageBreak = MicrosoftOfficeInteropWordWdBreakTypewdPageBreak;//分页符
wrdRngCollapse(ref oCollapseEnd);
wrdRngInsertBreak(ref oPageBreak);//插入了一页
wrdRngCollapse(ref oCollapseEnd);
wrdRngInsertAfter("信息");
wrdRngFontSize = 20;//指定 *** 作对象的文字大小
wrdRngFontBold = 1;//指定 *** 作对象的粗体:1为粗体,0为正常
wrdRngParagraphFormatAlignment = WdParagraphAlignmentwdAlignParagraphCenter;//指定 *** 作区域的文字布局:居中对齐
//上述代码的意思是:找到当前的末尾位置,然后插入一个分页符,相当于跳到了一个新页,在这个新页的顶端写入文字“信息”,并指定文字大小为20,粗体居中显示。
wrdRng = docBookmarksget_Item(ref oEndOfDoc)Range;
wrdRngInsertAfter(" ");
wrdRng = docBookmarksget_Item(ref oEndOfDoc)Range;
wrdRngInsertParagraphAfter();//插入一个段落,在此段落上插入一个2行一列的表格。
MicrosoftOfficeInteropWordTable newTable = docTablesAdd(wrdRng, 2, 1, ref oMissing, ref oMissing);
我们还可以对表格进行格式设置,此处我们就不在一一列举。
3下面我们分析一下对表格的单元格的 *** 作:合并,拆分。这个就需要我们根据实际表格来进行 *** 作:
//获取具体的某个单元格(1,1),获取第一行第一列的单元格
Cell cell = docTables[1]Cell(1,1);
cellRangeText="Text";//指定当前单元格的内容为Text
在Table的 *** 作中,添加新行:
object beforeRow = docTables[1]Rows[2];//此行是先获取到第二行
docTables[1]RowsAdd(ref beforeRow);//效果类似于在WORD中此表格的第二行上进行插入行 *** 作,插入的新行将会插入到当前行的上一行中,格式也是和此行一致的。
//合并单元格:感觉在此处合并单元格挺傻瓜的,你只需要指定你要合并的起始单元格和结束单元格,然后通过Merge *** 作就行了
Cell cell = docTables[1]Cell(iRow, 2);//列合并
cellMerge(docTables[1]Cell(iRow, 6));
Cell cell1 = docTables[1]Cell(iRow - 1, 1);//行合并
cell1Merge(docTables[1]Cell(iRow + 1, 1));
上述 *** 作就是在此程序中用到的一些知识点,还有好多的东西需要去熟悉、理解。
另外,在程序的测试过程中发现,当执行一次文档生成后,在资源管理器中始终有winwordexe进程杀不掉,目前的解决办法是:直接杀进程,代码如下:
protected void killAllProcess() // 杀掉所有winwordexe进程
{
SystemDiagnosticsProcess[] myPs;
myPs = SystemDiagnosticsProcessGetProcesses();
foreach (SystemDiagnosticsProcess p in myPs)
{
if (pId != 0)
{
string myS = "WINWORDEXE" + pProcessName + " ID:" + pIdToString();
try
{
if (pModules != null)
if (pModulesCount > 0)
{
SystemDiagnosticsProcessModule pm = pModules[0];
myS += "\n Modules[0]FileName:" + pmFileName;
myS += "\n Modules[0]ModuleName:" + pmModuleName;
myS += "\n Modules[0]FileVersionInfo:\n" + pmFileVersionInfoToString();
if (pmModuleNameToLower() == "winwordexe")
pKill();
}
}
catch
{ }
finally
{
}
}
}
}
目前为止,一个WORD文档就生成了。上述为我在这个程序开发中遇到的问题和解决方法,可能有好多地方都是考虑不全的,如果在程序开发中对WORD的 *** 作有新的认识的话,欢迎和我沟通交流,彼此提高!
下边是在网上一些比较好的摘抄:
创建新Word
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
oDoc = oWordDocumentsAdd(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
打开文档:
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
object fileName = @"E:CCCXCXXTestDocdoc";
oDoc = oWordDocumentsOpen(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);
导入模板
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
object fileName = @"E:XXXCCXTestdoc";
oDoc = oWordDocumentsAdd(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
添加新表
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
oDoc = oWordDocumentsAdd(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
WordRange tableLocation = oDocRange(ref start, ref end);
oDocTablesAdd(tableLocation, 3, 4, ref oMissing, ref oMissing);
表插入行
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
oDoc = oWordDocumentsAdd(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
WordRange tableLocation = oDocRange(ref start, ref end);
oDocTablesAdd(tableLocation, 3, 4, ref oMissing, ref oMissing);
WordTable newTable = oDocTables[1];
object beforeRow = newTableRows[1];
newTableRowsAdd(ref beforeRow);
单元格合并
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
oDoc = oWordDocumentsAdd(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
WordRange tableLocation = oDocRange(ref start, ref end);
oDocTablesAdd(tableLocation, 3, 4, ref oMissing, ref oMissing);
WordTable newTable = oDocTables[1];
object beforeRow = newTableRows[1];
newTableRowsAdd(ref beforeRow);
WordCell cell = newTableCell(1, 1);
cellMerge(newTableCell(1, 2));
单元格分离
object oMissing = SystemReflectionMissingValue;
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
oDoc = oWordDocumentsAdd( oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
WordRange tableLocation = oDocRange(ref start, ref end);
oDocTablesAdd(tableLocation, 3, 4, ref oMissing, ref oMissing);
WordTable newTable = oDocTables[1];
object beforeRow = newTableRows[1];
newTableRowsAdd(ref beforeRow);
WordCell cell = newTableCell(1, 1);
cellMerge(newTableCell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cellSplit(ref Rownum, ref Columnnum);
通过段落控制插入
object oMissing = SystemReflectionMissingValue;
object oEndOfDoc = "\endofdoc"; /// endofdoc is a predefined bookmark /
//Start Word and create a new document
Word_Application oWord;
Word_Document oDoc;
oWord = new WordApplication();
oWordVisible = true;
oDoc = oWordDocumentsAdd(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document
WordParagraph oPara1;
oPara1 = oDocContentParagraphsAdd(ref oMissing);
oPara1RangeText = "Heading 1";
oPara1RangeFontBold = 1;
oPara1FormatSpaceAfter = 24; //24 pt spacing after paragraph
oPara1RangeInsertParagraphAfter();
以上就是关于LabVIEW中怎么将控件任意角度旋转全部的内容,包括:LabVIEW中怎么将控件任意角度旋转、c#在word中插入bookmarks后如何用它来存贮数据,比如文字的word中的位置、怎么将bookmarks导入microsoft edge等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)