Winform生成html文件问题

tempxjf 2013-04-24 12:18:56
我用在C#中做一个按钮,这个按钮是输出数据保存到一个html文件。
然后关闭程序(排除了文件占用问题)
接着打开这个html文件,表格格式确正确,却发现内容是乱码。
用记事本打开,查看里面的内容完全正确,然后我一个字符都不改动的情况下保存文件,然后再打开这个html文件乱码问题不见了!! 不懂得问题出在哪里了,请各位大侠指教!~~ 3Q
...全文
231 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tempxjf 2013-04-25
  • 打赏
  • 举报
回复
我把content=\"text/html; charset=gb2312\" 改为content=\"text/html; charset=UTF-8\" 就可以了,谢谢各位了。
tempxjf 2013-04-25
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
把你的“保存到一个html文件”代码贴出来
下面是我这个方法的所有代码: private void butPrint_Click(object sender, EventArgs e) { InWarehouse inw = this.GetInWarehouse(); if (!Validate(inw)) { MessageBox.Show("对不起,入库单数据不完整!~"); return; } string path = "D:\\明峰石业\\打印数据\\入库单\\"; string fileName = path+"tempIntWarehouse.html"; if (!Directory.Exists(path))//如果不存在就创建file文件夹 { Directory.CreateDirectory(path); } using (StreamWriter sw = new StreamWriter(fileName,false)) { // Add some text to the file. #region 设置 sw.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); sw.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); sw.WriteLine("<head>"); sw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />"); sw.WriteLine("<title>打印入库单 </title>"); sw.WriteLine("<style type=\"text/css\">"); sw.WriteLine("<!--"); sw.WriteLine(".STYLE1 { font-size: 30px; font-weight: bold; }"); sw.WriteLine(".p1{ font-size:15px;line-height:10%}"); sw.WriteLine("-->"); sw.WriteLine("</style>"); sw.WriteLine("</head>"); #endregion sw.WriteLine("<body>"); sw.WriteLine("<p align=\"center\" class=\"STYLE1\" > 入 库 单</p>"); //ID sw.WriteLine("<p align=\"center\">入库单ID:"+this._inWarehouse.Id.ToString()+"</p>"); sw.WriteLine("<table width=\"1052\" height=\"245\" border=\"1\" bordercolor=\"#000000\" cellspacing=\"0px\" align=\"center\" >"); sw.WriteLine(" <tr>"); //商家 sw.WriteLine(" <td width=\"184\" height=\"51\">商家:"+inw.Businesses.Name+"</td>"); //入库时间 sw.WriteLine(" <td width=\"215\">入库时间:"+inw.IntoReserveDate+"</td>"); //仓库 sw.WriteLine(" <td width=\"216\">存入到:"+inw.Warehouse.Name+"</td>"); //石材编号 sw.WriteLine(" <td width=\"215\">石材编号:"+inw.Number+"</td>"); //车牌 sw.WriteLine(" <td width=\"200\">车牌:"+inw.License_plate+"</td>"); sw.WriteLine(" </tr>"); sw.WriteLine(" <tr>"); if (this.ridBox.Checked) //集装箱 { sw.WriteLine(" <td height=\"57\">集装箱:"+inw.BoxFreightcs.BoxNumber+"</td>"); } else //单车 { sw.WriteLine(" <td height=\"57\">单车</td>"); } //运费 sw.WriteLine("<td>运费:12000</td>"); //已付运费 sw.WriteLine("<td>已付运费:0元</td>"); //物流公司 sw.WriteLine("<td colspan=\"2\">物流公司:天宝物流</td>"); sw.WriteLine("</tr>"); #region 石材列表 //临时变量 float temp = 0f; string str = ""; for (int i = 0; i < inw.StoneList.Count; i++) { sw.WriteLine("<tr>"); sw.WriteLine("<td height=\"66\" colspan=\"5\"><table width=\"1043\" height=\"62\" border=\"0\" cellspacing=\"0\">"); sw.WriteLine("<tr>"); //品名 sw.WriteLine(" <td width=\"138\" height=\"32\">"+inw.StoneList[i].Name.Name+"</td>"); //厚度 sw.WriteLine("<td width=\"143\">"+inw.StoneList[i].Thickness.ToString()+"CM厚</td>"); //板面 sw.WriteLine("<td width=\"203\">"+inw.StoneList[i].Surface.Name+"</td>"); //防护 switch (inw.StoneList[i].Procect) { case Protect.oily: str = "油性"; break; case Protect.waterborne: str = "水性"; break; default: str = "无"; break; } sw.WriteLine("<td width=\"154\">防护:"+str+"</td>"); //使用部位 sw.WriteLine("<td width=\"182\">使用部位:"+inw.StoneList[i].Position+"</td>"); #region 石材宽度 sw.WriteLine(" <td width=\"211\" rowspan=\"2\">"); sw.WriteLine("<table width=\"211\" height=\"60\" border=\"0\" cellspacing=\"0\">"); sw.WriteLine(" <tr>"); sw.WriteLine("<td>600头</td>"); sw.WriteLine("<td>100平方</td>"); sw.WriteLine(" </tr>"); sw.WriteLine(" <tr>"); sw.WriteLine("<td height=\"27\">800头</td>"); sw.WriteLine("<td>150平方</td>"); sw.WriteLine(" </tr>"); sw.WriteLine("</table>"); sw.WriteLine("</td>"); #endregion sw.WriteLine("</tr>"); sw.WriteLine(" <tr>"); //板型 switch (inw.StoneList[i].Shape) { case Shape.dysmorphism: str = "异形"; break; case Shape.specification: str = "规格板"; break; default: str = "毛板"; break; } sw.WriteLine(" <td>" + str + "</td>"); //面积单位 sw.WriteLine(" <td>"+inw.StoneList[i].Count.ToString()+inw.StoneList[i].Unit+"</td>"); //单价 sw.WriteLine(" <td>"+inw.StoneList[i].Price.ToString()+"元/"+inw.StoneList[i].Unit+"</td>"); //金额 temp = inw.StoneList[i].Count * inw.StoneList[i].Price; sw.WriteLine("<td>金额:" + temp.ToString() + "</td>"); sw.WriteLine("<td></td>"); sw.WriteLine("</tr>"); sw.WriteLine(" </table>"); sw.WriteLine("</td>"); sw.WriteLine("</tr>"); } #endregion sw.WriteLine("<tr>"); sw.WriteLine("<td height=\"57\" colspan=\"3\"><table width=\"650\" height=\"43\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"); sw.WriteLine(" <tr>"); sw.WriteLine(" <td width=\"51\">备注:</td>"); sw.WriteLine(" <td width=\"599\"><p class=\"p1\">" + inw.Remarks + "</p>"); sw.WriteLine(" </tr>"); sw.WriteLine(" </table></td>"); sw.WriteLine("<td>货款金额:"+inw.Total.ToString()+"元</td>"); sw.WriteLine("<td>已付:"+inw.Paid.ToString()+"元</td>"); sw.WriteLine(" </tr>"); sw.WriteLine("</table>"); sw.WriteLine("</body>"); sw.WriteLine("</html>"); sw.Close(); } }
大腹 2013-04-24
  • 打赏
  • 举报
回复
Encoding.Default.GetString()
试试
  • 打赏
  • 举报
回复
你怎么保存的? 没用Encoding参数吧。
瑞卡哥哥 2013-04-24
  • 打赏
  • 举报
回复
写文件时指定编码方式 读文件时指定相同的编码方式 就可以了
游戏人间 2013-04-24
  • 打赏
  • 举报
回复
输出文件编码不正确。请指定一种编码。
  • 打赏
  • 举报
回复
把你的“保存到一个html文件”代码贴出来
因为一直想找个办法来统计一下自己写的代码总行数,但是打开代码文件一个一个的加有点儿太二了.而且还不能忽略空行和注释.因此就写了这么程序用来统计真实的代码总行数. 如果各位有什么高见一定要给在下指教 本程序支持对sln(解决方案文件)、csproj(c#项目文件)、vbproj(vb.net项目文件)下所包含的源代码进行代码行数统计工作. 支持vs2003\2005\2008等版本所生成的解决方案文件或者项目文件 程序针对的语言为.net winform下的C#和VB.net,不支持J#.c++尚未测试 在计算代码行数时可以选择忽略空行、忽略注释或者忽略visual studio所自动生成文件。这样可以得到自己亲手写的真实的代码行数。 同时,显示窗里可以看到该 解决方案文件或者项目文件下所包含的文件列表(有图表显示)界面还算漂亮 该树状列表可以支持多选,你可以只选择其中指定的几个文件或者项目进行代码行数统计,还是比较方便的。 v2.3.5版 1.支持C#与vb.net混合编程生成的解决方案文件,通过该解决方案文件可以统计解决方案下所有的代码文件的代码行数下载地址 2.增加C#与VB.NET的图标,可以更加清楚的识别不同语言写出的代码文件. 3.增加鼠标悬停提示,不必最大化程序或者或者拉伸标题栏就,只需悬停鼠标在指定的节点上就可显示该文件的完整名称. 4.增加"展开所有节点"选项框,可以让程序自动展开所有节点. 可以看我的博客,详细介绍发在这里了:http://www.cnblogs.com/tannaiyin/archive/2009/06/04/1496438.html

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