上网找了个二进制流导出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();
}
下面把代码贴上来,小弟第一次发贴,能解决问题的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();
}
解决方案 »
- INSERT INTO 语句的语法错误。天啊!!!搞了2天都搞不好这个问题
- 谁有地图标记的源码。
- asp.net 怎么进行数据库的倒序调用?
- 怎样把源代码中的&去除?
- 实在想不出来了,关于datagrid的问题,请教高手啊!!!!
- 100分助,关于扩展GridView的问题
- 散尽所有分求解,高手进。
- vs.net服务器资源管理器中就不能打开SqlServer (请高手指点?)
- 如何 弹出一个窗口
- session怎么传到另一个页面?急
- 编译器错误信息: CS0117: “System.Data.SqlDbType”并不包含“Date”的定义
- 图片连续滚动,为什么我这个就不滚动呢?求救!!!!!!!
方法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;
}
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);
}