R
raj
Hi,
I'm trying to export a dataset to an excel sheet using the
following code. this code works but is inconsistent....is
something wrong with this code.
1) Alternative approach...is it possible to export the
dataset to excel using arrays. If so, how.
public void ExportToExcel(DataSet dsCost)
{
// Exporting the Dataset to an ExcelSpreadsheet.
Excel.Application objApplication= new
Excel.ApplicationClass();
try
{
Excel.Workbook objWorkBook;
Excel.Worksheet objWorkSheet;
objWorkBook = (Excel.Workbook)
(objApplication.Workbooks.Add(Missing.Value));
//objWorkBook = (Excel.Workbook)
(objApplication.Workbooks.Add(objApplication));
objWorkSheet = (Excel.Worksheet)
objWorkBook.ActiveSheet;
objWorkSheet.Name = "Dataset Data";
objApplication.WindowState =
Excel.XlWindowState.xlMaximized;
// Initialise the progress form.
frmProgress newProgress = new frmProgress();
newProgress.pbprogress.Minimum = 1;
newProgress.pbprogress.Value = 1;
newProgress.pbprogress.Step = 1;
newProgress.Show();
foreach(DataTable table in dsCost.Tables)
{
//Set Maximum to the total number of files to copy.
//int numRows = dataGrid1.BindingContext
[dataGrid1.DataSource, dataGrid1.DataMember].Count;
//MessageBox.Show("Number of rows = " +
numRows.ToString() );
newProgress.pbprogress.Maximum = table.Rows.Count;
int row = 1, column = 1;
// Need to export the column headers here.
foreach (DataColumn dcColumn in
table.Columns)
{
objWorkSheet.Cells[row, column] =
dcColumn.ColumnName.ToString();
column++;
}
row++;
foreach(DataRow r in table.Rows)
{
column = 1;
foreach (object o in r.ItemArray)
{
objWorkSheet.Cells[row, column] = o.ToString();
column++;
}
row++;
newProgress.pbprogress.PerformStep();
Application.DoEvents();
}
}
MessageBox.Show("Export Process has completed");
newProgress.Close();
objApplication.Visible = true;
objApplication.WindowState =
Excel.XlWindowState.xlMaximized;
}
catch (Exception e)
{
// Exception Handler
MessageBox.Show("Export process raised the error: " +
e.Message);
}
//objApplication.Quit();
}
I'm trying to export a dataset to an excel sheet using the
following code. this code works but is inconsistent....is
something wrong with this code.
1) Alternative approach...is it possible to export the
dataset to excel using arrays. If so, how.
public void ExportToExcel(DataSet dsCost)
{
// Exporting the Dataset to an ExcelSpreadsheet.
Excel.Application objApplication= new
Excel.ApplicationClass();
try
{
Excel.Workbook objWorkBook;
Excel.Worksheet objWorkSheet;
objWorkBook = (Excel.Workbook)
(objApplication.Workbooks.Add(Missing.Value));
//objWorkBook = (Excel.Workbook)
(objApplication.Workbooks.Add(objApplication));
objWorkSheet = (Excel.Worksheet)
objWorkBook.ActiveSheet;
objWorkSheet.Name = "Dataset Data";
objApplication.WindowState =
Excel.XlWindowState.xlMaximized;
// Initialise the progress form.
frmProgress newProgress = new frmProgress();
newProgress.pbprogress.Minimum = 1;
newProgress.pbprogress.Value = 1;
newProgress.pbprogress.Step = 1;
newProgress.Show();
foreach(DataTable table in dsCost.Tables)
{
//Set Maximum to the total number of files to copy.
//int numRows = dataGrid1.BindingContext
[dataGrid1.DataSource, dataGrid1.DataMember].Count;
//MessageBox.Show("Number of rows = " +
numRows.ToString() );
newProgress.pbprogress.Maximum = table.Rows.Count;
int row = 1, column = 1;
// Need to export the column headers here.
foreach (DataColumn dcColumn in
table.Columns)
{
objWorkSheet.Cells[row, column] =
dcColumn.ColumnName.ToString();
column++;
}
row++;
foreach(DataRow r in table.Rows)
{
column = 1;
foreach (object o in r.ItemArray)
{
objWorkSheet.Cells[row, column] = o.ToString();
column++;
}
row++;
newProgress.pbprogress.PerformStep();
Application.DoEvents();
}
}
MessageBox.Show("Export Process has completed");
newProgress.Close();
objApplication.Visible = true;
objApplication.WindowState =
Excel.XlWindowState.xlMaximized;
}
catch (Exception e)
{
// Exception Handler
MessageBox.Show("Export process raised the error: " +
e.Message);
}
//objApplication.Quit();
}