C#编写Rtf文档,中文与字母区分,C#将汉字转换成GBK编码
2011-12-24 16:57 ※森林小居※ 阅读(4545) 评论(0) 编辑 收藏 举报最近两天在调试仰邦的LED双色屏。他们用的控制卡只能支持三种自定义传参方式,一、文本传值,二、Rtf文档传值,三、bmp图片传值。第一种第三种在仓库中传值无法做到。其主要原因是传的数据比较的多。对于Rtf文档的操作有很大的麻烦,主要是在于编码格式,二、颜色控制。下面主要介绍如何生成Rtf文档,可以通过WORD打开并发磅到LED屏上进行正常的显示。
研究了一天,把Rtf文档主要分成:主文档区,显示文字区。在这里我把显示文字区分成:中文与英文两种,中文建议用GBK编码,英文可以直接进行替换更改。主要整理出来三种颜色:黄,红,绿。代码如下:
黄(中文):
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf18\insrsid1732426\charrsid1732426 \loch\af31506\hich\af31506\dbch\f31505 {KeyWord}}
黄(英文):
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf18\insrsid2637325\charrsid8723676 \hich\af31506\dbch\af31505\loch\f31506 {KeyWord}}
绿(中文):
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf17\insrsid9504078 \loch\af31506\hich\af31506\dbch\f31505 {KeyWord}}
绿(英文):
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf17\insrsid2637325\charrsid13778928 \hich\af31506\dbch\af31505\loch\f31506 {KeyWord}}
红(中文):
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf6\insrsid1732426\charrsid1732426 \loch\af31506\hich\af31506\dbch\f31505 {KeyWord}}
红(英文):
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf6\insrsid7626129 \hich\af31506\dbch\af31505\loch\f31506 {KeyWord}}
换行:
{\rtlch\fcs1 \af0 \ltrch\fcs0 \cf17\insrsid8937483\charrsid13778928 \par }
以上代码中所有用{KeyWord}标识的均为需要替换成自定义的文字。把替换成功后的字符串用主模板替换写成.rtf文件既可。代码如下:
public string ContentTemplate = string.Empty, GreenTemplate = string.Empty, YellowTemplate = string.Empty, RedTemplate = string.Empty, LineTemplate = string.Empty,CGreenTemplate = string.Empty,CYellowTemplate = string.Empty,CRedTemplate = string.Empty;
StreamReader Sr1 = new StreamReader("d:\\ContentTemplate.txt");
ContentTemplate = Sr1.ReadToEnd();
StreamReader Sr2 = new StreamReader("d:\\GreenTemplate.txt");
GreenTemplate = Sr2.ReadToEnd();
StreamReader Sr3 = new StreamReader("d:\\YellowTemplate.txt");
YellowTemplate = Sr3.ReadToEnd();
StreamReader Sr4 = new StreamReader("d:\\RedTemplate.txt");
RedTemplate = Sr4.ReadToEnd();
StreamReader Sr5 = new StreamReader("d:\\LineTemplate.txt");
LineTemplate = Sr5.ReadToEnd();
StreamReader Sr6 = new StreamReader("d:\\CGreenTemplate.txt");
CGreenTemplate = Sr6.ReadToEnd();
StreamReader Sr7 = new StreamReader("d:\\CYellowTemplate.txt");
CYellowTemplate = Sr7.ReadToEnd();
StreamReader Sr8 = new StreamReader("d:\\CRedTemplate.txt");
CRedTemplate = Sr8.ReadToEnd();
Sr1.Close();
Sr2.Close();
Sr3.Close();
Sr4.Close();
Sr5.Close();
Sr6.Close();
Sr7.Close();
Sr8.Close();
string Template = GreenTemplate.Replace("{KeyWord}", "test") + CGreenTemplate.Replace("{KeyWord}", s1) + LineTemplate + RedTemplate.Replace("{KeyWord}", "350KM/h") + CRedTemplate.Replace("{KeyWord}", s1) + LineTemplate + YellowTemplate.Replace("{KeyWord}", "500") + CYellowTemplate.Replace("{KeyWord}", s1);
Template = ContentTemplate.Replace("{Content}", Template);
//需要加文件是否存在判断
File.Copy("d:\\Template.rtf", "d:\\hs.rtf");
StreamWriter sw = new StreamWriter("d:\\hs.rtf", false, Encoding.Default);
sw.Write(Template);
sw.Close();
关于代码中出现的s1变量为中文的GBK编码。如查对“数量:40030根”进行这种方式写入,我们需要对它做分解-》GBK编码-》进行字符串合并-》替换主模板-》保存为.rtf文件。解析的代码如下:
//直接调用方法
static List<Character> GetChars(string Chars)
{
List<Character> ListResult = new List<Character>();
Character Temp = null;
int count = 0,CurrentIndex = 1;
for (int i = 0; i < Chars.Length; i++)
{
Temp = new Character();
Temp.Index = i;
if (IsChina(Chars[i]))
{
Temp.CharStr = Chars[i] + GetChar(Chars, 1, i, out CurrentIndex);
Temp.IsChinaChar = true;
}
else
{
Temp.CharStr = Chars[i] + GetChar(Chars, 0, i, out CurrentIndex);
Temp.IsChinaChar = false;
}
if (CurrentIndex != -1)
i = CurrentIndex;
ListResult.Add(Temp);
}
return ListResult;
}
// 获取分割汉字和字母
static string GetChar(string Chars,int CharType,int EndIndex,out int CurrentIndex)
{
if (string.IsNullOrEmpty(Chars) || Chars.Length <= EndIndex + 1)
{
CurrentIndex = -1;
return string.Empty;
}
string Result = Chars.Substring(EndIndex + 1, 1);
if (IsChina(Result[0]))
{
if (CharType == 1)
{
Result += GetChar(Chars, 1, EndIndex + 1, out CurrentIndex);
}
else
{
CurrentIndex = EndIndex;
Result = string.Empty;
}
}
else
{
if (CharType == 0)
{
Result += GetChar(Chars, 0, EndIndex + 1, out CurrentIndex);
}
else
{
CurrentIndex = EndIndex;
Result = string.Empty;
}
}
return Result;
}
//获取字符串中汉字的个数
static int GetStringLen(string str)
{
//获取字符长度,汉字算2个长度,全角字符算2个长度
int count = 0;
for (int i = 0; i < str.Length; i++)
{
if (IsChina(str[i]))
count += 1;
}
return count;
}
//验证字符是否为汉字
static bool IsChina(char chr)
{
if (Convert.ToInt32(chr) < Convert.ToInt32(Convert.ToChar(128)))
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 获取GBK编码
/// </summary>
/// <returns></returns>
static string GetGBKCode(string Chars)
{
string Result = string.Empty;
byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(Chars);
foreach (byte b in gbk)
{
Result += "\\'" + string.Format("{0:X2}", b);
}
return Result;
}
[Serializable]
public class Character{
/// <summary>
/// 字符串
/// </summary>
public string CharStr{get;set;}
/// <summary>
/// 所在整个字符串中的位置
/// </summary>
public int Index{get;set;}
/// <summary>
/// 是否是中文
/// </summary>
public bool IsChinaChar{get;set;}
}
以上是实现.net自动生成rtf文件的方法。希望对读者起来实际的用处,也希望有更好的方法来交流。事例下载请点击