C#数据表到Excel仅填充2列

时间:2019-07-11 16:28:49

标签: c# excel

我试图用DataTable中的数据填充Excel电子表格,由于某种原因,它仅填充了第5列和第10列。

我尝试将数据从DataTable打印到控制台,并且所有数据都出现在控制台中。

public static void DtToExcel(System.Data.DataTable dt, Excel._Worksheet sheet, int firstRow, int firstCol, int lastRow, int lastCol)
        {
            Excel.Range top = sheet.Cells[firstRow, firstCol];
            Excel.Range bottom = sheet.Cells[lastRow, lastCol];
            Excel.Range all = sheet.Range[top,bottom];
            string[,] arrayDT = new string[dt.Rows.Count, dt.Columns.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                for (int j = 0; j < dt.Columns.Count; j++)
                    arrayDT[i, j] = dt.Rows[i][j].ToString();
            all.Value = arrayDT;
        }

DtToExcel(table, objSheet, 4, 1, table.Rows.Count+4, 18);

0 个答案:

没有答案
相关问题