在C#获取word文档的字数,并输出到excel
2006-01-23 17:28
465 查看
参考文档
http://www.microsoft.com/china/msdn/library/office/office/UndstaExcelObjModNETDev.mspx注册了一段时间啦,一直都没有时间写点东西,实在是不好意思啊。
由于最近老婆大人需要统计大量word文档的字数,俺就抽时间写了
一个统计多个word文档字数的小程序。
读取word文件的字数(或字符数),首选需要引用word对象和excel对象。
//打开多个word文件,获取字数
Word.ApplicationClass ThisApplication = new Word.ApplicationClass();
object missingValue = Type.Missing;
object myTrue = true;
int count;
object fileName;
foreach(ListViewItem item in listView1.Items)//多个word文档列表
{
count =0;
fileName = item.SubItems[0].Text;
Word.Document ThisDocument = ThisApplication.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
Word.Range rng = ThisDocument.Content;
rng.Select();
count = ThisDocument.Characters.Count;
}
ThisDocument.Close(ref missingValue, ref missingValue, ref missingValue);
//保存到excel文件
void SaveToExcel()
{
System.Windows.Forms.SaveFileDialog file = new SaveFileDialog();
file.DefaultExt = ".doc";
file.Filter ="Excle文件(*.xls)|*.xls|所有文件(*.*)|*.*";
file.Title = "请选择保存文件位置";
if(file.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
Excel.ApplicationClass ThisApplication = new Excel.ApplicationClass();
Excel.Workbook workbook = ThisApplication.Workbooks.Add(Type.Missing);
Excel.Worksheet newWorkSheet;
newWorkSheet =
(Excel.Worksheet)workbook.Worksheets.Add(
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
int i =1;
foreach(ListViewItem item in listView1.Items)
{
Excel.Range range = (Excel.Range)newWorkSheet.Cells[i,1];
range.Value2 = item.SubItems[0].Text;
range = (Excel.Range)newWorkSheet.Cells[i,2];
range.Value2 = item.SubItems[1].Text;
i++;
}
foreach (Excel.Workbook book in ThisApplication.Workbooks)
{
book.SaveCopyAs(file.FileName);
book.Close(false, Type.Missing, Type.Missing);
}
}
}
相关文章推荐
- 在C#获取word文档的字数,并输出到excel
- c#获取txt,word,excel文档内容方法
- C# .Net输出word和excel文件方法和函数
- C#获取Word文档页数,并跳转到指定的页面获取页面信息
- Asp.Net页面输出到WORD、EXCEL、TXT、HTM等类型的文档
- C# 快速获取Word文档结构
- C#获取Word文档中所有表格的实现代码分享
- 将网页内容转换成word或excel文档的方法(c#.net)
- 用C#转换Word或Excel文档为Html文件
- Asp.Net页面输出到WORD、EXCEL、TXT、HTM等类型的文档
- ASP输出文件为word及excel文档形式
- C#获取Word文档页数,并跳转到指定的页面获取内容,且插入分页符
- C#在线预览文档(word,excel,pdf,txt,png)
- JS实现获取word文档内容并输出显示到html页面示例
- C#将文档(Word\ Excel\ PowerPoint\ Visio\ text\ XML\ RTF\ CSV )转成Pdf
- 将网页内容转换成word或excel文档的方法(c#.net)
- 将网页内容转换成word或excel文档的方法(c#.net)
- 获取数据库中表的字段名和字段类型,并输出到word文档中
- C#获取Word文档结构图 并且转成Datatable表格
- ASP输出文件为word及excel文档