C#編寫Rtf文檔,中文與字母區分,C#將漢字轉換成GBK編碼


      最近兩天在調試仰邦的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文件的方法。希望對讀者起來實際的用處,也希望有更好的方法來交流。事例下載請點擊



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM