I am trying to update the items batches wise to excel using itempossition but it's not appending the items and it's overriding the previous items. could you please help on this
while (true)
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ListItemCollectionPosition = itemPosition;
camlQuery.ViewXml = @"<View><RowLimit>1</RowLimit></View>";
items = DemandList.GetItems(camlQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();
DataTable ExcelDatatable = GenerateDataTable(items, clientContext);
if (ExcelDatatable != null && ExcelDatatable.Rows.Count > 0)
{
PushImpactsToExcel(clientContext, ExcelDatatable, FnCheckifFileExists());
}
itemPosition = items.ListItemCollectionPosition;
Console.WriteLine(itemPosition);
if (itemPosition == null)
break;
Console.WriteLine(itemPosition.PagingInfo);
}
if (!Filexists)
{
//Create Excel File
xlWorkBook = excelApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet = xlWorkBook.Sheets["Sheet1"];
xlWorkSheet = xlWorkBook.ActiveSheet;
xlWorkSheet.Name = "Sheet1";
}
else
{
//Append Excel File
excelApp.Visible = true;
xlWorkBook = excelApp.Workbooks.Open(location, 0,
false, 5, "", "", false, Excel.XlPlatform.xlWindows,
"", true, false, 0, true, false, false);
Excel.Sheets AllSheets = xlWorkBook.Sheets;
string SheetName = "Sheet1";
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)AllSheets.get_Item(SheetName);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
foreach (DataTable Sheet1 in ds.Tables)
{
//xlWorkSheet.Name = table.TableName;
xlWorkSheet.Name = "Sheet1";
for (int i = 1; i < Sheet1.Columns.Count + 1; i++)
{
xlWorkSheet.Cells[1, i] = Sheet1.Columns[i - 1].ColumnName;
}
for (int j = 0; j < Sheet1.Rows.Count; j++)
{
for (int k = 0; k < Sheet1.Columns.Count; k++)
{
xlWorkSheet.Cells[j + 2, k + 1] = Sheet1.Rows[j].ItemArray[k].ToString();
}
}
}
xlWorkBook.SaveAs(location, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
excelApp.Quit();