简体   繁体   English

EPPlus 改变电池内部颜色

[英]EPPlus changes cell interior color

I found something really strange while using EPPlus and I couldn't find the solution anywhere online (I also didn't find anyone with the same problem to be fair).我在使用 EPPlus 时发现了一些非常奇怪的东西,而且我在网上的任何地方都找不到解决方案(公平地说,我也没有找到任何有同样问题的人)。 Luckily, after some time, I found out how to fix it and because I couldn't find anything about it, I want to save someone else his/her time and document it here.幸运的是,一段时间后,我发现了如何修复它,因为我找不到任何关于它的信息,我想节省其他人的时间并将其记录在此处。 Note, the past week was my first experience with EPPlus so I'm sorry if this question is very trivial and everyone knew this already.请注意,过去一周是我第一次使用 EPPlus,所以如果这个问题非常琐碎并且每个人都已经知道这一点,我很抱歉。

The Issue问题

I'm trying to put data into an existing Excel template using EPPlus.我正在尝试使用 EPPlus 将数据放入现有的 Excel 模板中。 One of the sheets of the Excel file looks like this:其中一张 Excel 文件如下所示: 在此处输入图片说明

Nothing strange about it you would say.你会说这没什么奇怪的。 However, when I open this Excel file in my code and use EPPlus to add values and save it, it comes out like this:但是,当我在代码中打开此 Excel 文件并使用 EPPlus 添加值并保存时,结果如下:

在此处输入图片说明

Of course the numbers in the table are intentional, but how does it suddenly have a purple background!?表里的数字当然是故意的,但是怎么突然有紫色的背景!? Also why doesn't it happen to particular cells.还有为什么它不会发生在特定的细胞上。 There is nothing in the code that does anything with the styling.代码中没有对样式做任何事情的内容。 I only add values to the table.我只向表中添加值。

Another strange thing is that this happened on multiple pc's, but not on all.另一个奇怪的事情是这发生在多台电脑上,但不是全部。

The underlying problem根本问题

After looking around, I found out that the standard styling suddenly showed the same purple color.环顾四周,发现标准造型突然呈现出同样的紫色。

Before

普通造型

After

在此处输入图片说明

I still don't understand why it changes the color, but it does.我仍然不明白为什么它会改变颜色,但确实如此。 Then somewhere on github, someone mentioned that you need to change the code of EPPlus to get rid of this.然后在github的某个地方,有人提到您需要更改EPPlus的代码才能摆脱这种情况。 It mentioned that because the Excel spreadsheet was created in another language than English, it couldn't find the "Normal" style, since it's named "Standaard" in Dutch in this case, and you have to change "Normal" into "Standaard" in the code.它提到因为Excel电子表格是用另一种语言而不是英语创建的,所以找不到“Normal”样式,因为在这种情况下它在荷兰语中被命名为“Standaard”,您必须将“Normal”更改为“Standaard”在代码中。

But I just used Nuget to get a package and didn't want to create my own package, and more important: What if I have a spreadsheet next time that is created in the English version?但是我只是用Nuget来获取包,不想创建自己的包,更重要的是:如果下次我有一个英文版的电子表格怎么办?

The Solution解决方案

For such a long story, the solution is just to easy: In you workbook, in VBA in the immediate window, type the following code对于这么长的故事,解决方案很简单:在您的工作簿中,在立即窗口的 VBA 中,键入以下代码

Thisworkbook.Styles.Add("Normal")

The workbook now has a custom format called "Normal".工作簿现在具有称为“正常”的自定义格式。 Since it's likely that your normal format would be just blank, this will show you the proper styling.由于您的正常格式可能只是空白,这将向您显示正确的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM