【问题标题】:C# split string array to ExcelC# 将字符串数组拆分为 Excel
【发布时间】: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#


    【解决方案1】:

    描述字符串的拆分可以更简单,前提是每个部分用管道字符分隔。

    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;    
    

    【讨论】:

    • 抱歉,我误解了您示例中描述行的固定索引。索引必须像其他行一样偏移。编辑答案。
    • 当你说“替换最后一行”时,我有点困惑,抱歉。 for 循环有效,但我不知道如何将它插入到另一组“startbundlerow”代码中,这样我每次都可以重复它。目前它在此之外工作,但只循环单行。我一直在编辑并尝试调整,但没有成功。我需要以某种方式使用“foreach”吗?
    • 你不能只插入for循环而不是以“//(这是哪里”开头的行吗?替换最后一行,我的意思是你把我的两个新行放在而不是现有 for 循环中的最后一行。我只是更改了 StartBundleRow 的递增方式。
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2012-02-22
    相关资源
    最近更新 更多