【发布时间】:2015-12-06 21:17:37
【问题描述】:
我之前有一个与此类似的问题,但我仍然对实际解决方案一无所知。我正在将动态文本框中的文本行发送到 Excel,此信息来自数据库。在我发送“txtProductNameBundle”的行中,有时还有一个“产品描述”(“txtProductDesc”)行,该行必须拆分并且需要放在 Excel 中的“txtProductNameBundle”下,它可能是 1 行或以上到 6. 我的 for 循环可以成功地将所有行(没有产品描述)准确地发送到我需要它们的地方。这是问题所在,我知道如何使用“txtProductDesc”执行“拆分字符串”,因为该文本可能相当长,甚至可以将其发送到 Excel,但我不知道如何添加循环以将其放置在“产品”之后姓名”。 Excel 工作表是一个模板,因此必须为要发送的尽可能多的信息行插入行。
int StartBundleRow = 11; // row 11 is where I start to insert the dynamic controls
string rowIndent = " "; // adds spaces to the beginning of the text
string DescriptionSplit = frmProposal.ProdDesc.Text;
for (int BndlRow = 0; BndlRow < bundleRows; BndlRow++)
{
worksheet.Rows[StartBundleRow].Insert();
worksheet.Rows[StartBundleRow].Font.Size = 14; //********Excel formatting*********
worksheet.Cells[StartBundleRow, "E"].Font.Bold = true;
worksheet.Rows[StartBundleRow].Interior.Color = ColorTranslator.ToOle(Color.White);
worksheet.Columns["A"].Interior.Color = ColorTranslator.ToOle((Color)cc.ConvertFromString("#808080"));
worksheet.Columns["J:XFD"].Interior.Color = ColorTranslator.ToOle((Color)cc.ConvertFromString("#808080"));
worksheet.Rows[StartBundleRow].HorizontalAlignment = XlHAlign.xlHAlignLeft;
worksheet.Cells[StartBundleRow, "C"].Interior.Color = ColorTranslator.ToOle((Color)cc.ConvertFromString("#49176D"));
worksheet.Cells[StartBundleRow, "D"].value = srcBundlePanel.Controls["txtQtyBundle" + BndlRow].Text;
//(product name below)
worksheet.Cells[StartBundleRow, "E"].value = srcBundlePanel.Controls["txtProductNameBundle" + BndlRow].Text;
//(this is where I need to insert the split string of product description)
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;
}
** BELOW IS MY SAMPLE STAND ALONE CODE FOR SPLITTING THE STRING INTO 3 ROWS **
worksheet.Cells[11, "E"].Value = rowIndent + DescriptionSplit.Substring(0, DescriptionSplit.IndexOf("|")).Trim();
worksheet.Cells[12, "E"].Value = rowIndent + DescriptionSplit.Substring(DescriptionSplit.IndexOf("|") + 1,
DescriptionSplit.IndexOf("|")).Trim();
worksheet.Cells[13, "E"].Value = rowIndent + DescriptionSplit.Substring(DescriptionSplit.LastIndexOf("|") + 1,
DescriptionSplit.Length - DescriptionSplit.LastIndexOf("|") - 1).Trim();
【问题讨论】:
标签: c#