public FileStreamResult ExportReport(string ReportID, WorkbookDesigner designer) { try { string LData = WebConfigurationManager.AppSettings["LData"]; Stream stream = new MemoryStream(Convert.FromBase64String(LData)); stream.Seek(0, SeekOrigin.Begin); new Aspose.Cells.License().SetLicense(stream); if (designer != null) { stream = new DongA.Core.DongAExcel().SaveToStream(designer.Workbook); } // Return excel return(File(stream, XLSX, string.Format("{0}.{1}", ReportID, "xlsx"))); } catch (Exception ex) { MemoryStream memStream = new MemoryStream(); return(File(memStream, PDF)); } }
/// <summary> /// Tạo mẫu cho Excel cho so sánh theo giai đoạn /// </summary> /// <param name="gradationID"></param> /// <param name="year"></param> /// <param name="typeID"></param> /// <returns></returns> public ActionResult CreateExcelGradationCompareLastYear(int year, int month, string reportTypeID) { WorkbookDesigner designer = new WorkbookDesigner(); string templatePath = "~/Content/Report/ReportForTotalPayment.xlsx"; // Get đường dẫn templatePath = System.Web.HttpContext.Current.Server.MapPath(templatePath); designer.Workbook = new DongA.Core.DongAExcel().OpenExcelFile(templatePath); designer.Workbook.CalculateFormula(); WorksheetCollection workSheets = designer.Workbook.Worksheets; Worksheet sheetReport = designer.Workbook.Worksheets[0]; // Tạo title string typeReport = "So sánh - Theo tháng - So với tháng trước"; string text = string.Format("Tháng: {0}/{1}", month, year); // Tạo title CreateTitle("A2", "U2", sheetReport, typeReport, 14); // Tạo title detailt string titleDetailt = text; CreateTitle("A3", "U3", sheetReport, titleDetailt, 12); // xóa từ/đến sheetReport.Cells["L4"].PutValue(""); sheetReport.Cells["P4"].PutValue(""); // Create Chart Line //Chart reference Aspose.Cells.Charts.Chart leadSourceLine; //Add Pie Chart int chartIndex = sheetReport.Charts.Add(ChartType.Column3DClustered, 6, 0, 30, 13); leadSourceLine = sheetReport.Charts[chartIndex]; //// Canh hiển thị CategoryAxis nghiên phù hợp //leadSourceLine.CategoryAxis.TickLabels.RotationAngle = 45; //Chart title leadSourceLine.Title.Text = string.Format("Tổng doanh số chi trả {0}/{1} so với tháng trước và so với cùng kì năm trước", month, year); leadSourceLine.Title.Font.Color = Color.Silver; // Set width cho column sheetReport.Cells.SetColumnWidthPixel(15, 220); List <ReportForTotalPayment> listReportData = new ReportBL().DataReportTPCompareForMonth(year, month, reportTypeID); // clone Object List <ReportForTotalPayment> listReportDataClone = new List <ReportForTotalPayment>(listReportData); DataTable dataTable = new DataTable(); // Theo doanh số chi trả loại hình dịch vụ if (listReportData.Count.Equals(3)) { foreach (ReportForTotalPayment item in listReportData) { item.ReportID = string.Format("Tháng {0}/{1}", item.Month, item.Year); } double totalPayment = listReportData[0].Payed - listReportData[1].Payed; double totalPaymentLastYear = listReportData[0].Payed - listReportData[2].Payed; // Object báo cáo tăng giảm so với tháng trước (%) ReportForTotalPayment dataDifference = null; dataDifference = new ReportForTotalPayment() { ReportID = "Tăng giảm so với tháng trước (%)", Payed = Math.Round(totalPayment / listReportData[1].Payed * 100, 2, MidpointRounding.ToEven), }; listReportData.Add(dataDifference); // Object báo cáo tăng giảm so với tháng trước (+/-) dataDifference = new ReportForTotalPayment() { ReportID = "Tăng giảm so với tháng trước (+/-)", Payed = Math.Round(totalPayment, 2, MidpointRounding.ToEven), }; listReportData.Add(dataDifference); // Object báo cáo tăng giảm so với cùng kì năm trước (%) dataDifference = new ReportForTotalPayment() { ReportID = "Tăng giảm so với cùng kì năm trước (%)", Payed = Math.Round(totalPaymentLastYear / listReportData[2].Payed * 100, 2, MidpointRounding.ToEven), }; listReportData.Add(dataDifference); // Object báo cáo tăng giảm so với cùng kì năm trước (+/-) dataDifference = new ReportForTotalPayment() { ReportID = "Tăng giảm so với cùng kì năm trước (+/-)", Payed = Math.Round(totalPaymentLastYear, 2, MidpointRounding.ToEven), }; listReportData.Add(dataDifference); // Add item to datatable dataTable = CreateDataTableFormart(); // Danh sách dataSet của báo cáo ngày DataSet dataReport = new DongA.Core.DongAExcel().ConvertListObjectToDataSet(listReportData); // Đổ data vào datatble mới FillData(dataReport.Tables[0], dataTable); // get data string totalRowData = "Q8:Q10"; leadSourceLine.NSeries.Add(totalRowData, true); // Get row string categoryData = "P8:P10"; leadSourceLine.NSeries.CategoryData = categoryData; // Set the names of the chart series taken from cells. leadSourceLine.NSeries[0].Name = "=Q7"; // Set plot area formatting as none and hide its border. leadSourceLine.PlotArea.Area.FillFormat.FillType = FillType.None; leadSourceLine.PlotArea.Border.IsVisible = false; // Set value axis major tick mark as none and hide axis line. // Also set the color of value axis major grid lines. leadSourceLine.ValueAxis.MajorTickMark = TickMarkType.None; leadSourceLine.ValueAxis.AxisLine.IsVisible = false; leadSourceLine.ValueAxis.MajorGridLines.Color = Color.FromArgb(217, 217, 217); // Set border Style style = new CellsFactory().CreateStyle(); style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black); if (dataTable.Rows.Count > 0) { int stepRow = 0; // total row = row start + số row hiện có int totalRow = dataTable.Rows.Count + 7; // Số dòng của row for (int a = 7; a < totalRow; a++) { int stepColumn = 0; // Số cột trong báo cáo cần hiển thị // Tổng số cột hiển thị = Số cột hiển thị bắt đầu + tổng số cột cần hiển thị int totalCol = 15 + 2; for (int b = 15; b < totalCol; b++) { // Giá trị của value trong table string valueOfTable = dataTable.Rows[stepRow][stepColumn].ToString(); // Tô màu cho các dòng có giá trị tăng giảm if (b >= 16 && a >= 10) { decimal tryParseValue = 0; decimal.TryParse(valueOfTable, out tryParseValue); style.Font.Color = Color.Green; if (tryParseValue < 0) { style.Font.Color = Color.Red; } } // Insert vào dòng cột xác định trong Excel sheetReport.Cells[a, b].PutValue(valueOfTable, true); // set style cho number style.Custom = "#,##0.00"; // set border sheetReport.Cells[a, b].SetStyle(style); // Cột tổng cộng if (b.Equals(totalCol - 1)) { sheetReport.Cells[a, b].PutValue(valueOfTable, true, true); style.Font.IsBold = true; sheetReport.Cells[a, b].SetStyle(style); } // Trường hợp thuộc 2 dòng cuối if (a.Equals(totalRow - 1) || a.Equals(totalRow - 2)) { sheetReport.Cells[a, b].PutValue(valueOfTable, true, true); style.Font.IsBold = true; sheetReport.Cells[a, b].SetStyle(style); } // Set lại giá trị mặt định style.Font.IsBold = false; // Tăng cột theo dòng của table stepColumn++; } // Tăng dòng của table lên stepRow++; // Set lại color cho dòng hiện tại style.Font.Color = Color.Black; } } else { sheetReport.Cells["D10"].PutValue("Không có dữ liệu"); } } // Chạy process designer.Process(); return(ExportReport("ReportGradationCompare", designer)); }