using ODCWeb.DataAccess.Repository.IRepository; using ODCWeb.Models; using ODCWeb.Services; using Syncfusion.XlsIO; using System.IO; public class ConstructionSheetReportService : IConstructionSheetReportService { private readonly IUnitOfWork _unitOfWork; public ConstructionSheetReportService(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } public byte[] GenerateExcelReport(int subtaskId) { var subtask = _unitOfWork.Subtask.Get(u => u.SubtaskId == subtaskId); if (subtask == null) return null; using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; // Set column widths worksheet.Range["A:B"].ColumnWidth = 30; // Add title worksheet.Range["A1:B1"].Merge(); worksheet.Range["A1"].Text = "Construction Sheet Report"; worksheet.Range["A1"].CellStyle.Font.Bold = true; worksheet.Range["A1"].CellStyle.Font.Size = 14; worksheet.Range["A1"].HorizontalAlignment = ExcelHAlign.HAlignCenter; ActionTask task = _unitOfWork.Task.Get(u => u.TaskId == subtask.TaskId); // Add details int currentRow = 3; AddDetailRow(worksheet, currentRow++, "Task", task.Name); AddDetailRow(worksheet, currentRow++, "Description", subtask.SubTaskD); AddDetailRow(worksheet, currentRow++, "Start Time", subtask.StartTimeStamp.ToString("g")); AddDetailRow(worksheet, currentRow++, "End Time", subtask.EndTimeStamp.ToString("g")); if (!string.IsNullOrEmpty(subtask.Comments)) { AddDetailRow(worksheet, currentRow++, "Comments", subtask.Comments); } if (subtask.FileData != null) { AddDetailRow(worksheet, currentRow++, "File Name", subtask.FileName ?? "N/A"); // If it's an image, add it to the next row if (subtask.FileContentType?.StartsWith("image/") == true) { worksheet.Range[$"A{currentRow}:B{currentRow}"].Merge(); worksheet.Range[$"A{currentRow}"].Text = "Attached Image:"; worksheet.Range[$"A{currentRow}"].CellStyle.Font.Bold = true; currentRow++; try { // Set the row height to accommodate the image worksheet.SetRowHeight(currentRow, 150); // Add image IPictureShape shape = worksheet.Pictures.AddPicture( currentRow, 1, new MemoryStream(subtask.FileData) ); // Set image size shape.Width = 300; shape.Height = 200; // Move to next row after image currentRow += 4; // Add some space after the image } catch (System.Exception ex) { // Add error information in the cell if image processing fails worksheet.Range[$"A{currentRow}"].Text = $"Error displaying image: {ex.Message}"; currentRow++; } } } // Style the details IRange usedRange = worksheet.UsedRange; usedRange.BorderAround(); usedRange.BorderInside(); usedRange.CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter; // Save to memory stream using (MemoryStream ms = new MemoryStream()) { workbook.SaveAs(ms); return ms.ToArray(); } } } private void AddDetailRow(IWorksheet worksheet, int row, string label, string value) { worksheet.Range[$"A{row}"].Text = label; worksheet.Range[$"A{row}"].CellStyle.Font.Bold = true; worksheet.Range[$"B{row}"].Text = value; } }