学生做word题时,需要让他们将word中的指定段落进行分栏,下面的代码就是检测他们做的是否正确的代码。

public word.Document 打开Word文档(string filename, bool kejian) { app = new word.ApplicationClass(); app.Visible = kejian; object thefilename = filename; word.Document doc = app.Documents.Open(ref thefilename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); return doc; } public void 关闭Word文档(word.Document thedoc) { try { thedoc.Close(ref miss, ref miss, ref miss); app.Quit(); Marshal.ReleaseComObject(app); app = null; } catch { //杀死进程("winword"); } } public bool 分栏(word.Document thedoc, string 起始段, string 结束段, string 分栏数) { try { int qishiduan = Int32.Parse(起始段) + 1; int jieshuduan = Int32.Parse(结束段) + 1; for (int i = qishiduan; i <= jieshuduan; i++) { if (thedoc.Paragraphs[i].Range.PageSetup.TextColumns.Count != Int32.Parse(分栏数)) { return false; } } return true; } catch { return false; } }

使用方法:

word.Document w = 打开Word文档("a.docx",false);

w.分栏("1","3",3)

关闭Word文档(w);

就是检查第1到3自然段的分栏数是不是3栏。