0

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

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

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;
4

0 回答 0