SQL 使用 Epplus 在 C# 中查询 Excel

SQL Query to Excel in C# using Epplus

我一直在为这个问题苦苦挣扎。 我正在尝试将 sql 查询中的字符串和列名写入 excel 文档。 但我只写了行。我如何修复代码以写入所有行和列?

            var Template = new FileInfo(@"C:\Temp\XLS\New.xlsx");
            var xlPackage = new ExcelPackage(Template);
            var wsCards = xlPackage.Workbook.Worksheets.Add(NAME_WORKSHEET);
            using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
            {
                sqlConn.Open();
                using (SqlCommand command = new SqlCommand(SQL_WORKSHEET, sqlConn))
                {
                    var reader = command.ExecuteReader();
                    int row = 1;

                    DataTable schemaTable = reader.GetSchemaTable();
                    foreach (DataRow rw in schemaTable.Rows)
                    {
                        foreach (DataColumn column in schemaTable.Columns)
                        {
                            if (column.ColumnName == "ColumnName")
                            {
                                wsCards.Cells["A1"].Value = rw[column];
                            }
                        }
                    }
                    while (reader.Read())
                    {
                        row++;
                        for (int col = 1; col <= reader.FieldCount; col++)
                        {
                            wsCards.Cells[row, col].Value = reader.GetValue(col - 1);
                        }
                    }
                    xlPackage.SaveAs(Template);
                    xlPackage.Dispose();
                }
            }

            var Template = new FileInfo(@"C:\Temp\XLS\New.xlsx");
            var xlPackage = new ExcelPackage(Template);
            var wsCards = xlPackage.Workbook.Worksheets.Add(NAME_WORKSHEET);
            using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
            {
                sqlConn.Open();
                using (SqlCommand command = new SqlCommand(SQL_WORKSHEET, sqlConn))
                {
                    var reader = command.ExecuteReader();
                    int row = 1,col=1;


                    DataTable schemaTable = reader.GetSchemaTable();
                    //foreach (DataRow rw in schemaTable.Rows)
                    //{
                        // Write the headers to the first row
                        foreach (DataColumn column in schemaTable.Columns)
                        {
                             // Condition Will only be required if you want to write 
                            // specific column names
                            //if (column.ColumnName == "ColumnName")
                            //{ 
                            
                                wsCards.Cells[1,col].Value = rw[column].ColumnName;
                                col++;
                            //}
                        }
                    //}
                    while (reader.Read())
                    {
                        row++;
                        for ( col = 1; col <= reader.FieldCount; col++)
                        {
                            wsCards.Cells[row, col].Value = reader.GetValue(col - 1);
                        }
                    }
                    xlPackage.SaveAs(Template);
                    xlPackage.Dispose();
                }
            }

此代码将从 excel sheet

的第一行的模式写入 headers