这是我的方法的摘录,该方法将
DataTable(
dt变量)转换为数组,然后将该数组写入
Range工作表(
wshvar)中。您还可以将
topRow变量更改为要放置字符串数组的任何行。
object[,] arr = new object[dt.Rows.Count, dt.Columns.Count];for (int r = 0; r < dt.Rows.Count; r++){ DataRow dr = dt.Rows[r]; for (int c = 0; c < dt.Columns.Count; c++) { arr[r, c] = dr[c]; }}Excel.Range c1 = (Excel.Range)wsh.Cells[topRow, 1];Excel.Range c2 = (Excel.Range)wsh.Cells[topRow + dt.Rows.Count - 1, dt.Columns.Count];Excel.Range range = wsh.get_Range(c1, c2);range.Value = arr;当然,您不需要
DataTable像我一样使用中间体,代码摘录只是演示如何在单个调用中将数组写入工作表。



