在Excel(2010)电子表格中填充SQL Server数据

时间:2017-03-16 18:26:12

标签: c# excel odbc vsto

我试图将从SQL Server中提取的数据填充到Excel 2010中。下面的代码运行正常,但难点在于我没有以编程方式创建Excel电子表格,它存在并且我存在通过用C#编写的Excel中的插件发出数据请求。 即使我将光标设置为A10单元格,Excel也会从第一个单元格中填写数据并覆盖标题(已存在)。请帮忙解决。 代码:

OdbcConnection cnn;
                cnn = new OdbcConnection(azureConn);
                using (OdbcCommand command = cnn.CreateCommand())
                {
                    command.CommandText = "{call sp_Get_Excel_Data(?,?,?,?,?,?,?,?)}";
                    command.Parameters.AddWithValue("@StartDate", startDate);
                    command.Parameters.AddWithValue("@EndDate", endDate);
                    command.Parameters.AddWithValue("@startTime", startTime);
                    command.Parameters.AddWithValue("@endTime", endTime);
                    command.Parameters.AddWithValue("@smp", smp);
                    command.Parameters.AddWithValue("@Reg", reg);
                    command.Parameters.AddWithValue("@event", events);
                    command.Parameters.AddWithValue("@userId", userId);

                    cnn.Open();

                    //DataTable
                    OdbcDataAdapter adapter = new OdbcDataAdapter(command);

                    //DataSet
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);

                    //Cast to DataTable
                    DataTable dataTable = ds.Tables[0];

                    string[] colNames = new string[dataTable.Columns.Count];
                    int col = 0;

                    foreach (DataColumn dc in dataTable.Columns)
                        colNames[col++] = dc.ColumnName;

                    w = this.Application.ActiveWorkbook;
                    ws = (Worksheet)w.ActiveSheet;

                    Range hdrRow = (Range)ws.Rows[9];

                    hdrRow.Value = colNames;
                    hdrRow.Font.Bold = true;
                    hdrRow.VerticalAlignment = XlVAlign.xlVAlignCenter;

                    //Position the cursor
                    var range = ws.get_Range("A10");
                    range.Select();

                    //Inserting the Column and Values into Excel file
                    string data = null;
                    int i = 0;
                    int j = 0;

                    for (i = 0; i <= dataTable.Rows.Count - 1; i++)
                    {
                        for (j = 0; j <= dataTable.Columns.Count - 1; j++)
                        {
                            data = dataTable.Rows[i].ItemArray[j].ToString();
                            ws.Cells[i + 2, j + 1] = data;

                        }
                    }

1 个答案:

答案 0 :(得分:1)

讨厌回答我自己的问题,但这是解决方案(具有优化的性能):

                    int column = 1;
                    foreach (DataColumn c in dataTable.Columns)
                    {
                        //Ninth row, starting from the first cell
                        ws.Cells[10, column] = c.ColumnName;
                        column++;
                    }

                    // Create a 2D array with the data from the data table
                    int i = 0;
                    string[,] data = new string[dataTable.Rows.Count, dataTable.Columns.Count];
                    foreach (DataRow row in dataTable.Rows)
                    {
                        int j = 0;
                        foreach (DataColumn c in dataTable.Columns)
                        {
                            data[i, j] = row[c].ToString();
                            j++;
                        }
                        i++;
                    }

                    // Set the range value to the 2D array in Excel (10th row, starting from 1st cell)
                    ws.Range[ws.Cells[11, 1], ws.Cells[dataTable.Rows.Count + 11, dataTable.Columns.Count]].Value = data;