[英]SQL export data to Excel
In Windows Forms, I use C# to export data from SQL to Excel, It was successful, I want any data greater than 2, I will highlight that cell in red, I write that but I get an error在 Windows Forms 中,我使用 C# 将数据从 SQL 导出到 Excel,它成功了,我想要任何大于 2 的数据,我会用红色突出显示那个单元格,我写了那个但是我得到了一个错误
Index was outside the bounds of the array
指数数组的边界之外
My code:我的代码:
int stt1 = 0;
float temp = 2.0f;
row = 6;
foreach (var element in datalist)
{
stt1++;
row++;
dynamic[] arr1 = { element.col2, element.col3, element.col4, element.col5 };
Range rowData = ws.get_Range("C" + row, "F" + row);
rowData.Font.Size = 11;
rowData.Font.Name = "Times New Roman";
rowData.Value2 = arr1;
Tag_Public.temp = Convert.ToSingle(arr1[stt1]);
if (Tag_Public.temp >= temp)
{
rowData.Font.Bold = true;
rowData.Font.Color = ColorTranslator.ToOle(Color.Red);
};
}
Can someone give me an idea how to solve the problem?有人可以告诉我如何解决这个问题吗? Thanks a lot
非常感谢
You haven't given us that much information, however one thing I noticed was that you increase stt1 at the begining of your foreach loop and later use it for accessing your array arr1.你没有给我们那么多信息,但我注意到的一件事是你在 foreach 循环开始时增加了 stt1,然后用它来访问你的数组 arr1。 I suspect you intended to increase stt1 at the end of your foreach so that you start accessing arr1 at index 0 and not 1. Since you start with 1 and increase you are likley to exceed the array bounds which leads to the error you get.
我怀疑您打算在 foreach 的末尾增加 stt1,以便您开始访问索引 0 而不是 1 处的 arr1。由于您从 1 开始并增加,您很可能会超出数组边界,这会导致您得到错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.