使用c#error写入excel中的单元格

时间:2012-12-15 13:10:35

标签: c# excel

我在使用c#写入excel中的单元格时出现错误“没有足够的存储空间来完成此操作”

我知道excel表格不允许我们每个单元格写入超过32,767个字符。 我每个单元写32,000个字符。 它允许我在第1列中写入前3个单元格,但对于第4列,它会抛出上述错误。这是什么原因?任何想法?

xml_txt_length的总长度为2,65,000。

我在下面的评论中通过 * ** * *** 标记了代码,我收到错误。 我的代码如下:

        if (xml_txt_length > 32000)  // checking length of text is more than 32,000. if yes than need to split it to write in cell
        {
            int counter = 0; // used to multiply to 32,000
            while (true)
            {
                if (counter == 0)
                {
                    cell_string = xmlText.Substring(0, 32000);
                    xml_string += cell_string;
                    // writing to cell for column1
                    oSheet3.Cells[counter + 1, 1] = cell_string;
                }

                else
                {
                    // if taken 32,000 characters exceeds the end length of string then go into this condition and take actual final position.  
                    if ((32000 * counter) + 32000 >= xml_txt_length) 
                    {
                        // below substring taking start position and up to end character of string instead of putting directly last 32,000th character position

                        cell_string = xmlText.Substring(32000 * counter, Convert.ToInt32(xml_txt_length - (32000 * counter)));

                        xml_string += cell_string;

                        //writing to cell for column 1
                        oSheet3.Cells[counter + 1, 1] = cell_string;
                        break;
                    }
                    else
                    {
                        // taking the start and upto 32,000 characters from string
                        cell_string = xmlText.Substring(32000 * counter, 32000);
                        xml_string += cell_string;

                        // writing to cell for column 1
                        // ********************************************************
                        // **** HERE I AM GETTING ERROR FOR CELL 4 IN COLUMN 1 ****

                        oSheet3.Cells[counter + 1, 1] = cell_string;
                        cell_string = string.Empty;
                    }
                }

                if (counter >= Math.Floor(xml_txt_length / 32000))
                    break;
                counter++;
            }
        }
        else
            oSheet3.Cells[1, 1] = xmlText;

0 个答案:

没有答案