Hi anonymous user, I think this issue is more related to compatibility of Npgsql with OpenRowSet function. I was trying to search through the documentation of Npgsql package but could not find any details where in we can use it with OpenRowset.
However there is simple workaround to achieve this is firstly we can load the data from database to a dataset/datatable and then dump that data into excel. Below is one example on the same:
using (var conn = new NpgsqlConnection(connString))
{
string szFilePath = "yourfilepath";
string szConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + szFilePath + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
var command = new NpgsqlCommand("SELECT * FROM yourtable", conn);
NpgsqlDataAdapter npgsql = new NpgsqlDataAdapter(command);
DataSet ds = new DataSet();
npgsql.Fill(ds);
conn.Close();
using (OleDbConnection connOledb = new OleDbConnection(szConnectionString))
{
connOledb.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connOledb;
foreach(DataRow dr in ds.Tables[0].Rows)
{
cmd.CommandText = @"Insert into [Sheet1$] (id,name,quantity) VALUES ('" + dr[0] + "'," + dr[1] + "','" + dr[2] + "')";
cmd.ExecuteNonQuery();
}
}
}
Reference Article: Read Write Excel file with OLEDB in C#
Please let me know if this helps or else we can discuss further.
if answer helps, please mark it as 'Accept Answer'