I written myself some small generic class to export any dataset to an
excel spreadsheet:

public static void Export(DataSet data, String excelFileName)
{
System.IO.File.Delete(excelFileName);
string strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
+ System.IO.Path.GetDirectoryName(excelFileName) + @"\" +
System.IO.Path.GetFileName(excelFileName)
+ @";Extended Properties='Excel 8.0;HDR=YES'";

using (System.Data.OleDb.OleDbConnection objConn = new
System.Data.OleDb.OleDbConnection(strConnectionString))
using (System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand("", objConn))
{
objConn.Open();
foreach (DataTable dt in data.Tables)
{
cmd.CommandText = "CREATE TABLE [" + dt.TableName + "] (";
String valueNames = "(";
Boolean first = true;
foreach (DataColumn dc in dt.Columns)
{
if (!first)
{
cmd.CommandText += ",\r\n";
valueNames += ", ";
}
cmd.CommandText += " [" + dc.ColumnName + "] NVARCHAR(100)";
valueNames += " [" + dc.ColumnName + "]";
first = false;
}
cmd.CommandText += ")";
valueNames += ")";
cmd.ExecuteNonQuery();
foreach (DataRow dr in dt.Rows)
{
String values = "(";
first = true;
foreach (DataColumn dc in dt.Columns)
{
if (!first)
values += ", ";
values += " '" + dr[dc] + "'";
first = false;
}
values += ")";
cmd.CommandText = "INSERT INTO [" + dt.TableName + "$] " +
valueNames + " VALUES " + values;
cmd.ExecuteNonQuery();
}
}
}
}

This does work quite ok for my uses, the only problem is: After export
the first character in every single cell of the excel spreadsheet is
the quotation mark '
Somehow the export does not strip the leading quotation marks from my
values - anyone can give me a hint how I do get rid of these?

thanks,
Sam

Re: export dataset to excel by Paul

Paul
Tue Jun 28 09:12:20 CDT 2005

On 28 Jun 2005 03:17:13 -0700, "Sam Jost" <samjost@web.de> wrote:

¤ I written myself some small generic class to export any dataset to an
¤ excel spreadsheet:
¤
¤ public static void Export(DataSet data, String excelFileName)
¤ {
¤ System.IO.File.Delete(excelFileName);
¤ string strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
¤ Source="
¤ + System.IO.Path.GetDirectoryName(excelFileName) + @"\" +
¤ System.IO.Path.GetFileName(excelFileName)
¤ + @";Extended Properties='Excel 8.0;HDR=YES'";
¤
¤ using (System.Data.OleDb.OleDbConnection objConn = new
¤ System.Data.OleDb.OleDbConnection(strConnectionString))
¤ using (System.Data.OleDb.OleDbCommand cmd = new
¤ System.Data.OleDb.OleDbCommand("", objConn))
¤ {
¤ objConn.Open();
¤ foreach (DataTable dt in data.Tables)
¤ {
¤ cmd.CommandText = "CREATE TABLE [" + dt.TableName + "] (";
¤ String valueNames = "(";
¤ Boolean first = true;
¤ foreach (DataColumn dc in dt.Columns)
¤ {
¤ if (!first)
¤ {
¤ cmd.CommandText += ",\r\n";
¤ valueNames += ", ";
¤ }
¤ cmd.CommandText += " [" + dc.ColumnName + "] NVARCHAR(100)";
¤ valueNames += " [" + dc.ColumnName + "]";
¤ first = false;
¤ }
¤ cmd.CommandText += ")";
¤ valueNames += ")";
¤ cmd.ExecuteNonQuery();
¤ foreach (DataRow dr in dt.Rows)
¤ {
¤ String values = "(";
¤ first = true;
¤ foreach (DataColumn dc in dt.Columns)
¤ {
¤ if (!first)
¤ values += ", ";
¤ values += " '" + dr[dc] + "'";
¤ first = false;
¤ }
¤ values += ")";
¤ cmd.CommandText = "INSERT INTO [" + dt.TableName + "$] " +
¤ valueNames + " VALUES " + values;
¤ cmd.ExecuteNonQuery();
¤ }
¤ }
¤ }
¤ }
¤
¤ This does work quite ok for my uses, the only problem is: After export
¤ the first character in every single cell of the excel spreadsheet is
¤ the quotation mark '
¤ Somehow the export does not strip the leading quotation marks from my
¤ values - anyone can give me a hint how I do get rid of these?


That is the way the Excel ISAM driver was designed to work. It adds the apostrophe to discriminate
between text and numeric values. I don't believe it shows up in the cell, just the formula bar.


