【问题标题】:Create EXCEL from html table using C#使用 C# 从 html 表创建 EXCEL
【发布时间】:2012-12-07 18:34:22
【问题描述】:

我正在尝试使用 C# 和 HTML 表格创建一个用于运送产品的 Excel 表格。下面显示的图像是我在浏览器中显示的 HTML。我想要做的是把这张桌子塞进一个excel页面,保持比例和合并完好无损。 (我在每个 td 上设置了一个边框颜色来表示 excel 中的单元格,一旦在 Excel 工作表中就不需要该颜色了)。

我尝试从这篇文章 (How to export html table to excel using asp.net/c#) 开始工作,但是当使用此代码时,我的表格在文件中并没有变成单元格,它只是一个包含文本的单元格。

要补充的是,当使用该链接上的代码时,我在 aspx 页面中复制了代码并将其作为 html 字符串添加到表单中,如链接上的示例所示。然后,这产生了与我之前手工制作的相同形式。

var html = "<tabe><tr><td width=\"86\">x</td><td width=\"72\">x</td><td width=\"91\">x</td><td width=\"72\">x</td><td width=\"72\">x</td><td width=\"139\">x</td><td width=\"93\">x</td><td width=\"72\">x</td><td width=\"72\">x</td><td width=\"72\">x</td><td width=\"72\">x</td><td width=\"72\">x</td></tr><tr><td rowspan=\"5\" colspan=\"2\" style=\"border: solid 2px black;\">INVOICE</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td rowspan=\"2\" colspan=\"2\" style=\"background-color: green;\">Ref</td><td></td><td style=\"background-color: green;\">Swiss Post</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td style=\"background-color: yellow;\">SoBling</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></table>";

我做错了什么?

【问题讨论】:

  • 我建议您制作 DataTable,将您的条目从 html 保存到其中。将数据放入 Excel 中,并格式化单元格合并/颜色/大小等。
  • 你有我如何设置列宽、行高和使用数据表执行单元格合并的示例吗?
  • 你不能在数据表中,但是当你将数据表导出到 Excell 中时,你可以直接在那里格式化单元格,即合并:eWSheet.Range[eWSheet.Cells[1, 1], eWSheet.Cells[4, 1 ]].Merge();

标签: c# html excel export html-table


【解决方案1】:

我为您制作了一些链接,可以帮助您进行合并、宽度、高度和创建 Excell。我制作的链接只引用了应该一起工作的 SO。

creating simple excel sheet in c# with strings as input

Merging Cells in Excel using C#

.net c# excel column AutoFit

excel cell coloring

宽度和高度是“相同的”。我没有在代码方面为您提供解决方案,而是提供链接,以便您制定自己的解决方案并从中学习。如果您有任何问题,请询问。

【讨论】:

    【解决方案2】:

    我推荐使用 epplus 来创建 excel 图表

    http://epplus.codeplex.com/

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 2015-08-05
      • 1970-01-01
      • 2012-04-05
      • 2023-04-03
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多