说明(2017-12-22 11:20:44):
1. 因为数学脚本的主标题和副标题没有格式,目录导航里不显示,修改的时候不好定位,所以需要改成下图格式:
2. 问题的难点有两个,一个是word的操作,c#操作word本来应该是顺理成章、水到渠成的,不过实际上并没有很好的参考资料,最权威的MSDN虽然很详细,但是内容太多、查找起来太费劲https://msdn.microsoft.com/vba/vba-word。我查到的一个比较精简的word操作,https://www.cnblogs.com/shi2172843/p/5848116.html,应付一般的问题足够了。但是还有很多不常用的设置,比如我这个设置标题,网上根本就查不到了,研究了半天,得出一个终极结论:使用VBA录制宏,然后去VS里通过智能提示,找到C#里对应的方法,基本跟VBA里的名字是一样的,比如:
VBA里的设置标题,和左对齐:
C#里的设置标题,和左对齐:
3. 问题的第二个难点是,C#对文件的操作,因为设计目标是把文件夹里所有的word文件都获取到,但是Directory.GetDirectories和Directory.GetFiles只能获取一层文件夹里的子文件夹和文件,如果里面还有子文件夹的话就拿不到了。这个问题拖了好几天,昨天晚上灵光一现给解决了。似乎是用了递归,因为一直觉得递归挺难的,之前查的资料也是用的递归,但是我看不太懂,http://blog.****.net/kwy15732621629/article/details/72085701,我对那种参数很多、封装很好的方法有点抵触,就决定自己试着写了一个,我记得之前好像是蒋坤说的,递归有两个特点,一个是调用自身,一个是要有返回值,特别注意不要出现无限递归。我觉得自己写的这个方法这两点都做到了,反正写完测试了一下,可以正常获取所有子目录文件。代码如下,应该是很通俗易懂了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace _00_Test
{ class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\JJW\Desktop\新建文件夹";
List<string> list = GetFile(path);
foreach (string l in list)
{
Console.WriteLine(l);
}
Console.ReadKey();
}
private static List<string> GetFile(string path)
{
//传入一个路径
List<string> list = new List<string>();
//如果路径是文件夹,继续遍历,把新遍历出来的文件夹和文件存到list
if (Directory.Exists(path))
{
string[] dirs = Directory.GetDirectories(path);
if (dirs != null)
{
foreach (string dir in dirs)
{
list.AddRange(GetFile(dir));
}
} string[] files = Directory.GetFiles(path);
if (files != null)
{
list.AddRange(files);
}
}
//如果路径是文件,添加到list
else if (File.Exists(path))
{
list.Add(path);
}
return list;
}
}
}
4. 剩下的是完整代码,为了自己以后方便抄的,不放winform的了,反正就拖一下完事:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using msWord = Microsoft.Office.Interop.Word; namespace CheckLabel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnOK_Click(object sender, EventArgs e)
{
//实例化wordapp类
msWord.Application app = new msWord.Application();
//操作时显示word,测试时使用,实际使用时隐藏就可以了
app.Visible = false;
//判断拖入的文件夹是否存在
if (Directory.Exists(txtBox.Text))
{
//获取路径下所有doc和docx文件,如果拖入的是文件夹,就需要继续遍历文件夹,这里有两种方式,一种是递归遍历,另一种根据文件夹结构读取每个文件夹,反正每个版本就一层文件夹。
//string[] paths = Directory.GetFiles(Path.GetDirectoryName(txtBox.Text), "*.doc*");
List<string> files = GetFile(txtBox.Text);
//两个泛型,A是标题1,B是副标题
List<string> labelA = new List<string> { "课前预习导学", "课后复习巩固", "自我测试及解答", "能力提升" };
List<string> labelB = new List<string> { "定义及说明", "简单例题", "(三)练习", "课堂回顾", "经典例题及解答点拨", "写作业", "中考知识点", "中考真题及解答", "拓展提高及解答" }; //遍历所有路径
foreach (string file in files)
{
//判断doc文件是否存在
if (File.Exists(file))
{
Console.WriteLine(file);
//实现wordapp.document接口,打开word
msWord.Document doc = app.Documents.Open(file);
//获取word全文为字符串,没用上
//string temp = doc.Content.Text.Trim();
//遍历所有段落,注意要从1开始,paragraph从1开始而不是从0!!
//for (int i = 1; i <= doc.Paragraphs.Count; i++)
//{
// Console.WriteLine(doc.Paragraphs[i].Range.Text);
//}
///*
//用foreach循环也可以
foreach (msWord.Paragraph para in doc.Paragraphs)
{
foreach (string lA in labelA)
{
//如果段落里包含标题1的文字
if (para.Range.Text.Contains(lA))
{
//测试一下改颜色
//para.Range.Font.ColorIndex = msWord.WdColorIndex.wdBlue;
para.Range.set_Style("标题 1");
Console.WriteLine("已处理:" + para.Range.Text);
}
else
{
//Console.WriteLine(para.Range.Text + "不包含" + lA);
}
}
foreach (string lB in labelB)
{
//如果段落里包含标题1的文字
if (para.Range.Text.Contains(lB))
{
para.Range.set_Style("副标题");
para.Range.ParagraphFormat.Alignment = msWord.WdParagraphAlignment.wdAlignParagraphLeft;
Console.WriteLine("已处理:" + para.Range.Text);
}
else
{
//Console.WriteLine(para.Range.Text + "不包含" + lB);
}
}
}
//*/
doc.Save();
doc.Close(true);
}
else
{
MessageBox.Show(file + " ,word文件不存在!");
}
}
} else
{
MessageBox.Show(txtBox.Text + "文件夹不存在!");
}
app.Quit();
MessageBox.Show("修改完毕!");
} private List<string> GetFile(string path)
{
//获取所有目录包括子目录的文件
//传入一个路径
List<string> list = new List<string>();
//如果路径是文件夹,继续遍历,把新遍历出来的文件夹和文件存到list,用了递归
if (Directory.Exists(path))
{
string[] dirs = Directory.GetDirectories(path);
if (dirs != null)
{
foreach (string dir in dirs)
{
list.AddRange(GetFile(dir));
}
} string[] files = Directory.GetFiles(path);
if (files != null)
{
list.AddRange(files);
}
}
//如果路径是文件,添加到list
else if (File.Exists(path))
{
list.Add(path);
}
return list;
} private void btnSelect_DragEnter(object sender, DragEventArgs e)
{
//拖进文件夹,获取信息
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
} private void btnSelect_DragDrop(object sender, DragEventArgs e)
{
//拖动松手后,获得路径
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue().ToString(); //获得路径
txtBox.Text = path;
} private void btnSelect_Click(object sender, EventArgs e)
{
//选择文件夹
//FolderBrowserDialog fbd = new FolderBrowserDialog();
//fbd.SelectedPath = @"c:\";
//if (fbd.ShowDialog() == DialogResult.OK)
//{
// txtBox.Text = fbd.SelectedPath;
//}
//选择文件
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
//C:\Users\JJW\Desktop\新建文件夹\1_负数的引入.doc
txtBox.Text = ofd.FileName;
}
} }
}
设计器:
结束。