当前位置:网站首页>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 .
边栏推荐
- Installation and initialization of MariaDB database
- Simple example of class template
- A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
- 反編譯正常回編譯出現問題自己解决辦法
- 880. decoded string at index
- Altium designer creates multiple class networks to realize different lineweights of multiple networks
- 深度学习---三好学生各成绩所占权重问题(3)
- Vscode configuration proxy
- 一个完整的性能测试流程
- IO stream (file class introduction)
猜你喜欢

Dynamic routing job
A complete performance test process

Traitement d'images 7 - amélioration d'images

Base64 explanation: playing with pictures Base64 encoding

Use and principle of completionservice (source code analysis)

Unable to access the Internet at win10 /11 hotspot

Centos8 install redis

1.8 - 多级存储

银河麒麟初体验

Master slave synchronization of MySQL database to realize read-write separation
随机推荐
DHCP operation
Spin official tutorial
文件操作IO-Part1
Installation and initialization of MariaDB database
Pycharm shortcut key
Several commands not commonly used in MySQL
[wechat applet: single or multiple styles, background color, rounded corners]
Record a problem tracking of excessive load
DOM (document object model) document XML file object model
RSA and AES
[database] transaction
Learn fpga---ram IP core and key parameters from the bottom structure
New project folder based on PIO plug-in in vscode -- Interpretation
Multithreading advanced level
华泰炒股安全吗?我想网上开户。
Set in set (III)
Application of redis client list in practice
Basic use of markdown
DXP copper laying settings
Arrangement of in-depth learning materials