Hi,
I want to create an excel sheet report wherein my 1st row will remain same.1st time when I create the excel it works fine.I used save as method.Its saved in shared format.The data should be added each time to incremented rows.
I used a static variable to increment row number.Where file is newly created I set this variable to 2 and next time it checks if file exists and increments.But the incrementing and data addition is not happening.Please help.
What I have tried:
My code:
public static int count;
private void button2_Click(object sender, EventArgs e)
{
if(File.Exists(@"C:\Repot.xls"))
{
workbook = xlApp.Workbooks.Open(@"C:\Repot.xls");
count+=1;
sheet.Cells[count,"A"] = value1;
sheet.Cells[count,"B"] = value2;
sheet.Cells[count,"C"] = value3;
sheet.Cells[count,"D"] = value4;
sheet.Cells[count,"E"] = value5;
sheet.Cells[count,"F"] = value6;
.
.
.
.
.
xlApp.DisplayAlerts = false;
workbook.SaveAs(@"C:\Repot.xls",Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlShared,misValue,misValue,misValue,misValue,misValue);
}
else
{
sheet.Name = "X";
sheet.Cells[1,"A"] = "Date";
sheet.Cells[1,"B"] = "Time";
sheet.Cells[1,"C"] = "Y1";
sheet.Cells[1,"D"] = "Y2";
sheet.Cells[1,"E"] = "Y3";
sheet.Cells[1,"F"] = "Y4";
.
.
.
sheet.Cells[2,"A"] = value1;
sheet.Cells[2,"B"] = value2;
sheet.Cells[2,"C"] = value3;
sheet.Cells[2,"D"] = value4;
sheet.Cells[2,"E"] = value5;
sheet.Cells[2,"F"] = value6;
.
.
.
workbook.SaveAs(@"C:\Repot.xls",Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlShared,misValue,misValue,misValue,misValue,misValue);
count=2;
}
workbook.Close(true);
xlApp.Quit();