Paul
~~~~
Microsoft MVP (Visual Basic)

Re: export dataset to excel by Sam

Sam
Tue Jun 28 09:43:11 CDT 2005

My bad - is there any way to get rid of this 'Feature by design' so I
don't have these apostrophs in every cell?
Maybe using a different type instead of NVARCHAR for the field,
anything?

Thanks,
Sam

Paul Clement schrieb:
> On 28 Jun 2005 03:17:13 -0700, "Sam Jost" <samjost@web.de> wrote:
>
> =A4 I written myself some small generic class to export any dataset to an
> =A4 excel spreadsheet:
> =A4
> =A4 public static void Export(DataSet data, String excelFileName)
> =A4 {
> =A4 System.IO.File.Delete(excelFileName);
> =A4 string strConnectionString =3D @"Provider=3DMicrosoft.Jet.OLEDB.4.0=
;Data
> =A4 Source=3D"
> =A4 + System.IO.Path.GetDirectoryName(excelFileName) + @"\" +
> =A4 System.IO.Path.GetFileName(excelFileName)
> =A4 + @";Extended Properties=3D'Excel 8.0;HDR=3DYES'";
> =A4
> =A4 using (System.Data.OleDb.OleDbConnection objConn =3D new
> =A4 System.Data.OleDb.OleDbConnection(strConnectionString))
> =A4 using (System.Data.OleDb.OleDbCommand cmd =3D new
> =A4 System.Data.OleDb.OleDbCommand("", objConn))
> =A4 {
> =A4 objConn.Open();
> =A4 foreach (DataTable dt in data.Tables)
> =A4 {
> =A4 cmd.CommandText =3D "CREATE TABLE [" + dt.TableName + "] (";
> =A4 String valueNames =3D "(";
> =A4 Boolean first =3D true;
> =A4 foreach (DataColumn dc in dt.Columns)
> =A4 {
> =A4 if (!first)
> =A4 {
> =A4 cmd.CommandText +=3D ",\r\n";
> =A4 valueNames +=3D ", ";
> =A4 }
> =A4 cmd.CommandText +=3D " [" + dc.ColumnName + "] NVARCHAR(100)=
";
> =A4 valueNames +=3D " [" + dc.ColumnName + "]";
> =A4 first =3D false;
> =A4 }
> =A4 cmd.CommandText +=3D ")";
> =A4 valueNames +=3D ")";
> =A4 cmd.ExecuteNonQuery();
> =A4 foreach (DataRow dr in dt.Rows)
> =A4 {
> =A4 String values =3D "(";
> =A4 first =3D true;
> =A4 foreach (DataColumn dc in dt.Columns)
> =A4 {
> =A4 if (!first)
> =A4 values +=3D ", ";
> =A4 values +=3D " '" + dr[dc] + "'";
> =A4 first =3D false;
> =A4 }
> =A4 values +=3D ")";
> =A4 cmd.CommandText =3D "INSERT INTO [" + dt.TableName + "$] " +
> =A4 valueNames + " VALUES " + values;
> =A4 cmd.ExecuteNonQuery();
> =A4 }
> =A4 }
> =A4 }
> =A4 }
> =A4
> =A4 This does work quite ok for my uses, the only problem is: After export
> =A4 the first character in every single cell of the excel spreadsheet is
> =A4 the quotation mark '
> =A4 Somehow the export does not strip the leading quotation marks from my
> =A4 values - anyone can give me a hint how I do get rid of these?
>
>
> That is the way the Excel ISAM driver was designed to work. It adds the a=
postrophe to discriminate
> between text and numeric values. I don't believe it shows up in the cell,=
just the formula bar.
>=20
>=20
> Paul
> ~~~~
> Microsoft MVP (Visual Basic)


Re: export dataset to excel by Paul

Paul
Wed Jun 29 08:23:50 CDT 2005

On 28 Jun 2005 07:43:11 -0700, "Sam Jost" <samjost@web.de> wrote:

¤ My bad - is there any way to get rid of this 'Feature by design' so I
¤ don't have these apostrophs in every cell?
¤ Maybe using a different type instead of NVARCHAR for the field,
¤ anything?
¤

I seem to recall that if the Excel driver is not used to create the Worksheet then the apostrophe is
not included upon insert. Of course if you're using a Worksheet created in Excel then it must have
column headers to perform the Insert.

I also seem to remember that the behavior was specific to ADO/ADO.NET and not DAO.


Paul
~~~~
Microsoft MVP (Visual Basic)