c# - 使用 Epplus 在 C# 中对 Excel 进行 SQL 查询

标签 c# sql sql-server excel 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 工作表的第一行写入架构中的标题

关于c# - 使用 Epplus 在 C# 中对 Excel 进行 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66299978/

相关文章:

mysql - 按日期参数查询数据

sql-server - SQL Server 上的语法错误

c# - 当目标是 ImageBrush.ImageSource 时,TemplateBinding 失败

c# - 如何找到黑色像素位置

SQL Server 作业/时间表 - 美国与英国夏令时调整

sql - 为什么交叉应用会使查询变慢?

c# - 生成一组(相对)短的 "invitation codes"的好方法是什么

c# - asp.net mvc 返回视频的 Action 结果

c# - For循环中的日期时间

sql - 我可以在较大的SQL查询中只能将一列加入或将多行转换为额外的列吗?