public void GenerateContractReport(WholesaleEntities db, DateTime fromDate, DateTime toDate) { object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ int i = 1; //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = String.Format("Отчет о продажах за период c {0} по {1}", fromDate.ToShortDateString(), toDate.ToShortDateString()); oPara1.Range.Font.Bold = 1; oPara1.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //alignment center oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter(); //Insert a 3 x 5 table, fill it with data, and make the first row //bold and italic. List<Contracts> contracts = db.Contracts.Where(x=> x.date >= fromDate && x.date <= toDate).ToList(); Word.Table oTable; Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oTable = oDoc.Tables.Add(wrdRng, contracts.Count+1, 5, ref oMissing, ref oMissing); oTable.Range.ParagraphFormat.SpaceAfter = 6; oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; //header oTable.Cell(i, 1).Range.Text = "№ накладной"; oTable.Cell(i, 2).Range.Text = "Дата"; oTable.Cell(i, 3).Range.Text = "Покупатель"; oTable.Cell(i, 4).Range.Text = "Контактное лицо"; oTable.Cell(i, 5).Range.Text = "Общая сумма, грн"; //content foreach(Contracts contract in contracts) { i++; oTable.Cell(i, 1).Range.Text = Convert.ToString(contract.contractID); oTable.Cell(i, 2).Range.Text = Convert.ToString(contract.date.ToShortDateString()); oTable.Cell(i, 3).Range.Text = Convert.ToString(db.Clients.FirstOrDefault(x => x.clientID == contract.clientID).name); oTable.Cell(i, 4).Range.Text = Convert.ToString(db.Clients.FirstOrDefault(x => x.clientID == contract.clientID).contactName); oTable.Cell(i, 5).Range.Text = Convert.ToString(db.ContractProducts.Where(z => z.contractID == contract.contractID).Sum(x => x.count * x.ProductDeliveries.Products.price + x.ProductDeliveries.Deliveries.price)); } oTable.Rows[1].Range.Font.Bold = 1; oTable.Rows[1].Range.Font.Italic = 1; oTable.Columns.AutoFit(); }
public void GenerateContractProductReport(WholesaleEntities db, int contractID) { object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ int i = 1; //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = String.Format("Отчет о продажах по накладной № {0}",contractID); oPara1.Range.Font.Bold = 1; oPara1.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //alignment center oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter(); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara2; oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; //alignment left oPara2.Range.Text = String.Format("Дата: {0}", db.Contracts.FirstOrDefault(x=> x.contractID == contractID).date.ToShortDateString()); oPara2.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara2.Range.ParagraphFormat.SpaceAfter = 6; oPara2.Range.InsertParagraphAfter(); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara3; oPara3 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; //alignment left oPara3.Range.Text = String.Format("Покупатель: {0}", db.Contracts.FirstOrDefault(x => x.contractID == contractID).Clients.name); oPara3.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara3.Range.ParagraphFormat.SpaceAfter = 6; oPara3.Range.InsertParagraphAfter(); //Insert a 3 x 5 table, fill it with data, and make the first row //bold and italic. List<ContractProducts> products = db.ContractProducts.Where(x => x.contractID == contractID).ToList(); Word.Table oTable; Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oTable = oDoc.Tables.Add(wrdRng, products.Count + 1, 6, ref oMissing, ref oMissing); oTable.Range.ParagraphFormat.SpaceAfter = 6; oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; //header oTable.Cell(i, 1).Range.Text = "№"; oTable.Cell(i, 2).Range.Text = "Торвар"; oTable.Cell(i, 3).Range.Text = "Описание"; oTable.Cell(i, 4).Range.Text = "Кол-во"; oTable.Cell(i, 5).Range.Text = "Способ доставки"; oTable.Cell(i, 6).Range.Text = "Цена за товар + доставка, грн"; //content foreach (ContractProducts product in products) { i++; oTable.Cell(i, 1).Range.Text = Convert.ToString(product.productID); oTable.Cell(i, 2).Range.Text = Convert.ToString(product.ProductDeliveries.Products.name); oTable.Cell(i, 3).Range.Text = Convert.ToString(product.ProductDeliveries.Products.description); oTable.Cell(i, 4).Range.Text = Convert.ToString(product.count); oTable.Cell(i, 5).Range.Text = Convert.ToString(product.ProductDeliveries.Deliveries.type); oTable.Cell(i, 6).Range.Text = Convert.ToString(product.ProductDeliveries.Products.price * product.count) + " + " + Convert.ToString(product.ProductDeliveries.Deliveries.price); } oTable.Rows[1].Range.Font.Bold = 1; oTable.Rows[1].Range.Font.Italic = 1; oTable.Columns.AutoFit(); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara4; oPara4 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; //alignment right oPara4.Range.Text = String.Format("Общая сумма, грн: {0}", products.Sum(x=> x.count*x.ProductDeliveries.Products.price + x.ProductDeliveries.Deliveries.price)); oPara4.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara4.Range.InsertParagraphAfter(); oPara4.Range.ParagraphFormat.SpaceBefore = 6; oPara4.Range.ParagraphFormat.SpaceAfter = 6; }