C#将String拆分为Excel

时间:2015-11-21 22:18:53

标签: c# string excel

我一直在阅读Split字符串。我将数据从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;

1 个答案:

答案 0 :(得分:1)

试试这样:

        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();

您可以编写生成的&#34;行&#34;进入一个数组或直接进入你的细胞。我只是使用Stringbuilder来轻松检查结果......