C#循环到Excel行 [英] C# loop to Excel row
本文介绍了C#循环到Excel行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用我提供的代码,我将动态文本框发送到excel模板。通过这五个语句,它将创建一行信息。我需要为其他动态文本框创建另一个循环,以便在先前数据下方开始6行。有人可以帮忙吗?
int StartBundleRow = 11 跨度>;
for ( int BndlRow = 0 ; BndlRow < bundleRows; BndlRow ++) // 将捆绑行添加到电子表格
{
worksheet.Rows [StartBundleRow] .Insert();
worksheet.Cells [StartBundleRow, D]。 value = srcBundlePanel.Controls [ txtQtyBundle + BndlRow] .Text;
worksheet.Cells [StartBundleRow, E]。 value = srcBundlePanel.Controls [ txtProductNameBundle + BndlRow] .Text;
worksheet.Cells [StartBundleRow, F]。 value = srcBundlePanel.Controls [ txtListPriceBundle + BndlRow] .Text;
worksheet.Cells [StartBundleRow, G]。 value = srcBundlePanel.Controls [ txtMaxDiscountBundle + BndlRow] .Text;
worksheet.Cells [StartBundleRow ++, H]。 value = srcBundlePanel.Controls [ txtProposedPriceBundle + BndlRow] .Text;
}
解决方案
看起来你的问题是跟踪你的工作。
从评论中,我看到你被第一个块的可变行数所困。
这是简单的数学:
你知道第一个块的第一行是StartBundleRow
= 11
你知道第一行的行数是多少block是bundleRows
Deduce第一个块的最后一行是StartBundleRow
+bundleRows
- 1
添加6个空白行StartBundleRow
+bundleRows
- 1 + 6
下一行开始第二个块StartBundleRow
+bundleRows
- 1 + 6 + 1,它给出StartBundleRow
+bundleRows
+ 6
你就完成了。
这类事情是编程的基础。
int RowOffset;
int StartBundleRow = 11 ;
for (RowOffset = StartBundleRow; RowOffset < bundleRows + StartBundleRow; RowOffset ++ )
{
worksheet.Rows [RowOffset] .Insert();
worksheet.Cells [RowOffset, D]。 value = srcBundlePanel.Controls [ txtQtyBundle + RowOffSet ]。文本;
// 依此类推
}
RowOffset + = 6 ;
// 使用'RowOffset $ p $开始第二次循环p>使用在'for循环外声明的计数器变量,然后你可以在'for循环退出时使用它。
With the code I've provided I am sending dynamic textboxes to an excel template. With these five statements it will create 1 row of information. I need to create another loop for other dynamic textboxes to start 6 rows below the previous data. Can someone please help ??
int StartBundleRow = 11;
for (int BndlRow = 0; BndlRow < bundleRows; BndlRow++) //add bundle rows to spreadsheet
{
worksheet.Rows[StartBundleRow].Insert();
worksheet.Cells[StartBundleRow, "D"].value = srcBundlePanel.Controls["txtQtyBundle" + BndlRow].Text;
worksheet.Cells[StartBundleRow, "E"].value = srcBundlePanel.Controls["txtProductNameBundle" + BndlRow].Text;
worksheet.Cells[StartBundleRow, "F"].value = srcBundlePanel.Controls["txtListPriceBundle" + BndlRow].Text;
worksheet.Cells[StartBundleRow, "G"].value = srcBundlePanel.Controls["txtMaxDiscountBundle" + BndlRow].Text;
worksheet.Cells[StartBundleRow++, "H"].value = srcBundlePanel.Controls["txtProposedPriceBundle" + BndlRow].Text;
}
解决方案
It looks like your problem is to keep track of what you do.
From the comments, I see that you are stuck with the variable number of rows of first block.
This is simple maths:
You know that first row of first block isStartBundleRow
= 11
You know that the number of rows in first block isbundleRows
Deduce that Last row of first block isStartBundleRow
+bundleRows
- 1
add 6 blank rowsStartBundleRow
+bundleRows
- 1 + 6
Second block start on next rowStartBundleRow
+bundleRows
- 1 + 6 + 1 which givesStartBundleRow
+bundleRows
+ 6
and you are done.
This kind of things is the basic of programming.
int RowOffset; int StartBundleRow = 11; for (RowOffset = StartBundleRow; RowOffset < bundleRows + StartBundleRow ; RowOffset++) { worksheet.Rows[RowOffset].Insert(); worksheet.Cells[RowOffset, "D"].value = srcBundlePanel.Controls["txtQtyBundle" + RowOffSet].Text; // and so on } RowOffset += 6; // start your second loop using 'RowOffsetUsing a counter variable declared outside the 'for loop, then you can use that when the 'for loop exits.
这篇关于C#循环到Excel行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文