public FileStreamResult SachBan(DateTime startDate, DateTime endDate) { List<Sach> DMSach = SachManager.getAll() .Where(s => s.tongSoLuongBanTheoThang( ((DateTime)startDate).Month, ((DateTime)startDate).Year, ((DateTime)endDate).Month, ((DateTime)endDate).Year) > 0 && s.tongTienBanTheoThang( ((DateTime)startDate).Month, ((DateTime)startDate).Year, ((DateTime)endDate).Month, ((DateTime)endDate).Year) > 0).ToList(); var printer = new PrintHelper(); printer.FileName = "report.pdf"; printer.FolderPath = "D://"; printer.Title = "Thống kê sách bán"; var info = new MemoryStream(printer.printSachBan(DMSach, startDate, endDate)); return new FileStreamResult(info, "application/pdf"); }
private void btIn_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Bạn có muốn xuất tạo file báo cáo", "Thông báo", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { var printer = new PrintHelper(); string x = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + ""; string tenfile = x + "ReportSachBan.pdf"; printer.FileName = tenfile; printer.FolderPath = "D://Report"; printer.Title = "Báo cáo Sách bán"; var startDate = new DateTime(_startYear, _startMonth, 1); var endDate = new DateTime(_endYear, _endMonth, 1); endDate.AddMonths(1).AddDays(-1); printer.printSachBan(_Sach,startDate,endDate); MessageBox.Show("Đã tạo file thành công , Tên file là : " + tenfile); // var redListTextFont = FontFactory.RegisterDirectory(Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts"); // var _bold = FontFactory.GetFont("Times New Roman", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10f, iTextSharp.text.Font.BOLD, BaseColor.BLACK); // var _bold1 = FontFactory.GetFont("Times New Roman", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10f, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); // PdfPTable pdfTable = new PdfPTable(gdvDMSach.ColumnCount); // pdfTable.DefaultCell.Padding = 3; // pdfTable.WidthPercentage = 30; // pdfTable.HorizontalAlignment = Element.ALIGN_CENTER; // pdfTable.DefaultCell.BorderWidth = 1; // pdfTable.TotalWidth = 550f; // pdfTable.LockedWidth = true; // float[] widths = new float[] { 70f, 120f, 120f, 120f, 120f }; // pdfTable.SetWidths(widths); // //Adding Header row // foreach (DataGridViewColumn column in gdvDMSach.Columns) // { // PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, _bold)); // cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240); // pdfTable.AddCell(cell); // } // //Adding DataRow // foreach (DataGridViewRow row in gdvDMSach.Rows) // { // foreach (DataGridViewCell cell in row.Cells) // { // if (!String.IsNullOrEmpty(Convert.ToString(cell.Value))) // pdfTable.AddCell(new Phrase(cell.Value.ToString(), _bold1)); // } // } // //Exporting to PDF // string folderPath = @"C:\Users\huy\Desktop\Report\"; // string x = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + ""; // string tenfile = x + "ReportThongKeBanSach.pdf"; // if (!Directory.Exists(folderPath)) // { // Directory.CreateDirectory(folderPath); // } // using (FileStream stream = new FileStream(folderPath + tenfile, FileMode.Create)) // { // Document pdfDoc = new Document(PageSize.A3, 100f, 100f, 100f, 0); // PdfWriter.GetInstance(pdfDoc, stream); // pdfDoc.Open(); // var FontColour = new BaseColor(255, 0, 0); // var _bold2 = FontFactory.GetFont("Times New Roman", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 20f, iTextSharp.text.Font.NORMAL, BaseColor.BLUE); // Paragraph docTitle = new Paragraph("Thống kê bán sách " +"\n", _bold2); // Paragraph docTitle1 = new Paragraph("Từ tháng : " + cmbStartMonth.Text + " Năm " + cmbStartYear.Text + "\n", _bold2); // Paragraph docTitle2 = new Paragraph("đến tháng : " + cmbEndMonth.Text + " Năm " + cmbEndYear.Text + "\n", _bold2); // Paragraph docTitle3 = new Paragraph("Số lượng : " +lbSachBanDuoc.Text+ "\n", _bold2); // Paragraph docTitle4 = new Paragraph("Tổng tiền : " + lbTongTien.Text+ "\n", _bold2); // docTitle.Alignment = Element.ALIGN_CENTER; // docTitle1.Alignment = Element.ALIGN_LEFT; // docTitle2.Alignment = Element.ALIGN_LEFT; // docTitle3.Alignment = Element.ALIGN_LEFT; // docTitle4.Alignment = Element.ALIGN_LEFT; // pdfDoc.Add(docTitle); // pdfDoc.Add(docTitle1); // pdfDoc.Add(docTitle2); // pdfDoc.Add(docTitle3); // pdfDoc.Add(docTitle4); // pdfDoc.Add(new Paragraph("\n")); // pdfDoc.Add(new Paragraph("\n")); // pdfDoc.Add(pdfTable); // pdfDoc.Close(); // stream.Close(); // MessageBox.Show("Đã tạo file thành công , Tên file là : " + tenfile); } else if (dialogResult == DialogResult.No) { return; } }