0

I have a [,] string array that is 40 rows by 40 column items. I am trying to write them into an excel sheet but the output keeps writing the same word on the first row of every column. What am I doing wrong?

   public static void writeToFile(this string [,] result)
    {

        try
        {
            //open instance of excel 
            Microsoft.Office.Interop.Excel.Application app = null;


            app = new Excel.Application();
            app.Workbooks.Add();
            Excel._Worksheet sheet = app.ActiveSheet;

            sheet.Name = "Sheet 1";
            int resultCount = result.Length;


                for (int i = 0; i < 40; i++)
                {
                    for (int j = 0; j < 40; j++ )
                    {

                        sheet.Cells[i].Value = result[i,j];
                        j++;
                    }
                    i++;
                }
1
  • sheet.Cells[i, j].Value = result[i,j]; It's not clear why you're using i++ and j++ within your loops - you don't need that. Also - indexes passed to Cells[] should begin at 1: there's no Cells[0, 0], the first cell on the sheet is Cells[1, 1] Commented Aug 31, 2015 at 3:36

1 Answer 1

1

If you got an array with rows at first dimension and columns at second dimension, you can simply write:

sheet.Range[sheet.Cells[1,1], sheet.Cells[40,40]].Value = result;
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.