




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#统计文件字数工具目标:写这个小工具是因为两天前老婆的一句话“看电子书太不方便,全书的字数又没统计,还要用Word来统计字数!”;在选择开发语言上可以有Delphi7、JavaSE、C#、VB、以及Android(因为最近在弄Android模拟器下的程序),后来用了VS2010下C#;完全原创谈不上,因为Google了一些问题,但是如果你是想试试这个工具-统计出来的字数似乎还准、看看C#的源码-这份源码结构简单注释清晰、或是还想进一步做点扩展-技术无优劣功能无止境,那么请你绝对不必怀疑。解决了如下几个问题(可能其中的某个正是你想要解决的):能读取的文件包括文本文件(*.txt;文本,另外还有*.csv,*.ini)、Word文件(*.doc,*.docx,二进制),尤其是文本文件,提供了常见几种读取方式;能对应多种文本文件的编码格式;写了一个简单的EncodingType类来专门作处理;C#实现文件字数统计工具,可统计文本文件和Word文档中的内容工程项目文件结构:主要文件源码清单: 程序的入口文件,由VS2010建立WinForm项目时自动生成,这个基本不用改了(除非程序有特殊的要求)。using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace TotalFileWords static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm(); 主界面逻辑处理代码文件,打开文件、统计字数等。using System;using System.Windows.Forms;using System.IO;using Microsoft.Office.Interop.Word;namespace TotalFileWords public partial class MainForm : Form public MainForm() InitializeComponent(); / 打开(&O) private void btnOpen_Click(object sender, EventArgs e) try OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = 文本文件(*.txt)|*.txt|Doc文件(*.doc,*.docx)|*.doc;*.docx|所有文件(*.*)|*.*; dialog.FilterIndex = 1; dialog.Title = 选择需统计字数的文件; string fn; if (dialog.ShowDialog() = DialogResult.OK) fn = dialog.FileName; this.txtFile.Text = fn; this.lblWords.Visible = false; catch (Exception ex) MessageBox.Show(ex.Message); / 统计(&T) private void btnTotal_Click(object sender, EventArgs e) try / cnt1字数,cnt2字节数,cnt3汉字数,cnt4行数 int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; / 判断文件是否存在 string fn = this.txtFile.Text.Trim(); if (fn.Equals() MessageBox.Show(请选择文件。); this.txtFile.Focus(); return; if (!File.Exists(fn) MessageBox.Show(文件不存在。); this.txtFile.Focus(); this.txtFile.SelectAll(); return; / 忽略空格标记(全角空格、半角空格、Tab键) bool skipSpace = true; if (!this.chkSpace.Checked) skipSpace = false; / 判断文件类型 string ext = fn.Substring(fn.LastIndexOf(.) + 1); if (ext.ToLower().Equals(txt) FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, EncodingType.GetType(fn); /sr.BaseStream.Seek(0, SeekOrigin.Begin); string sLine; while (sLine = sr.ReadLine() != null) / 忽略空格 if (skipSpace) sLine = sLine.Replace(?, ).Replace( , ).Replace(t, ); cnt1 += getWordLength(sLine); / 字数 cnt2 += getByteLength(sLine); / 字节数 cnt4+; / 行数 sr.Close(); fs.Close(); else if (ext.ToLower().Equals(doc) | ext.ToLower().Equals(docx) string txt = readDoc(fn); / 行数 char ch = txt.ToCharArray(); for (int i = 0; i ch.Length; i+) if (int)chi = 13) / 回车键 cnt4+; if (txt != null) txt = txt.Replace(n, ).Replace(r, ); / 忽略空格 if (skipSpace) txt = txt.Replace(?, ).Replace( , ).Replace(t, ); cnt1 += getWordLength(txt); / 字数 cnt2 += getByteLength(txt); / 字节数 else MessageBox.Show(暂时只支持文本文件(*.txt)和Doc文件(*.doc,*.docx),请重选。); this.txtFile.Focus(); this.txtFile.SelectAll(); return; cnt3 = cnt2 - cnt1; this.lblWords.Text = 经统计,此文件内容有 + cnt4 + 行,共 + cnt1 + 字( + cnt3 + 汉字)。; this.lblWords.Visible = true; catch (Exception ex) MessageBox.Show(ex.Message); / 退出(&X) private void btnExit_Click(object sender, EventArgs e) this.Close(); System.Windows.Forms.Application.Exit(); / / 读取Doc文档(*.doc或*.docx)中文本内容 / / / private string readDoc(string fn) try object readOnly = true; object missing = System.Reflection.Missing.Value; object fileName = fn; / 初始化word程序 /ApplicationClass wordapp = new ApplicationClass(); Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application(); / 打开指定的doc文件 Document doc = wordapp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); / 读取指定的doc文件的文本 string txt = doc.Content.Text; / 关闭doc文件 doc.Close(ref missing, ref missing, ref missing); / 退出word程序 wordapp.Quit(ref missing, ref missing, ref missing); return txt; catch (Exception ex) throw ex; / / 返回字数 / / / private int getWordLength(string s) if (s != null) return s.Length; else return 0; / / 返回字节数 / / / private int getByteLength(string s) int l = 0; char q = s.ToCharArray(); for (int i = 0; i = 0x4E00 & (int)qi = 0x9FA5) / 汉字 l += 2; else l += 1; return l; 文件编码格式对应处理类。using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace TotalFileWords / / 获取文件的编码格式 / class EncodingType / / 给定文件的路径,读取文件的二进制数据,判断文件的编码类型 / / 文件路径 / 文件的编码类型 public static System.Text.Encoding GetType(string FILE_NAME) FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); Encoding r = GetType(fs); fs.Close(); return r; / / 通过给定的文件流,判断文件的编码类型 / / 文件流 / 文件的编码类型 public static System.Text.Encoding GetType(FileStream fs) byte Unicode = new byte 0xFF, 0xFE, 0x41 ; byte UnicodeBIG = new byte 0xFE, 0xFF, 0x00 ; byte UTF8 = new byte 0xEF, 0xBB, 0xBF ; / 带BOM Encoding reVal = Encoding.Default; BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default); int i; int.TryParse(fs.Length.ToString(), out i); byte ss = r.ReadBytes(i); if (IsUTF8Bytes(ss) | (ss0 = 0xEF & ss1 = 0xBB & ss2 = 0xBF) reVal = Encoding.UTF8; else if (ss0 = 0xFE & ss1 = 0xFF & ss2 = 0x00) reVal = Encoding.BigEndianUnicode; else if (ss0 = 0xFF & ss1 = 0xFE & ss2 = 0x41) reVal = Encoding.Unicode; r.Close(); return reVal; / / 判断是否是不带BOM的UTF8格式 / / / private static bool IsUTF8Bytes(byte data) int charByteCounter = 1; / 计算当前分析的字符应还有的字节数 byte curByte; / 当前分析的字节 for
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 活动策划专员工作总结
- 气管切开的患者如何护理
- 护理人的初心和使命
- 酒店客房年终总结报告
- 根尖周炎护理查房
- (2024版)苏教版三年级上册数学3.1数据的收集(1)课件
- 零售药店工作总结
- 音乐老师工作汇报
- 护理培训质量检查记录
- 事业单位安全培训课程课件
- 大学门户网站及站群管理系统规划与建设指南
- 中学生青春期恋爱教育主题班会
- 叙事护理案例汇报
- 2025年广东省中考地理试卷(含2025年答案及考点分析)
- 债务加入还款协议书
- 《纯电动汽车构造与检修》课件-任务2 比亚迪E5电机驱动系统构造与检修
- 2024年企业所得税年度纳税申报表(A类2017 年版2025年01月修订)-(2025 0323)
- 派单业务合同模版模板
- 2025年体育与健康初中学业水平考试体育综合知识考试题库(附答案)
- 2024装配式碳纤维增强免拆底模钢筋桁架楼承板建筑构造
- 文化传媒公司抖音代运营合同
评论
0/150
提交评论