实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; //using System.Threading.Tasks; namespace WindowsFormsApplication1 { /// /// RTF文档书写器 /// /// /// 本书写器对生成RTF文档提供了基础的支持 /// 编制 袁永福 http://www.xdesigner.cn /// public class testRTF { #region 测试代码 ****************************************************** [System.STAThread] public static void MainEXE() { TestWriteFile(); //TestClipboard(); } /// /// 测试生成RTF文件 /// 执行这个函数后可以使用 MS Word 打开文件 c:\a.rtf /// internal static void TestWriteFile() { try { testRTF w = new testRTF("C:\\a01.rtf"); testRTF w1 = new testRTF("C:\\a02.rtf"); if (w != null) { TestBuildRTF(w); w.Close(); } if (w1 != null) { TestBuildRTF(w1); w1.Close(); } } //System.Windows.Forms.MessageBox.Show("好了,你可以打开文件 c:\\a.rtf 了."); catch (Exception e) { // Console.WriteLine("An error occurred: '{0}'", e); } } /// /// 测试生成RTF文档并设置到系统剪切板中 /// 执行这个函数后就可以在 MS Word中使用粘贴操作来显示程序生成的文档了 /// internal static void TestClipboard() { System.IO.StringWriter myStr = new System.IO.StringWriter(); testRTF w = new testRTF(myStr); TestBuildRTF(w); w.Close(); System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject(); data.SetData(System.Windows.Forms.DataFormats.Rtf, myStr.ToString()); System.Windows.Forms.Clipboard.SetDataObject(data, true); // System.Windows.Forms.MessageBox.Show("好了,你可以在MS Word 中粘贴文本了."); } /// /// 测试生成RTF文档 /// /// RTF文档书写器 private static void TestBuildRTF(testRTF w) { try { w.Encoding = System.Text.Encoding.GetEncoding(936); // 输出文件头 w.WriteStartGroup(); w.WriteKeyword("rtf1"); w.WriteKeyword("ansi"); w.WriteKeyword("ansicpg" w.Encoding.CodePage); // 输出字体表 w.WriteStartGroup(); w.WriteKeyword("fonttbl"); w.WriteStartGroup(); w.WriteKeyword("f0"); w.WriteText("隶书;"); w.WriteEndGroup(); w.WriteStartGroup(); w.WriteKeyword("f1"); w.WriteText("宋体;"); w.WriteEndGroup(); w.WriteEndGroup(); // 输出颜色表 w.WriteStartGroup(); w.WriteKeyword("colortbl"); w.WriteText(";"); w.WriteKeyword("red0"); w.WriteKeyword("green255"); w.WriteKeyword("blue0"); w.WriteText(";"); w.WriteEndGroup(); // 输出正文 double a1, a2, a3, a4; Random rnd = new Random(); a1 = 1000.0 rnd.Next(0, 100) / 100.0; a2 = 1000.0 rnd.Next(0, 100) / 100.0; a3 = 100.0 rnd.Next(0, 100) / 100.0; a4 = 50.0 rnd.Next(0, 100) / 100.0; StreamReader sr = File.OpenText(@"C:\test.txt"); string nextLine; int lineCount = 0; double[] dataRead = new double[4]; while ((nextLine = sr.ReadLine()) != null) { dataRead[lineCount] = Convert.ToDouble(nextLine); lineCount ; } sr.Close(); //w.WriteKeyword("par"); // 开始新的段落 w.WriteKeyword("pard"); // 清除居中对齐 w.WriteKeyword("f1"); // 设置字体 w.WriteKeyword("fs34"); // 字体大小 w.WriteKeyword("cf1"); // 设置颜色 w.WriteText("系统总出力"); w.WriteText(dataRead[1].ToString() "MW"); w.WriteText("系统总负荷"); w.WriteText(dataRead[2].ToString() "MW"); w.WriteKeyword("par"); // 开始新的段落 w.WriteKeyword("pard"); // 清除居中对齐 w.WriteKeyword("f1"); // 设置字体 w.WriteKeyword("fs34"); // 字体大小 w.WriteKeyword("cf1"); // 设置颜色 w.WriteText("风电总出力"); w.WriteText(dataRead[0].ToString() "MW"); w.WriteText(" 系统频率 "); w.WriteText(dataRead[3].ToString() "Hz"); // 结束输出 w.WriteEndGroup(); } catch (Exception e) { // Console.WriteLine("An error occurred: '{0}'", e); } } #endregion /// /// 初始化对象 /// /// 文本书写器 public testRTF(System.IO.TextWriter w) { myWriter = w; } /// /// 初始化对象 /// /// 文件名 public testRTF(string strFileName) { try { if(myWriter != null) myWriter.Close(); myWriter = new System.IO.StreamWriter( strFileName, false, System.Text.Encoding.ASCII); } catch { if (myWriter != null) myWriter.Close(); } } private System.Text.Encoding myEncoding = System.Text.Encoding.GetEncoding(936); /// /// 字符编码格式 /// public System.Text.Encoding Encoding { get { return myEncoding; } set { myEncoding = value; } } /// /// 内置的文本书写器 /// private System.IO.TextWriter myWriter = null; private bool bolIndent = false; /// /// 是否使用缩进 /// /// /// RTF文档内部不能随便缩进,提供此选项只是用于生成便于阅读的RTF文档,便于程序的调试, /// 在开发调试中可以设置该属性为true,方便开发者能直接查看生成的RTF文档,但在生成最终运行的 /// 程序时应当设置该属性为 false . /// public bool Indent { get { return bolIndent; } set { bolIndent = value; } } private string strIndentString = " "; /// /// 缩进字符串 /// public string IndentString { get { return strIndentString; } set { strIndentString = value; } } /// /// 当前缩进层次 /// private int intGroupLevel = 0; /// /// 关闭对象 /// public void Close() { if (this.intGroupLevel > 0) throw new System.Exception("还有组未写完"); if (myWriter != null) { myWriter.Close(); myWriter = null; } } /// /// 输出一个组 /// /// 关键字 public void WriteGroup(string KeyWord) { this.WriteStartGroup(); this.WriteKeyword(KeyWord); this.WriteEndGroup(); } /// /// 开始输出组 /// public void WriteStartGroup() { if (bolIndent) { InnerWriteNewLine(); myWriter.Write("{"); } else myWriter.Write("{"); intGroupLevel ; } /// /// 结束输出组 /// public void WriteEndGroup() { intGroupLevel--; if (intGroupLevel < 0) throw new System.Exception("组不匹配"); if (bolIndent) { InnerWriteNewLine(); InnerWrite("}"); } else InnerWrite("}"); } /// /// 输出原始文本 /// /// 文本值 public void WriteRaw(string txt) { if (txt != null && txt.Length > 0) { InnerWrite(txt); } } /// /// 输出关键字 /// /// 关键字值 public void WriteKeyword(string Keyword) { WriteKeyword(Keyword, false); } /// /// 输出关键字 /// /// 关键字值 /// 是否是扩展关键字 public void WriteKeyword(string Keyword, bool Ext) { if (Keyword == null || Keyword.Length == 0) throw new System.ArgumentNullException("值不得为空"); if (bolIndent == false && (Keyword == "par" || Keyword == "pard")) { // par 或 pard 前可以输出空白行,不影响RTF文档显示 InnerWrite(System.Environment.NewLine); } if (this.bolIndent) { if (Keyword == "par" || Keyword == "pard") { this.InnerWriteNewLine(); } } if (Ext) InnerWrite("\\*\\"); else InnerWrite("\\"); InnerWrite(Keyword); } /// /// 内容文本编码格式 /// private System.Text.Encoding Unicode = System.Text.Encoding.Unicode; /// /// 输出纯文本 /// /// 文本值 public void WriteText(string Text) { if (Text == null || Text.Length == 0) return; InnerWrite(' '); for (int iCount = 0; iCount < Text.Length; iCount ) { char c = Text[iCount]; if (c == '\t') { this.WriteKeyword("tab"); InnerWrite(' '); } else if (c < 256) { if (c > 32 && c < 127) { // 出现特殊字符,需要斜线转义 if (c == '\\' || c == '{' || c == '}') InnerWrite('\\'); InnerWrite(c); } else { InnerWrite("\\\'"); WriteByte((byte)c); } } else { byte[] bs = myEncoding.GetBytes(c.ToString()); for (int iCount2 = 0; iCount2 < bs.Length; iCount2 ) { InnerWrite("\\\'"); WriteByte(bs[iCount2]); } } }//for( int iCount = 0 ; iCount < Text.Length ; iCount ) } /// /// 当前位置 /// private int intPosition = 0; /// /// 当前行的位置 /// private int intLineHead = 0; /// /// 16进制字符组 /// private const string Hexs = "0123456789abcdef"; /// /// 输出字节数组 /// /// 字节数组 public void WriteBytes(byte[] bs) { if (bs == null || bs.Length == 0) return; WriteRaw(" "); for (int iCount = 0; iCount < bs.Length; iCount ) { if ((iCount % 32) == 0) { this.WriteRaw(System.Environment.NewLine); this.WriteIndent(); } else if ((iCount % 8) == 0) { this.WriteRaw(" "); } byte b = bs[iCount]; int h = (b & 0xf0) >> 4; int l = b & 0xf; myWriter.Write(Hexs[h]); myWriter.Write(Hexs[l]); intPosition = 2; } } /// /// 输出一个字节数据 /// /// 字节数据 public void WriteByte(byte b) { int h = (b & 0xf0) >> 4; int l = b & 0xf; myWriter.Write(Hexs[h]); myWriter.Write(Hexs[l]); intPosition = 2; //FixIndent(); } #region 内部成员 ****************************************************** private void InnerWrite(char c) { intPosition ; myWriter.Write(c); } private void InnerWrite(string txt) { intPosition = txt.Length; myWriter.Write(txt); } private void FixIndent() { if (this.bolIndent) { if (intPosition - intLineHead > 100) InnerWriteNewLine(); } } private void InnerWriteNewLine() { if (this.bolIndent) { if (intPosition > 0) { InnerWrite(System.Environment.NewLine); intLineHead = intPosition; WriteIndent(); } } } private void WriteIndent() { if (bolIndent) { for (int iCount = 0; iCount < intGroupLevel; iCount ) { InnerWrite(this.strIndentString); } } } #endregion /// /// 销毁对象 /// public void Dispose() { this.Close(); } } }
标签: LED
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论