在C#获取word文档的字数,并输出到excel

注册了一段时间啦,一直都没有时间写点东西,实在是不好意思啊。
由于最近老婆大人需要统计大量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);
    }
   }
}

转载于:https://www.cnblogs.com/netchina/archive/2004/09/20/44747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值