C#将字符串数组拆分为Excel

本文关键字:Excel 拆分 数组 字符串 | 更新日期: 2023-09-27 18:25:44

我之前有一个类似的问题,但我仍然停留在实际的解决方案上。我正在将动态文本框中的文本行发送到Excel,这些信息来自数据库。在我发送的行中,"txtProductNameBundle",有时还有一个"产品描述"("txtPoductDesc")行,该行必须拆分,并且需要位于Excel中的"txtProjectNameBundle"下,可以是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#将字符串数组拆分为Excel

如果每个部分都用一个管道字符分隔,则可以简化描述字符串的拆分。

string[] descriptionParts = DescriptionSplit.Split('|');

要插入行,可以使用简单的for循环:

for(int i = 0; i < descriptionParts.Length; i++) 
{
    worksheet.Cells[StartBundleRow + i, "E"].Value = 
        rowIndent + descriptionParts[i].Trim();
}

您可能还想用下面的代码替换最后一行,以根据用于描述的行数调整下一个捆绑包的行偏移:

worksheet.Cells[StartBundleRow,"H"].value = 
    srcBundlePanel.Controls["txtProposedPriceBundle" + BndlRow].Text;
StartBundleRow += descriptionParts.Length;