上网找了个二进制流导出word的方法,先开始导出的时候文件名也是乱码,换成UTF-8就行了,输出内容的格式也换了,但是在Word里面中文显示依旧是乱码.
下面把代码贴上来,小弟第一次发贴,能解决问题的50分献上! protected void Button1_Click(object sender, EventArgs e)
        {
            string aa = @"<table width='100%' border='0' style='font-size:9pt; background:black;'cellpadding='2' cellspacing='1'><tr style='background:rgb(182,221,232); height:30px; font-weight:bold; text-align:center'>
            <td style='width:150px;' rowspan='2'>组名</td>
            <td style='width:150px;' rowspan='2'>成员</td>
            <td colspan='2'>需要评价的制度</td>
            <td style='width:100px;' rowspan='2'>已完成百分比</td>
            <td style='width:200px;' rowspan='2'>组长已审核并提交管理员的情况</td>
            <td style='width:150px;' rowspan='2'>已完成百分比</td>
            <td rowspan='2'>备注</td></tr><tr style='background:rgb(182,221,232); height:30px; font-weight:bold; text-align:center'>
            <td style='width:70px;'>分配情况</td>
            <td style='width:70px;'>已提交情况</td>
            </tr><tr style='background-color:white;'></td></tr></table>";            HttpContext.Current.Response.Buffer = true;            HttpContext.Current.Response.Clear();            HttpContext.Current.Response.ClearContent();            HttpContext.Current.Response.ClearHeaders();               Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("文档", System.Text.Encoding.UTF8) + ".doc");            Response.ContentEncoding = System.Text.Encoding.UTF8;            HttpContext.Current.Response.ContentType = "application/ms-word";            HttpContext.Current.Response.Charset = "gb2312";            HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
          
            oHtmlTextWriter.Write(aa);
            
            Response.Write(oStringWriter.ToString());            Response.End();
}

解决方案 »

  1.   

    try...
    方法1
        /// <summary>
        /// 二进制数据转换为word文件
        /// </summary>
        /// <param name="data">二进制数据</param>
        /// <param name="fileName">word文件名</param>
        public string ConvertWord(byte[] data, string fileName)
        {
            string savePath = @"\SystemWord\"; 
            if (!System.IO.Directory.Exists(GetPath() + savePath))
            {
                Directory.CreateDirectory(GetPath() + savePath);
            }
            savePath += fileName + ".doc";
            string filePath = GetPath() + savePath;
            
            FileStream fs;
            if (System.IO.File.Exists(filePath))
            {
                fs = new FileStream(filePath, FileMode.Truncate);
            }
            else
            {
                fs = new FileStream(filePath, FileMode.CreateNew);
            }
            BinaryWriter br = new BinaryWriter(fs);
            br.Write(data, 0, data.Length);
            br.Close();
            fs.Close();
            return savePath;
        }    /// <summary>
        /// word文件转换二进制
        /// </summary>
        /// <param name="wordPath">word文件路径</param>
        /// <returns>二进制</returns>
        private byte[] wordConvertByte(string wordPath)
        {
            byte[] bytContent = null;
            System.IO.FileStream fs = null;
            System.IO.BinaryReader br = null;
            try
            {
                fs = new FileStream(wordPath, System.IO.FileMode.Open);
            }
            catch
            {
            }
            br = new BinaryReader((Stream)fs);
            bytContent = br.ReadBytes((Int32)fs.Length);        return bytContent;
        }
      

  2.   

    ·方法2-------------将文件转换为二进制,并保存到数据库中----------------            string strResult = strPath + @"\result.doc";            System.IO.FileStream fs = new System.IO.FileStream(@strResult, System.IO.FileMode.Open);
                fs.Position = 0;
                byte[] content = new byte[fs.Length];
                fs.Read(content, 0, (int)fs.Length);
                fs.Close();
                fluidDesignDoc.qhse1 = content;//其中qhse1在数据库中保存的类型为OLE对象
                IFluidDesignDocManager manager = new FluidDesignDocManager();
                manager.UpdateFluidDesignDoc(fluidDesignDoc);
    -------------将二进制数据转化为指定位置文件---------------------private void getFile(byte[] content, string filePath)
            {
                string fileName = filePath;            if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }            //FileStream sw = new FileStream(@fileName, FileMode.Create);
                //StreamWriter fs = new StreamWriter(sw, Encoding.UTF8);
                //fs.Write(entity.Content);
                System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
                fs.Write(content, 0, content.Length);
                fs.Flush();
                fs.Close();
                fileName = System.IO.Path.Combine(Application.StartupPath, fileName);
            }