/// <summary> /// Opens Microsoft Excel to view spreadsheet of the invoice. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ViewInvoiceBtn_Click(object sender, EventArgs e) { Invoice invoice = (Invoice)invoiceListBox.SelectedItem; if (invoice != null) { Customer customer = ProductDb.GetCustomer(invoice.Customers[0].CustomerID); List <Product> products = ProductDb.GetInvoiceProducts(invoice.InvoiceID); List <InvoiceLineItems> lineItems = ProductDb.GetInvoiceQuantities(invoice.InvoiceID); // Setting up the excel spreadsheet object Nothing = System.Reflection.Missing.Value; var app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = true; Workbook workbook = app.Workbooks.Add(Nothing); Worksheet worksheet = (Worksheet)workbook.Sheets[1]; worksheet.Name = "Worksheet"; // Formats the columns to be bold and font to 20 worksheet.Cells[1, 1].EntireColumn.Font.Bold = true; worksheet.Cells[1, 1].EntireColumn.Font.Size = 20; worksheet.Cells[1, 2].EntireColumn.Font.Bold = true; worksheet.Cells[1, 2].EntireColumn.Font.Size = 20; worksheet.Cells[1, 3].EntireColumn.Font.Bold = true; worksheet.Cells[1, 3].EntireColumn.Font.Size = 20; // Fills in the cells with the invoice number and customer information worksheet.Cells[1, 1] = "Invoice number: " + invoice.InvoiceID; worksheet.Cells[3, 1] = customer.Business; worksheet.Cells[4, 1] = customer.ContactFirstName + " " + customer.ContactLastName; worksheet.Cells[5, 1] = customer.Address; worksheet.Cells[6, 1] = customer.City + ", " + customer.State + " " + customer.ZipCode; worksheet.Cells[7, 2] = "Product"; worksheet.Cells[7, 3] = "Quantity"; // Goes through a loop and fills in product information int count = 8; int column = 2; for (int i = 0; i < products.Count; i++) { worksheet.Cells[count, column] = products[i].InvoiceDisplayString(); worksheet.Cells[count, column + 1] = lineItems[i].Quantity; count++; } count++; worksheet.Cells[count, column] = "Date Shipped:"; worksheet.Cells[count + 1, column] = invoice.ShipDate; // Adjusts all the cells size to fit the text worksheet.Columns.AutoFit(); } else { MessageBox.Show("There are no invoices to display", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }