C#将string拆分为Excel

我一直在阅读拆分string。 我正在将数据从C#发送到Excel,并且其中一些文本可能会相当长。 因此,不要使用自动换卷或Excel自动调整。 我希望数据在某个时间点被打破,并继续到它下面的行。可以做到这一点吗? ProdDescription是这里的目标领域。 以下是我用来发送基本数据的代码:

worksheet.Cells[3, "E"].Value = txtCustomer.Text; worksheet.Cells[4, "E"].Value = txtDate.Text; worksheet.Cells[5, "E"].Value = cboTerms.Text; worksheet.Cells[6, "E"].Value = txtProposalID.Text; worksheet.Cells[10, "D"].value = frmProposal.QtyMaintxt.Text; worksheet.Cells[10, "E"].value = frmProposal.ProdName.Text; worksheet.Cells[11, "E"].value = frmProposal.ProdDescription.Text;** worksheet.Cells[10, "F"].value = frmProposal.ListPrice.Text; worksheet.Cells[10, "G"].value = frmProposal.MaxDiscount.Text; 

试试像这样:

  string s = "This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. "; s += "This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. "; s += "This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. This is a rather long text. "; var words = s.Split(new char[] { ' ' }); int maxLineCount = 35; var sb=new System.Text.StringBuilder(); string part=words[0]; int i=1; while (true) { if (i >= words.Length) break; if ((part + " " + words[i]).Length < maxLineCount) part += " " + words[i]; else { sb.AppendLine(part); part = words[i]; } i++; } var result = sb.ToString(); 

您可以将生成的“行”写入数组或直接写入您的单元格。 我只是使用Stringbuilder轻松检查结果…