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?

kPuck
  • 127
  • 3
  • 12
  • `rng = ws.Cells[1,1].Resize(ftmp.Length, 1)` should work also to reference a table of cells. – John Alexiou Oct 19 '21 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[,]`. – John Alexiou Oct 19 '21 at 22:33

2 Answers2

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;
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
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];
}
Hazrelle
  • 758
  • 5
  • 9
  • 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. – kPuck Oct 19 '21 at 12:32