I am attempting to export an Excel file that should ideally resemble the one shown in the first photo. To achieve this, I am dynamically generating images, with their width and placement being calculated. However, I am encountering a problem when two or more images share the same cell. Ideally, they should readjust, similar to the behavior of a flexbox, which I am currently unable to implement. Does anyone have any suggestions on how I could approach this? Or perhaps using images is not the best solution? At present, when there are two images in the same cell, they overlap (as can be seen in the second image)..
Here is my code
public class StatusPlot
{
public string StatusText { get; set; }
public string StatusColor { get; set; }
public DateTime? StatusDate { get; set; }
public bool IsRange { get; set; }
}
public class GroupedPlot
{
public int ColumnNumber { get; set; }
public List<StatusPlot> Plot { get; set; }
}
// Plot Schedule
List<GroupedPlot> schedules = ProcessSchedule(d, range);
foreach(GroupedPlot schedule in schedules)
{
int column = schedule.ColumnNumber;
int numberOfBoxesInACell = schedule.Plot.Count;
int order = 1;
int totalWidth = 0;
foreach (StatusPlot statusPlot in schedule.Plot)
{
SolidBrush textColor = new SolidBrush(ColorTranslator.FromHtml(statusPlot.StatusColor));
SolidBrush backgroundColor = new SolidBrush(ColorTranslator.FromHtml("#70b7fa"));
int width = 120;
int height = 60;
if (statusPlot.IsRange)
{
// Calculate difference
int difference = ((d.construction_period_plan_end_month.Value.Year - statusPlot.StatusDate.Value.Year) * 12) + d.construction_period_plan_end_month.Value.Month - statusPlot.StatusDate.Value.Month;
width += 138 * difference;
}
// If theres multiple same dates.
if (numberOfBoxesInACell > 1)
{
width /= numberOfBoxesInACell;
CreateSharedStatusBoxAndInsertToRow(workbook, sheet, height, width, backgroundColor, textColor, statusPlot.StatusText, currentRow, column, order);
} else
{
CreateStatusBoxAndInsertToRow(workbook, sheet, height, width, backgroundColor, textColor, statusPlot.StatusText, currentRow, column);
}
order++;
totalWidth += width;
}
}
public static void CreateStatusBoxAndInsertToRow(XSSFWorkbook workbook, ISheet sheet, int height, int width, SolidBrush backgroundColor, SolidBrush textColor, string text, int row, int column)
{
Bitmap bitmap = new Bitmap(width, height);
int defaultLeftPosition = 90000;
int defaultYPosition = 140000;
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.FillRectangle(backgroundColor, 0, 0, bitmap.Width, bitmap.Height);
// Define the font, color and position
Font font = new Font("Arial", 15, FontStyle.Bold);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
// Draw the text in the middle of the rectangle
graphics.DrawString(text, font, textColor, new RectangleF(0, 0, bitmap.Width, bitmap.Height), stringFormat);
}
// Save the image to a byte array
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] data = ms.ToArray();
int pictureIndex = workbook.AddPicture(data, PictureType.PNG);
IDrawing drawing = sheet.CreateDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor
{
AnchorType = AnchorType.MoveDontResize,
Col1 = column,
Row1 = row,
Col2 = 0,
Row2 = 0,
Dx1 = defaultLeftPosition,
Dx2 = 0,
Dy1 = defaultYPosition,
Dy2 = 0,
};
IPicture picture = drawing.CreatePicture(anchor, pictureIndex);
picture.Resize();
}
public static void CreateSharedStatusBoxAndInsertToRow(XSSFWorkbook workbook, ISheet sheet, int height, int width, SolidBrush backgroundColor, SolidBrush textColor, string text, int row, int column, int order)
{
Bitmap bitmap = new Bitmap(width, height);
int defaultLeftPosition = 90000;
int defaultYPosition = 140000;
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.FillRectangle(backgroundColor, 0, 0, bitmap.Width, bitmap.Height);
// Define the font, color and position
Font font = new Font("Arial", 15, FontStyle.Bold);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
// Draw the text in the middle of the rectangle
graphics.DrawString(text.Substring(0, 2), font, textColor, new RectangleF(0, 0, bitmap.Width, bitmap.Height), stringFormat);
}
// Save the image to a byte array
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] data = ms.ToArray();
int pictureIndex = workbook.AddPicture(data, PictureType.PNG);
IDrawing drawing = sheet.CreateDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor
{
AnchorType = AnchorType.DontMoveAndResize,
Col1 = column,
Row1 = row,
Col2 = column + 1,
Row2 = row + 1,
Dx1 = defaultLeftPosition,
Dx2 = 0,
Dy1 = defaultYPosition,
Dy2 = 0,
};
IPicture picture = drawing.CreatePicture(anchor, pictureIndex);
picture.Resize();
}