Hello all,
I have written a class that exports all the rows of a table in a dataset to an excel file, I use an oledb connection to do this.
now it works no problem but I wonder if I can speed it up, this is where you specialist come in, this is my code
this one creates the excel file and the first cells ( headers )
this inserts the records into the exsisting excel file.
the BVQuery.excel.executenonquery just executes the sql string given on a oledb connection to the excel file
All comment apreciated
Greetz to you all
I have written a class that exports all the rows of a table in a dataset to an excel file, I use an oledb connection to do this.
now it works no problem but I wonder if I can speed it up, this is where you specialist come in, this is my code
this one creates the excel file and the first cells ( headers )
Code:
try
{
if(System.IO.File.Exists(System.IO.Path.GetFullPath(_FileName)))
System.IO.File.Delete(System.IO.Path.GetFullPath(_FileName));
DT = _ds.Tables[0];
StringBuilder CreateTable = new StringBuilder("CREATE TABLE ");
CreateTable.Append(_ds.DataSetName);
CreateTable.Append(" (");
foreach(DataColumn col in DT.Columns)
{
CreateTable.Append(col.ColumnName);
CreateTable.Append(" " + DBDataType(col.DataType,col.MaxLength) + ", ");
}
CreateTable.Replace(",",")",CreateTable.Length - 2,1);
CreateTable.Append(";");
Console.WriteLine(CreateTable.ToString());
Console.WriteLine( BVQuery.Excel(_FileName).ExecuteNonQuery(CreateTable.ToString()) );
return true;
}
catch(OleDbException)
{
return false;
}
this inserts the records into the exsisting excel file.
Code:
StringBuilder InsertString = new StringBuilder();
foreach(DataRow row in DT.Rows)
{
if(InsertString.Length > 0)
InsertString.Remove(0,InsertString.Length);
InsertString.Append("INSERT INTO ");
InsertString.Append(_ds.DataSetName);
InsertString.Append(" (");
foreach(DataColumn col in DT.Columns)
{
InsertString.Append(col.ColumnName + ",");
}
InsertString.Replace(",",")",InsertString.Length - 1,1);
InsertString.Append(" VALUES (");
for(int i = 0;i<row.ItemArray.Length;i++)
{
InsertString.Append("" + row.ItemArray.GetValue(i).ToString() + ",");
}
InsertString.Replace(",",")",InsertString.Length -1,1);
InsertString.Append(";");
Console.WriteLine(InsertString.ToString());
Console.WriteLine(BVQuery.Excel(_FileName).ExecuteNonQuery(InsertString.ToString()));
}
the BVQuery.excel.executenonquery just executes the sql string given on a oledb connection to the excel file
All comment apreciated
Greetz to you all