0

If I do this then I only get the first value of the float array in every row in the excel column A:

        float[] ftmp // my float array

        Excel.Range rng = ws.Range[ws.Cells[1, 1], ws.Cells[ftmp.Count(), 1]]; 

        rng.Value = ftmp;

I understand that it has something to do with transposing based on similar questions.

Writing an array to a range. Only getting first value of array

but I can't find the transpose function in neither interop library or the worksheet function library?

2
  • rng = ws.Cells[1,1].Resize(ftmp.Length, 1) should work also to reference a table of cells. Commented Oct 19, 2021 at 12:55
  • For one Excel accepts double values in the form of a 2D array of object (Variant in VBA). So you need to convert float[] to double[] and then store in object[,]. Commented Oct 19, 2021 at 22:33

2 Answers 2

1

This works for me

float[] fvals = ...

dynamic xvals = ws.Range[ws.Cells[1, 1], ws.Cells[fvals.Length, 1]].Value2;

for (int i = 0; i < fvals.Length; i++)
{
    xvals[i+1, 1] = fvals[i];
}

ws.Range[ws.Cells[1, 1], ws.Cells[fvals.Length, 1]].Value2 = xvals;
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure you can transpose an array as is to cells. But you can loop over.

for(int i = 0; i < ftmp.Length; i++) {
  Excel.Range rng = ws.Range[ws.Cells[i+1, 1], ws.Cells[i+i, 1]];
  rng.Value = ftmp[i];
}

1 Comment

Sry I forgot to tell that I already have this solution i.e looping over the array and populate the spreadsheet cell by cell but it's very slow. i'm expermenting to see if this would be a better solution.

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.