实例介绍
【实例简介】C#提取目录中的Word文档,有提取、统计页数功能
【实例截图】

【核心代码】
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.Reflection;
using System.IO;
using System.Threading;
namespace DistillWordList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread td;
private void GetWordList(string WordPath)
{
try
{
Word.Document doc = null;
Word.Application app = new Word.Application();
Object none = System.Reflection.Missing.Value;
Word.Document doc2 = app.Documents.Add(ref none, ref none, ref none, ref none);
object Unknown = Type.Missing;
object missing = System.Reflection.Missing.Value;
object filename = WordPath;
object readOnly = false;
object isVisble = true;
object index = 0;
doc = app.Documents.Open(ref filename, ref missing, ref readOnly,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown);
app.Visible = false;
object start = 0;
object end = 0;
Word.Range rg = doc.Range(ref start, ref end);
object oUseHeadingStyles = true;
object oUpperHeadingLevel = 1;
object oLowerHeadingLevel = 9;
object oUseFields = false;
object oTableID = 1;
object oRightAlignPageNumbers = false;
object oIncludePageNumbers = false;
object oAddedStyles = null;
object oUseHyperlinks = false;
object oHidePageNumbersInWeb = false;
doc.TablesOfContents.Add(rg, ref oUseHeadingStyles,
ref oUpperHeadingLevel, ref oLowerHeadingLevel,
ref oUseFields, ref oTableID, ref oRightAlignPageNumbers,
ref oIncludePageNumbers, ref oAddedStyles, ref oUseHyperlinks, ref oHidePageNumbersInWeb);
if (doc.Fields.Count >= 1)
{
doc.Fields.Item(1).Cut();
doc2.Range(ref start, ref end).Paste();
}
doc.Close(ref Unknown, ref Unknown, ref Unknown);
app.Quit(ref Unknown, ref Unknown, ref Unknown);
}
catch(Exception ex)
{
MessageBox.Show("警告:" ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
string[] filePath;
FileInfo fi;
string[] fileinfo=new string[4];
private void GetFileInfos(string path)
{
listView1.Items.Clear();
filePath=Directory.GetFiles(path,"*.doc");
for (int i = 0; i < filePath.Length; i )
{
string fpath = filePath[i].ToString();
string name = fpath.Substring(fpath.LastIndexOf("\\") 1, fpath.Length - fpath.LastIndexOf("\\") -1);
fi=new FileInfo(fpath);
string fsize = Convert.ToString(fi.Length / 1024) "KB";
fileinfo[0] = name;
fileinfo[1] = fpath;
fileinfo[2] = fsize;
if (fsize != "0KB")
{
ListViewItem lvi = new ListViewItem(fileinfo);
listView1.Items.Add(lvi);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtPath.Text = folderBrowserDialog1.SelectedPath;
GetFileInfos(txtPath.Text.Trim());
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count <= 0)
{
MessageBox.Show("警告:请选择要提取目录的文档!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string path = listView1.SelectedItems[0].SubItems[1].Text;
GetWordList(path);
}
}
private void button3_Click(object sender, EventArgs e)
{
Word.Document doc = null;
Word.Application app = new Word.Application();
Object none = System.Reflection.Missing.Value;
Word.Document doc2 = app.Documents.Add(ref none, ref none, ref none, ref none);
object Unknown = Type.Missing;
if (listView1.Items.Count> 0)
{
for (int i = 0; i < listView1.Items.Count; i )
{
string WordPath = listView1.Items[i].SubItems[1].Text;
object missing = System.Reflection.Missing.Value;
object filename = WordPath;
object readOnly = false;
object isVisble = true;
object index = 0;
doc = app.Documents.Open(ref filename, ref missing, ref readOnly,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown);
app.Visible = false;
object start = 0;
object end = 0;
Word.Range rg = doc.Range(ref start, ref end);
object oUseHeadingStyles = true;
object oUpperHeadingLevel = 1;
object oLowerHeadingLevel = 9;
object oUseFields = false;
object oTableID = 1;
object oRightAlignPageNumbers = false;
object oIncludePageNumbers = false;
object oAddedStyles = null;
object oUseHyperlinks = false;
object oHidePageNumbersInWeb = false;
doc.TablesOfContents.Add(rg, ref oUseHeadingStyles,
ref oUpperHeadingLevel, ref oLowerHeadingLevel,
ref oUseFields, ref oTableID, ref oRightAlignPageNumbers,
ref oIncludePageNumbers, ref oAddedStyles, ref oUseHyperlinks, ref oHidePageNumbersInWeb);
if (doc.Fields.Count >= 1)
{
doc.Fields.Item(1).Cut();
doc2.Range(ref start, ref end).Paste();
}
doc.Close(ref Unknown, ref Unknown, ref Unknown);
}
app.Quit(ref Unknown, ref Unknown, ref Unknown);
}
}
private void statPagenumber()
{
Word.Document doc = null;
int mPages = 0;
Word.Application app = new Word.Application();
object Unknown = Type.Missing;
for (int i = 0; i < listView1.Items.Count; i )
{
string WordPath = listView1.Items[i].SubItems[1].Text;
object missing = System.Reflection.Missing.Value;
object filename = WordPath;
object readOnly = false;
object isVisble = true;
object index = 0;
doc = app.Documents.Open(ref filename, ref missing, ref readOnly,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown);
app.Visible = false;
mPages = (int)app.Selection.get_Information(Word.WdInformation.wdNumberOfPagesInDocument);
listView1.Items[i].SubItems[3].Text = mPages.ToString() "页";
doc.Close(ref Unknown, ref Unknown, ref Unknown);
}
app.Quit(ref Unknown, ref Unknown, ref Unknown);
}
private void button4_Click(object sender, EventArgs e)
{
td = new Thread(new ThreadStart(this.statPagenumber));
td.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (td != null)
{
td.Abort();
}
}
}
}
标签: Word
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论