简体   繁体   English

按长度将字符串拆分为Excel

[英]Split string by length to Excel

I am currently sending and splitting long lines of data to Excel. 我目前正在发送长行数据并将其拆分到Excel。 Each split prints in a new row. 每个拆分均在新行中打印。 I want to change this from splitting at a pipe symbol in the existing data to splitting at a character length of 85, but , there are chances that at character 85 it may split a word in two. 我想将其从现有数据中的管道符号拆分为字符长度85拆分,但是,有可能在字符85中将一个单词拆分为两个。 How would I tell it to split further into the data if is going to split an actual word. 如果要拆分实际单词,我将如何告诉它进一步拆分为数据。 I know if at 85 it should also find a space after. 我知道如果在85岁以后也应该找到一个空格。 I'm curious on what to add. 我很好奇要添加的内容。

// Add Description
      string DescriptionSplit = srcAddOnPanel.Controls["txtProductDescAddOn" + AddRow].Text;
      string[] descriptionParts = DescriptionSplit.Split('|');
      int i;
      for (i = 0; i <= descriptionParts.GetUpperBound(0); i++)
      {
          worksheet.Rows[currentRow].Insert();    //applies the description for the default bundle row
          worksheet.Rows[currentRow].Font.Bold = false;
          worksheet.Cells[currentRow, "E"].Value = rowIndent + descriptionParts[i].Trim();
          currentRow++;
      }

You could use this approach (Warning not fully tested) 您可以使用这种方法(警告尚未完全测试)

int x = 85;
int y = 0;
int currentRow = 0;

// Loop until you have at least 85 char to grab
while (x + y < DescriptionSplit.Length)
{
    // Find the first white space after the 85th char
    while (x + y < DescriptionSplit.Length && 
          !char.IsWhiteSpace(DescriptionSplit[x+y]))
        x++;
    // Grab the substring and pass it to Excel for the currentRow
    InsertRowToExcel(DescriptionSplit.Substring(y, x), currentRow);

    // Prepare variables for the next loop
    currentRow++;
    y = y + x + 1;
    x = 85;
}
// Do not forget the last block
if(y < DescriptionSplit.Length)
    InsertRowToExcel(DescriptionSplit.Substring(y), currentRow);
...

void InsertRowToExcel(string toInsert, int currentRow)
{
      worksheet.Rows[currentRow].Insert();    
      worksheet.Rows[currentRow].Font.Bold = false;
      worksheet.Cells[currentRow, "E"].Value = rowIndent + toInsert.Trim();
}

Here's a VBA version that seems to work. 这是一个似乎可行的VBA版本。 As suggested in my comment, it separates the string by spaces and then tests whether adding a word makes the length of the current line greater than the maximum (85). 正如我的评论中所建议的那样,它用空格分隔字符串,然后测试添加单词是否使当前行的长度大于最大值(85)。 As is usual with these kinds of things getting the last word to populate correctly is hard. 像往常一样,要使最后的单词正确填充很难。

If this works for you it should be simple enough to modify to C#. 如果这对您有效,那么将其修改为C#应该足够简单。 Let me know if that's not true: 让我知道是否不正确:

Sub ParseRows()
Const ROW_LENGTH As Long = 85
Dim Text As String
Dim Words As Variant
Dim RowContent As String
Dim RowNum As Long
Dim i As Long

Text = ActiveCell.Text
Words = Split(Text, " ")
RowNum = 2

For i = LBound(Words) To UBound(Words)
    If Len(RowContent & Words(i) & " ") > ROW_LENGTH Then
        ActiveSheet.Range("A" & RowNum).Value = RowContent
        RowNum = RowNum + 1
        If i = UBound(Words) Then
            RowContent = Words(i)
        Else
            RowContent = ""
        End If
    Else
        RowContent = RowContent & Words(i) & " "
    End If
Next i
If RowContent <> "" Then ActiveSheet.Range("A" & RowNum).Value = RowContent
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM