I am trying updating a excel sheet in c# and i am using interop.excel. When i update a excel sheet this is update a single data. I want all data update in a single excel sheet. I am using this code
`private void button1_Click(object sender, EventArgs e)
{
if (cnt1 >= 1)
{
connectionSilca();
for (int counter = 0; counter < cnt1; counter++)
{
fcl.cmd = new OracleCommand("select book_id,book_title,category_id,author,book_pub,book_copies,publisher_name,isbn,copyright_year,date_added,status from book where book_id='" + listBox1.Items[counter].ToString() + "'", fcl.con);
DataTable dt = new DataTable();
DataSet DS = new DataSet();
OracleDataAdapter AD = new OracleDataAdapter(cmd);
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dt);
da.Fill(DS);
//DataTable temptable = DS.Tables[0];
//ExportToExcel(temptable);
ExportToExcel(DS.Tables[0]);
}
}
else
{
MessageBox.Show("Please Select Atleast One Item");
}
con.Close();
MessageBox.Show("EXCEL DOWNLOAD SUCCESSFULLY");
datarefresh();
}
public void ExportToExcel(DataTable dtCollection)
{
var ds = dtCollection;
Microsoft.Office.Interop.Excel.Worksheet objWorkSheet1 = null;
Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application { Visible = false };
Microsoft.Office.Interop.Excel.Workbooks objWorkbooks = objExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook objWorkbook = objWorkbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel.Sheets objSheets = objWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Range objCells;
Microsoft.Office.Interop.Excel.Range myCell;
//open the excel file form the location
//here is existing excel code
string path = @"C:\\EXCEL\\DATA\\myData.xls";
objExcel = new Microsoft.Office.Interop.Excel.Application();
objExcel.DisplayAlerts = false;
objWorkbook = objExcel.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Get all the sheets in the workbook
objSheets = objWorkbook.Worksheets;
//Get the allready exists sheet
objWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)objSheets.get_Item("Input");
Microsoft.Office.Interop.Excel.Range range = objWorkSheet1.UsedRange;
objWorkSheet1.Cells[5, 3]= 100000089;
objWorkSheet1.Cells[5, 4] = 001;
objWorkSheet1.Cells[5, 7] = System.DateTime.Now.ToString("ddMMyyyy");
objWorkSheet1.Cells[5, 10] = "ASHSBCTS2017";
var iCurrentRow = 12;
var dt = ds;
int columnsCount = dt.Columns.Count;
objWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)(objSheets[1]);
objCells = objWorkSheet1.Cells;
/*for (var h = 0; h < dt.Columns.Count; h++)
{
myCell = (Microsoft.Office.Interop.Excel.Range)objCells[iCurrentRow, h + 1];
myCell.Value2 = dt.Columns[h].ColumnName;
}
iCurrentRow++;*/
for (var r = 0; r < dt.Rows.Count; r++)
{
for (var c = 0; c < dt.Columns.Count; c++)
{
if (dt.Columns[c].DataType.Name == "String" || dt.Columns[c].DataType.Name == "DateTime")
{
myCell = (Microsoft.Office.Interop.Excel.Range)objCells[r + iCurrentRow, c + 1];
myCell.Value2 = "'" + dt.Rows[r][c].ToString().Trim();
}
else
{
myCell = (Microsoft.Office.Interop.Excel.Range)objCells[r + iCurrentRow, c + 1];
myCell.Value2 = dt.Rows[r][c];
}
}
}
//objWorkSheet1.Cells.EntireRow.AutoFit();
//objWorkSheet1.Cells.EntireColumn.AutoFit();
objWorkbook.SaveAs("C:\\EXCEL\\DATA\\myData.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel12, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, false, false,
System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
objExcel.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}`
How to solve this, please give me some solution for this Thanks in advance