以简单的方式导出到 Excel(但不是 CSV)

发布于 2024-08-11 11:03:20 字数 876 浏览 4 评论 0原文

昨天,我发现了这个 如何通过简单地生成将数据导出到 Excel 电子表格的方法 HTML。这似乎是一种直接导出带格式的单元格的方法,而仅使用 CSV 格式是无法实现的。

我的想法是在Java应用程序中处理一些数据,将结果导出到Excel电子表格,并利用Excel的功能进一步分析数据,绘制图表等......(避免重新实现Excel的强大功能)。但是,在导出数据之前,我想为某些单元格的背景着色。 Excel 的“条件格式”功能还不够(另外,我不想手动执行此操作)。我发现在 Java 应用程序中准备具有所有格式的电子表格并在 Excel 中使用非常方便。

How-To 中提到的方法很简单并且效果很好,除了对于两个问题。我希望你们中的一些人可能有这方面的经验,并且能够为这个问题提供一些线索。

  1. 当我在 Firefox 中打开 XLS 文件时,为各个单元格着色(如 )看起来完全符合我想要的方式,但 Excel 会将颜色更改为我想要的颜色相信他们称之为“网络安全”调色板。这意味着,我得到的不是 256 种不同深浅的红色,而是“红色”、“紫红色”、“粉色”、“橙色”、“浅黄色”和“白色”。有什么方法可以阻止这种颜色转换的发生吗?

  2. 当我打开 XLS 文件(实际上内部只是 HTML)时,一切看起来都不错,但分隔各个单元格的灰色细线消失了,我不知道如何“打开它们”。这是怎么回事?

问题1是我真正想解决的主要问题。

Yesterday I found this How-To on exporting data to Excel spreadsheets by simply producing HTML. Seemed like a straightforward way of exporting cells with formatting, what one can not achieve using just CSV format.

My idea was to process some data in a Java app, export the results to Excel spreadsheet and make use of Excel's capabilities to further analyse the data, draw charts etc... (avoid re-implementing Excel's powerful features). However, before I export the data I wanted to color the background of some of the cells. The 'conditional formatting' feature of Excel would not suffice (plus, I don't want to do that manually). I find it rather convenient to prepare the spreadsheet with all formatting in the Java app and go from there in Excel.

The approach mentioned in the How-To, is simple and works well, except for two problems. I was hoping some of you may have previous experience with this and will be able to provide some light into this issue.

  1. Coloring the individual cells like <td bgcolor="#ff2323"> will look exactly the way I want when I open the XLS file in Firefox, but Excel changes the colors to what I believe they call 'web-safe' color palette. Which means, instead of having 256 different shades of red, I get a 'red', 'fuchsia', 'pink', 'orange', 'light yellow' and 'white'. Is there any way to stop this color conversion from happening?

  2. When I open the XLS file (which in reality is just HTML on the inside), all looks good but the thin grey lines separating individual cells are gone and I can't figure out how to 'turn them on'. What's with that?

Problem 1 is the main issue which I would really like to solve.

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

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

发布评论

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

评论(3

空心空情空意 2024-08-18 11:03:20

您可以使用特殊的 XML 格式选项在电子表格中强制使用网格线:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
                        xmlns:x="urn:schemas-microsoft-com:office:excel"
                         xmlns="http://www.w3.org/TR/REC-html40">
<head>
<xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Sheet</x:Name>
    <x:WorksheetOptions>
     <x:Print>
        <x:Gridlines />
     </x:Print>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
 </x:ExcelWorkbook>
</xml>
</head>                      
<body>
<table>
<tr><td bgcolor="#ff2323">hello</td></tr>
</table>
</body>
</html>                      

更一般地,请参阅此 MSDN 链接 有关此格式的详细文档。

You can use the special XML formatting options to force gridlines inside the spreadsheet:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
                        xmlns:x="urn:schemas-microsoft-com:office:excel"
                         xmlns="http://www.w3.org/TR/REC-html40">
<head>
<xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Sheet</x:Name>
    <x:WorksheetOptions>
     <x:Print>
        <x:Gridlines />
     </x:Print>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
 </x:ExcelWorkbook>
</xml>
</head>                      
<body>
<table>
<tr><td bgcolor="#ff2323">hello</td></tr>
</table>
</body>
</html>                      

More generally, see this MSDN link for detailed documentation on this format.

林空鹿饮溪 2024-08-18 11:03:20

我实际上希望 Excel 不会为您提供标准的 Web 安全颜色,而是从它自己的调色板中进行选择(调色板更小;“传统”版本的 Excel 上只有 56 个插槽,也许 2007 年已经取消了这一点)。可以在 Excel 中选择您自己的自定义颜色(我在工作中使用的版本中为“工具”->“选项”->“颜色”),但您选择的每种新颜色都将替换“工厂”默认值之一。

我怀疑自动 HTML 到 Excel 导入只是不想为您设置这些颜色,特别是因为您的 HTML 伪 Excel 文件包含的颜色可能多于 Excel 调色板插槽的颜色。那么无论如何它都会被迫让你失望。 Excel 的实现者可能认为预先让您失望会更容易、更简单。 ;)

I would actually expect Excel not to give you the standard Web-safe colors but rather choose from its own palette (which is even smaller; only 56 slots on "traditional" versions of Excel, perhaps 2007 has lifted this). It is possible to select your own custom colors within Excel (Tools->Options->Color in the version I use at work), but each new color you choose would replace one of the "factory" defaults.

I suspect the automatic HTML-to-Excel import just doesn't want to go through the trouble of setting up these colors for you, especially since the possibility exists that your HTML pseudo-Excel file contains more colors than Excel's palette has slots. Then it would be forced to disappoint you anyway. Excel's implementers probably decided it would be easier and simpler to just disappoint you up front. ;)

故事与诗 2024-08-18 11:03:20

对于网格线...

  1. 进入工具> 选项,然后在视图选项卡中选中网格线复选框

(基于 Excel 2003)

For Gridlines...

  1. Go into Tools > Options, then in the View Tab check the Gridlines checkbox

(Based on Excel 2003)

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