簡體   English   中英

C#將字符串數組拆分為Excel

[英]C# split string array to Excel

我之前有一個與此類似的問題,但我仍然對實際解決方案感到困惑。 我正在將文本行從動態文本框發送到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();

如果每個部分都由管道字符分隔,則描述字符串的拆分可以變得更加簡單。

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;    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM