当前位置:网站首页>不忘初心,能偷懒就偷懒:C#操作Word文件
不忘初心,能偷懒就偷懒:C#操作Word文件
2022-06-30 06:18:00 【Ki1381】
换了一家公司,新单位的信息化建设仍然在路上。OA也正在建设中,目前项目发票报销等流程仍然是手工的,只能线下填单子。至于是电脑上填好打印出来还是直接空白模板打出来手写倒是不管,随意。疫情期间积压了太多项目,所以复工后填单子很快就让我暴躁了。尤其是大小写金额,还有三分位的那种小写金额的格式,浪费了太多时间,几乎就没有一次成功过的,简直不能忍。。。所以最后默默的打开了devenv。干嘛不写个程序呢,多少能省点力气。

不过从头按这个模板的格式一行行代码生成Word文件看样子工作量有点大,本着能偷懒就偷懒的宗旨,想到了一个办法:空白处用先占位符代替,到程序里做替换。具体说来,就是用一些特殊的字符串先把位置占好,连带着格式也调好,到程序里用真实有意义的内容去替换这些特殊字符串,就像下图:

所以需求就转换成了如何用C#实现Word的字符串替换。显然有这个需求的人应该也不少,网上很快就找到了相关代码,经测试靠谱。操作Word文档替换文字的核心代码为(需要在项目中引入word的com):
private void FindAndReplace(Microsoft.Office.Interop.Word.Application WordApp, object findText, object replaceWithText)
{
object matchCase = true; // 重点关注!!
object matchWholeWord = false; // 重点关注!!
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);
}调用的核心代码为:
// 打开文档
MSWord.Application wordApp = new MSWord.Application();
MSWord.Document wordDoc = wordApp.Documents.Open(带占位符的模板文件名);
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();大小写转换的代码略了,反正网上到处都是。小写金额加三分位也很简单,ToString("#,##0.00")即可。软件界面也略了,总之就是一堆Text控件用以输入与占位符对应的内容。
运行后打开程序修改过的Word效果如下:

这下可以稍微平复一下暴躁的心情了,不过还是希望OA能早点建好。
边栏推荐
- 从底层结构开始学习FPGA----RAM IP核及关键参数介绍
- Notes of the first week of 2021 Chengdu Arts and Sciences cloud computing intensive training class
- 1.8 - 多级存储
- List in set (2)
- Learn fpga---ram IP core and key parameters from the bottom structure
- Picture.....
- Go pack and unpack
- Unable to read file for extraction: gdx64. dll
- Traitement d'images 7 - amélioration d'images
- Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程
猜你喜欢

My experience in functional testing for so many years

Picture.....

C语言:练习题三

Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程

Wechat applet mall project

DHCP operation

01. regular expression overview

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

Usage of case, casez and casex statements in Verilog

1.8 - 多级存储
随机推荐
There is a group of students' score {99, 85, 82, 63, 60}. To add a student's score, insert it into the score sequence and keep the descending order
Decompilation normal decompilation problems. Solve them yourself
Set in set (III)
46. full arrangement -dfs double hundred code
Hao looking for a job
Dynamic routing job
2020-10-06
ES6 array traversal and Es5 array traversal
Résoudre le problème de décompiler la compilation normale
Develop stylelint rules from zero (plug-ins)
Master slave synchronization of MySQL database to realize read-write separation
Unable to read file for extraction: gdx64. dll
一个完整的性能测试流程
Several commands not commonly used in MySQL
ROS multi machine
反編譯正常回編譯出現問題自己解决辦法
Detailed description of methods in the interface
Prototype and prototype chain in JS
Share problems solved
Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程