C# 导出word 和中文乱码

   protected void btnWord_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT语句filename";
            string filename = this.Sql.GetString(strSql) + DateTime.Now.ToString("yyyyMMdd");
            strSql = "SELECT 主循环";
            DataTable dt = GetDataTable(strSql);
            if (dt.Rows.Count == 0 || dt == null)
            {
                this.MsgBox("读取数据失败!");
                return;
            }
			//这块就像html里面类似
            Response.ContentType = "application/msword";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + ToHexString(filename) + ".doc");//ToHexString(filename)防止中文乱码
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strSql = "SELECT 子循环";
                DataTable dtview = this.Sql.GetDataTable(strSql);
                if (dtview.Rows.Count > 0)
                {
                    sb.Append("");
                    sb.Append("");
                    sb.AppendFormat("", dt.Rows[i]["列名"].ToString(), dt.Rows[i]["列名"].ToString(), dt.Rows[i]["列名"].ToString());
                    sb.AppendFormat("
Tilte公司
Title标题
作业:{0}单位:{1}编号:{2}
日 期:{0}期间:{1}
", DateTime.Parse(dtview.Rows[0]["列名"].ToString()).ToString("yyyy-MM-dd"), dt.Rows[i]["列名"].ToString()); sb.Append(""); for (int j = 0; j < dtview.Rows.Count; j++) { sb.AppendFormat("", (j + 1).ToString() + "." + dtview.Rows[j]["列名"].ToString()); switch (int.Parse(dtview.Rows[j]["lngrule"].ToString())) { case 1: sb.AppendFormat("", "√"); break; case 2: sb.AppendFormat("", "√"); break; case 3: sb.AppendFormat("", "√"); break; } sb.AppendFormat("", dtview.Rows[j]["列名"].ToString()); sb.Append(""); } sb.Append("
重 点标 准结果
不适用
{0}{0}{0}{0}{0}
"); sb.Append("
"); } } Response.Write(sb.ToString()); Response.End(); } #region 保存word中文名称乱码 /// /// 为字符串中的非英文字符编码 /// /// /// public string ToHexString(string s) { char[] chars = s.ToCharArray(); StringBuilder builder = new StringBuilder(); for (int index = 0; index < chars.Length; index++) { bool needToEncode = NeedToEncode(chars[index]); if (needToEncode) { string encodedString = ToHexString(chars[index]); builder.Append(encodedString); } else { builder.Append(chars[index]); } } return builder.ToString(); } /// ///指定 一个字符是否应该被编码 /// /// /// private bool NeedToEncode(char chr) { string reservedChars = "$-_.+!*'(),@=&"; if (chr > 127) return true; if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0) return false; return true; } /// /// 为非英文字符串编码 /// /// /// private string ToHexString(char chr) { UTF8Encoding utf8 = new UTF8Encoding(); byte[] encodedBytes = utf8.GetBytes(chr.ToString()); StringBuilder builder = new StringBuilder(); for (int index = 0; index < encodedBytes.Length; index++) { builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16)); } return builder.ToString(); } #endregion


 
  
 
  

你可能感兴趣的:(C#)