Generating a Large Excel File from Code

This is one way of generating a large Excel file with random data from code:

  1. Fire up Visual Studio 2013.
  2. Create a new Excel 2013 Workbook project.
  3. Use the following code:
        public partial class ThisWorkbook
        {
            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                var random = new Random();
                var activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
                for (int i = 1; i <= 1024*30; i++)
                {
                    byte[] bytes = new byte[768];
                    random.NextBytes(bytes);
                    string s = Convert.ToBase64String(bytes);
                    var range = activeWorksheet.get_Range("A" + i);
                    range.Value = s;
                }
            }
    
    		// ...
    	}
    

    It will create 1024 * 30 rows of 1024 characters each.

  4. Press F5 to run.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s