0

I want to split one large Excel file to few smaller and accessible files.

I already tried to use this code but the files are not accessible:

using (System.IO.StreamReader sr = new System.IO.StreamReader("path"))
{
    int fileNumber = 0;

    while (!sr.EndOfStream)
    {
        int count = 0;

        using (System.IO.StreamWriter sw = new System.IO.StreamWriter("other path" + ++fileNumber + ".xlsx"))
        {
            sw.AutoFlush = true;

            while (!sr.EndOfStream && ++count < 20000)
            {
                sw.WriteLine(sr.ReadLine());
            }
        }
    }
}

Any ideas? thanks.

3
  • 1
    arbitrarily splitting a fie into chunks has purpose but what did you expect would be able to read it? excel has a format- if the format isnt valid it wont see it as a file.. Commented Sep 26, 2017 at 10:20
  • 2
    This is like trying to create two sedans out of a lorry, with only a saw. Commented Sep 26, 2017 at 10:23
  • It is do able with Aspose.Cells. But Aspose.Cells will read your original Excel file and create new Excel files for every portion or chunk of data. Commented Sep 27, 2017 at 14:16

1 Answer 1

1

Files, other than text files, don't work this way. You can't simply cut at a certain point and obtain a working copy.

As for Excel files, you may look into the following tutorial, which illustrates how to automate Excel from C#:

https://support.microsoft.com/en-us/help/302084/how-to-automate-microsoft-excel-from-microsoft-visual-c--net

Basically, what you want to do is open your large Excel file, decide where you want to split it (every n rows, every n sheets and so on), read each portion and write into a newly created xlsx.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.