Winform生成html文件问题

tempxjf 2013-04-24 12:18:56
我用在C#中做一个按钮,这个按钮是输出数据保存到一个html文件。
然后关闭程序(排除了文件占用问题)
接着打开这个html文件,表格格式确正确,却发现内容是乱码。
用记事本打开,查看里面的内容完全正确,然后我一个字符都不改动的情况下保存文件,然后再打开这个html文件乱码问题不见了!! 不懂得问题出在哪里了,请各位大侠指教!~~ 3Q
...全文
238 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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文件”代码贴出来

111,098

社区成员

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

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

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