I have a created a program to auto fill a invoice excel file so that it can be later printed. I am able to fill the cells and format them. But the excel process is still running in the background.
public void Write(tbl_Sale sale,List<SalesModel> saleCollection)
{
Mouse.SetCursor(Cursors.Wait);
Excel.Application application = null;
Workbook workbook = null;
Worksheet inputSheet = null;
Range range = null;
try
{
if (sale != null)
{
if (saleCollection.Count != 0)
{
application = new Excel.Application();
FileInfo fileInfo = new FileInfo("Sample.xlsx");
var sheet = "Invoice";
workbook = application.Workbooks.Open(fileInfo.FullName);
inputSheet = workbook.Worksheets.Cast<Worksheet>().Where(w => w.Name == sheet).FirstOrDefault();
range = inputSheet.Cells;
range.SetValue("G4", sale.Sale_ID);
range.SetValue("G5", sale.Sale_Date);
Range row = inputSheet.get_Range("A22", Missing.Value).EntireRow;
for (int i = 0; i < saleCollection.Count; i++)
{
row.Insert(XlInsertShiftDirection.xlShiftDown, row);
range = inputSheet.Cells;
range.SetValue("A" + (22 + i), i + 1);
range = inputSheet.Cells;
range.SetValue("B" + (22 + i), saleCollection[i].Product.Prod_Code);
range = inputSheet.Range["B" + (22 + i) + ":" + "D" + (22 + i)];
range.Merge();
range = inputSheet.Cells;
range.SetValue("E" + (22 + i), saleCollection[i].Product.Prod_Name);
range = inputSheet.Range["E" + (22 + i) + ":" + "K" + (22 + i)];
range.Merge();
range.HorizontalAlignment = 1;
range = inputSheet.Cells;
range.SetValue("X" + (22 + i), saleCollection[i].SalesDetail.SaleD_Quantity);
range = inputSheet.Range["X" + (22 + i) + ":" + "Y" + (22 + i)];
range.Merge();
range.HorizontalAlignment = 1;
range.NumberFormat = "0.00";
range = inputSheet.Cells;
range.SetValue("Z" + (22 + i), saleCollection[i].SalesDetail.SaleD_Rate);
range = inputSheet.Range["Z" + (22 + i) + ":" + "AA" + (22 + i)];
range.Merge();
range.HorizontalAlignment = 1;
range.NumberFormat = "0.00";
range = inputSheet.Cells;
range.SetValue("AB" + (22 + i), saleCollection[i].SalesDetail.SaleD_Amount);
range = inputSheet.Range["AB" + (22 + i) + ":" + "AD" + (22 + i)];
range.Merge();
range.HorizontalAlignment = 1; ;
range.NumberFormat = "0.00";
}
range = inputSheet.Cells;
range.SetValue("X" + (22 + saleCollection.Count + 2), saleCollection.Sum(s => s.SalesDetail.SaleD_Quantity));
range.SetValue("AB" + (22 + saleCollection.Count + 2), sale.Sale_Total);
fileInfo = new FileInfo("Invoices\\" + sale.Sale_ID + ".xlsx");
workbook.Application.DisplayAlerts = false;
workbook.SaveAs(fileInfo.FullName, XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges ,Type.Missing, Type.Missing, Type.Missing, Type.Missing);
releaseObject(row);
MessageBox.Show("Invoice exported successfully", "Invoice Export", MessageBoxButton.OK, MessageBoxImage.Information);
// Process.Start(fileInfo.FullName);
}
}
}
catch (Exception)
{
MessageBox.Show("Error while generating Excel report");
}
finally
{
workbook.Close(0);
application.Quit();
releaseObject(application);
releaseObject(workbook);
releaseObject(inputSheet);
releaseObject(range);
GC.Collect();
Mouse.SetCursor(Cursors.Arrow);
}
}
I am not able to kill the excel process from the background.I m not sure what I am doing wrong.The code above is the whole stuff I am doing for automating excel. Kindly please help me.