I've written a program to generate an Excel Workbook in C# using OLEDB. Mostly working, but one thing I've noticed is that if I output only 1 column, it leaves a blank row after the header.
Any ideas?
I've written a program to generate an Excel Workbook in C# using OLEDB. Mostly working, but one thing I've noticed is that if I output only 1 column, it leaves a blank row after the header.
Any ideas?
Here's some sample code that I'm using, and I'm not seeing the issue. I'm creating a new workbook/spreadsheet and then populating just one row and column.
Can you post your code and we can see what the differences are?
static void Main(string[] args)
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\SOTest2.xls;Extended Properties=""Excel 8.0;HDR=NO;""";
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = connection.CreateCommand())
{
connection.Open(); //open the connection
command.CommandText = "CREATE TABLE [Sheet1] (F1 number);";
command.ExecuteNonQuery(); //create the sheet before doing any inserts
command.CommandText = "INSERT INTO [Sheet1$] (F1) VALUES(4)";
command.ExecuteNonQuery(); //now insert a row into the sheet
}
}
}
I also don't see a blank row if I insert a single row with multiple columns:
command.CommandText = "CREATE TABLE [Sheet1] (F1 number, F2 char(255), F3 char(128))";
command.ExecuteNonQuery(); //create the sheet before doing any inserts
command.CommandText = "INSERT INTO [Sheet1$] (F1, F2, F3) VALUES(4,\"Tampa\",\"Florida\")";
command.ExecuteNonQuery(); //now insert a row into the sheet
Hope this helps!