当前位置:网站首页>Never forget the original intention, and be lazy if you can: C # operate word files
Never forget the original intention, and be lazy if you can: C # operate word files
2022-06-30 06:28:00 【Ki1381】
Changed company , The informatization construction of new units is still on the way .OA Also under construction , At present, the project invoice reimbursement process is still manual , You can only fill out the form offline . It doesn't matter whether it is printed out after filling in the computer or directly typed out by hand with a blank template , at will . There were too many projects overstocked during the epidemic , So filling out the form after returning to work soon made me angry . Especially the amount in words and figures , There is also the format of lower case amount in the third digit , Wasted too much time , There's hardly ever been a success , I can't bear it ... So I finally opened it silently devenv. Why not write a program , How much energy can be saved .

However, from the beginning, it is generated line by line according to the format of the template Word It seems that the workload of the document is a little heavy , In line with the purpose of being lazy if you can , I came up with a way : Replace the blank space with a placeholder , Make a replacement in the program . say concretely , Is to use some special strings to occupy the position first , The format is also adjusted , Replace these special strings with real and meaningful content in the program , As the figure below :

So requirements are translated into how to use C# Realization Word String replacement for . Obviously, there should be many people who have this need , The relevant code was found on the Internet soon , After testing, it is reliable . operation Word The core code of document replacement text is ( Need to introduce... Into the project word Of com):
private void FindAndReplace(Microsoft.Office.Interop.Word.Application WordApp, object findText, object replaceWithText)
{
object matchCase = true; // Focus on !!
object matchWholeWord = false; // Focus on !!
object matchWildCards = false;
object matchSoundsLike = false;
object nmatchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
WordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
ref nmatchAllWordForms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replaceAll, ref matchKashida,
ref matchDiacritics, ref matchAlefHamza,
ref matchControl);
}The core code of the call is :
// Open the document
MSWord.Application wordApp = new MSWord.Application();
MSWord.Document wordDoc = wordApp.Documents.Open( Template file name with placeholder );
wordApp.Visible = false;
wordDoc.Activate();
FindAndReplace(wordApp, "##YY", txtYear.Text.Trim());
FindAndReplace(wordApp, "#M", txtMonth.Text.Trim());
FindAndReplace(wordApp, "#D", txtDay.Text.Trim());
FindAndReplace(wordApp, "@@SY", txtSY.Text.Trim());
FindAndReplace(wordApp, "@@XX", txtJE.Text.Trim());
FindAndReplace(wordApp, "@@DX", txtCHN.Text.Trim());
FindAndReplace(wordApp, "@@GS", txtCorp.Text.Trim());
FindAndReplace(wordApp, "@@ZS", txtDJ.Text.Trim());
wordDoc.Save();
wordDoc.Close();
wordApp.Quit();The case conversion code is omitted , Anyway, the Internet is full of . The amount in figures plus the third percentile is also very simple ,ToString("#,##0.00") that will do . The software interface is also omitted , In short, it is a pile of Text Control to enter the content corresponding to the placeholder .
Open the program modified after running Word The effect is as follows :

Now you can calm down a little bit , But still hope OA It can be built earlier .
边栏推荐
猜你喜欢

CompletionService使用及原理(源码分析)

Dynamic routing job

Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold

Master slave synchronization of MySQL database to realize read-write separation

C language final experiment report (student achievement management system) source code

IO streams (common streams)

1.7 - CPU的性能指标

Common NPM install errors

图像处理7-图像增强

CompletableFuture从了解到精通,你想知道的这里都有
随机推荐
图像处理7-图像增强
文件操作IO-Part1
Software tools_ Shortcut_ Operation summary
CompletionService使用及原理(源码分析)
Variable parameters of go
PHP knowledge points
Suggestion: use tools:overrideLibrary
Installing googleplay environment on Huawei mobile phones
Use and principle of completionservice (source code analysis)
1.3 - 码制
Picture.....
C语言:练习题三
Installation and initialization of MariaDB database
ini解析学习文档
RSA and AES
ES6 extended operator (...)
VIM view file code
880. decoded string at index
New project folder based on PIO plug-in in vscode -- Interpretation
ES6 array traversal and Es5 array traversal