当前位置:网站首页>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 .
边栏推荐
- 旋转标注工具roLabelImg
- gazebo/set_ model_ State topic driving UAV model through posture
- Picture.....
- requests. The difference between session () sending requests and using requests to send requests directly
- Go pack and unpack
- Uniapp wechat applet returns to the previous page and refreshes
- Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required
- Decompilation normal decompilation problems. Solve them yourself
- 从底层结构开始学习FPGA----RAM IP核及关键参数介绍
- Base64详解:玩转图片Base64编码
猜你喜欢

Dynamic routing job

1.8 - 多级存储

Common NPM install errors

Learn fpga---ram IP core and key parameters from the bottom structure

Develop stylelint rules from zero (plug-ins)

Px4 control mode summary

Base64 explanation: playing with pictures Base64 encoding

Detailed description of methods in the interface

Application of redis client list in practice

Wechat applet mall project
随机推荐
File operation io-part1
Why does the verification code not refresh when clicked
Base64 explanation: playing with pictures Base64 encoding
How does Altium designer hide some temporarily unnecessary classes, such as GND
Go common judgments
Practice summary of Prometheus project in amu Laboratory
Collection and method of traversing collection elements (1)
Uniapp wechat applet returns to the previous page and refreshes
Traitement d'images 7 - amélioration d'images
Data read / write: realize data read / write function based on C # script in unity
880. 索引处的解码字符串
Common address collection
Rotate dimension tool rolabelimg
ES6解构赋值
1.4 - 定点数与浮点数
Static routing job
Develop stylelint rules from zero (plug-ins)
Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime
数据读写:Unity中基于C#脚本实现数据读写功能
Thread safe solutions, communication between threads (classic examples of producers and consumers)