是否有通过改变编码来加快速度的问题? [英] Is there a wau\y to speed this up by change coding?
问题描述
我有一个带有flowlayoutpanel的表单,其中包含25个按钮。
根据excelsheet中的信息(创建了readCell函数,我读取了特定单元格中的信息)我可以在形成或隐藏它们。代码下方(没有路径信息)。我想知道有没有办法加速这个代码,因为加载表单需要一些时间,大约20秒?
private void LoadForm()
{
Excel excel =新Excel(@excel文档的一些路径,1);
for(int i = 1; i < = 25; i ++)
{
foreach(在flowLayoutPanel1.Controls中控制c)
{
按钮b = c为按钮;
if(b!= null&& excel.ReadCell(i,10)==AAN&& excel.ReadCell(i,11) == b.Name)
{
b.Visible = true;
}
if(b! = null&& excel.ReadCell(i,10)==UIT&& excel.ReadCell(i,11)== b.Name)
{
b.Visible = false;
}
}
}
excel.Close() ;
}
private void Form1_Load(object sender,EventArgs e)
{
LoadForm();
}
我尝试了什么:
代码工作正常但加载表格需要一些时间。我只是想知道是否有更聪明的方法来编写这部分代码。当我想添加更多按钮时,它确实变慢了。
I have a form with a flowlayoutpanel that contains 25 buttons.
Based on information in an excelsheet (with created readCell function I read the infomration in the specific cell) I can make buttons visible on the form or hide them. Below the code (without the path information). I was wondering is there a way to speed up this code as it takes some time, approximately 20 seconds, to load the form?
private void LoadForm()
{
Excel excel = new Excel(@"some path to an excel document", 1);
for (int i = 1; i <= 25; i++)
{
foreach (Control c in flowLayoutPanel1.Controls)
{
Button b = c as Button;
if (b != null && excel.ReadCell(i, 10) == "AAN" && excel.ReadCell(i, 11) == b.Name)
{
b.Visible = true;
}
if (b != null && excel.ReadCell(i, 10) == "UIT" && excel.ReadCell(i, 11) == b.Name)
{
b.Visible = false;
}
}
}
excel.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadForm();
}
What I have tried:
The code works fine but loading the form takes some time. I was just wondering if there is a smarter way to code this part. When I want to add more buttons it really slows down.
推荐答案
最好的解决方案是放弃使用Excel来实现它。
请看这里: C#如何阅读带有3行代码的.xlsx Excel文件 [ ^ ] - 这是我用来读取XLSX文件的那个。
The best solution is to drop the use of Excel to do it.
Have a look here: C# How To Read .xlsx Excel File With 3 Lines of Code[^] - it's the one I use to read XLSX files.
这篇关于是否有通过改变编码来加快速度的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!