StringWriter问题

时间:2010-01-27 15:09:33

标签: asp.net asp.net-mvc asp.net-mvc-2

我使用以下代码:(显然返回一个excel)

   ControllerContext.HttpContext.Response.ContentType = "application/ms-excel";
   ControllerContext.HttpContext.Response.Write(sw.ToString());
   ControllerContext.HttpContext.Response.End();
   return View();

其中sw是一个StringWriter,我在其中创建我的excel结构(一个表)。 因此,sw.ToString()是一个表,一些tds包含像0001234这样的值。 当我打开excel文件时,我在那些tds 1234中看到,没有零。 我该怎么做才能看到那些零?

4 个答案:

答案 0 :(得分:1)

您正在生成的文件格式是什么?既然你在谈论TD,我认为你正在使用HTML。

尝试生成XMLSS。它允许您指定大多数格式,它只是一个普通的XML文件,因此生成它并不是一件麻烦事。 (您可以通过将文档另存为“XML Spreadsheet 2003”格式来获得基本结构)

答案 1 :(得分:0)

我自己也没有尝试过,但是如果你在数字前加上',它会在Excel中将其视为一个字符串。也许它有用......

答案 2 :(得分:0)

您可以为TD添加css样式:

作为文字:

<style type="text/css">
.number-txt { mso-number-format:\@ }
</style>

<td class="number-txt">0001234<td>

作为数字:

<style type="text/css">
.number { mso-number-format:0000000 }
</style>

<td class="number">0001234<td>

答案 3 :(得分:0)

嗯,一个解决方案就是在每个td中写一个&amp; nbsp然后是值,它看起来很棒。

tw.RenderBeginTag(HtmlTextWriterTag.Td); tw.Write(“”);
tw.Write(值); tw.RenderEndTag();