• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在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);
    }
   }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#基础入门 十一发布时间:2022-07-10
下一篇:
C#文件操作(上传,下载,读取,写入)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap