设置 HTML 表格单元格的格式以使 Excel 格式为文本?

发布于 2024-10-11 02:36:53 字数 92 浏览 7 评论 0原文

我正在创建一个 HTML 表格,该表格将在 Excel 中作为电子表格打开。我可以使用什么 HTML 标记或 CSS 样式来“告诉”Excel 将单元格的内容显示为文本?

I am creating an HTML table that will be opened as a spreadsheet in Excel. What HTML tag or CSS style can I use to "tell" Excel to display the cell's contents as text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

深白境迁sunset 2024-10-18 02:36:53

您可以将格式应用于数字、文本、日期等单元格。

请参阅我之前的回答:HTML 到 Excel:如何告诉 Excel 将列视为数字?

(调整后的代码段)

如果您将 CSS 类添加到您的页面:

.num {
  mso-number-format:General;
}
.text{
  mso-number-format:"\@";/*force text*/
}

然后将这些类添加到您的 TD 上,这有效吗?

<td class="num">34</td>
<td class="num">17.0</td>
<td class="text">067</td>

You can apply formatting to the cells for numbers, text, dates, etc.

See my previous answer on this: HTML to Excel: How can tell Excel to treat columns as numbers?

(adjusted snippet)

If you add a CSS Class to your page:

.num {
  mso-number-format:General;
}
.text{
  mso-number-format:"\@";/*force text*/
}

And slap those classes on your TD's, does it work?

<td class="num">34</td>
<td class="num">17.0</td>
<td class="text">067</td>
乖乖兔^ω^ 2024-10-18 02:36:53

使用该解决方案存在一个问题(具有数字格式的 CSS 样式)。 Excel 给出错误“数字存储为文本”,这在某些情况下可能会带来不便。
为了避免此问题,可以在字段的开头使用零宽度空格字符 (​)。

There is one problem using that solution (css style with number-format). The Excel gives the error "Number Stored as text" which can be inconvenient in some cases.
To avoid this problem it's possible to use the ZERO WIDTH SPACE character (​) in the begining of the field.

ゞ花落谁相伴 2024-10-18 02:36:53

您也可以通过在 元素的值之前添加不间断空格:   来解决该问题。< br>
例子:
 0:12:12.185

而不是:
0:12:12.185

You can solve the problem too by adding non-breaking space:   before the value of the <td> element.
Example:
<td> 0:12:12.185</td>

Instead of:
<td>0:12:12.185</td>

要走就滚别墨迹 2024-10-18 02:36:53

我没有足够的代表来发表评论或投票,但拉波索的回答对我来说非常有效。我们的系统导入 SSRS 报告并以本地模式运行它们。它将数据集查询存储在数据库中并在运行时提取它们。然而,对于 Excel 导出,它只是将查询的结果数据运行到 DataGrid 对象中,并将其作为 HTML 直接写入流,并将扩展名设置为 .xls。将 Raposo 的解决方案放入报告的 DataSet 查询中:

SELECT someColumn = '​' + someColumn 
FROM, etc.

并在 SSRS 字段的表达式中将其删除:

=Replace(Fields!someColumn.Value, "​", "")

是我发现的唯一可行的方法。谢谢!

I don't have enough rep to comment or up-vote, but Raposo's answer worked very well for me. Our system imports SSRS reports and runs them in local mode. It stores the DataSet query(ies) in the database and pulls them at runtime. However for Excel exports it just runs the query's resulting data into a DataGrid object and writes that directly to the stream as HTML, setting the extension to .xls. Putting Raposo's solution in the report's DataSet query:

SELECT someColumn = '​' + someColumn 
FROM, etc.

and removing it in the SSRS field's expression:

=Replace(Fields!someColumn.Value, "​", "")

is the only thing I've found that works. Thanks!

一枫情书 2024-10-18 02:36:53

我找到了解决此问题的五种解决方案:

  1. 按照 scunliffe 的描述将字段格式化为文本。这存在 Raposo 所说的绿色三角形问题。

  2. 使用​正如拉波索所描述的。这有一个问题,即该值并不是真正的数字。如果将数据拉入某个系统进行处理,这可能会成为问题。

  3. 将 TD 添加为 ="067"。这与 #2 具有相同的问题。

  4. 将 TD 添加为 =text(067,"000")。这也有与 #2 相同的问题。

  5. 使用 CSS 类 .text3 {mso-number-format:"000";} 并将此类添加到 TD。这是我的首选解决方案,但如果您有不同长度的数字,则它存在需要多个类的问题。如果您编写标头,然后迭代数据,则必须添加所有可能的类,然后才能知道需要哪些类。但这的优点是文本实际上是数字并且没有绿色三角形。

I have found five solutions for this issue:

  1. Format the field as text as described by scunliffe. This has the problem of the green triangle as stated by Raposo.

  2. Use ​ as described by Raposo. This has the problem that the value is not really a number. This could be an issue if the data is pulled into some system for processing.

  3. Add the TD as <td>="067"</td>. This has the same problem as #2.

  4. Add the TD as <td>=text(067,"000")</td>. This also has the same problem as #2.

  5. Use a CSS class of .text3 {mso-number-format:"000";} and add this class to the TD. This is my preferred solution but it has the problem of requiring multiple classes if you have numbers of different lengths. If you write your header and then iterate through your data, you have to add all possible classes before knowing which of them you will need. But this has the advantages that the text is really a number and there is no green triangle.

娇柔作态 2024-10-18 02:36:53

非常棒的解决方案!我按照下面的方法做了

HttpContext.Current.Response.Write("<style>  .txt " + "\r\n" + " {mso-style-parent:style0;mso-number-format:\"" + @"\@" + "\"" + ";} " + "\r\n" + "</style>");
HttpContext.Current.Response.Write("<Td class='txt'>​");
HttpContext.Current.Response.Write(Coltext);
HttpContext.Current.Response.Write("</Td>");

,它对我来说效果很好

Superb solution! I did it like below

HttpContext.Current.Response.Write("<style>  .txt " + "\r\n" + " {mso-style-parent:style0;mso-number-format:\"" + @"\@" + "\"" + ";} " + "\r\n" + "</style>");
HttpContext.Current.Response.Write("<Td class='txt'>​");
HttpContext.Current.Response.Write(Coltext);
HttpContext.Current.Response.Write("</Td>");

and it works fine for me

下雨或天晴 2024-10-18 02:36:53

尝试在您的值之前添加单引号:

'

这样您的单元格将成为

<td>'007</td>

这是对我有用的唯一解决方案。没有一个住所适用于我的解决方案适用的“开放式办公室”和“自由办公室”。最好的部分是,如果您复制该值,它不会复制单引号。

Try adding a single quote before your value:

'

So your cell would become

<td>'007</td>

This was the only solution that worked for me. Non of the abode worked for "Open Office" and "Libre Office", where my solution work. And the best part is that if you copy the value it won't copy the single quote.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文