Hi,
we use FlexCel to create images from Excel files. First, a TFlexCelReport is prepared, then a TXlsFile is created, the Excel file is loaded, the report runs over the file, and finally a TUIImage is rendered.
We have problems with images embedded in those Excel files. Sometimes they disappear completely in the rendered TUIImage, as if they were not present in the Excel file. When they do so, ALL images are not visible.
I have seen two scenarios:
- When there is something with the Excel file, the export fails generally. For example, when a Excel row is too small, all images disappear. We have to adjust the height of the row, then it works.
- Some very rare times we have not changed the Excel file, but the images do disappear in one export. This happens only one time, all other exports before & after work as expected. I cannot reproduce this.
The question is, what can we do to make the image creation more reliable? Is there a way to detect the situation that images are not shown? Is there a way to see WHY those images are not shown?
procedure TTemplatePrinterFlexcel.DrawFromExcel(const xlsPath: string; const Bitmap: TBitmap; const Report: TFlexCelReport = nil);
var
xlsFile: TXlsFile;
image: TUIImage;
rowCount, colCount, tmpRow: Integer;
I1: Integer;
totalWidthInPx,
scaleFactor: Double;
begin
xlsFile := TXlsFile.Create(xlsPath);
try
xlsFile.PrintToFit := False;
xlsFile.PrintNumberOfHorizontalPages := 1;
xlsFile.PrintNumberOfVerticalPages := 0;
xlsFile.PrintHCentered := True;
xlsFile.PrintPaperSize := TPaperSize.Undefined;
// Erstes Sheet auswählen
xlsFile.ActiveSheet := 1;
// Ersetzung durchführen
if Assigned(Report) then Report.Run(xlsFile);
rowCount := xlsFile.RowCount;
colCount := xlsFile.ColCount;
// rowCount betrachtet nur Zellen mit Daten oder Hintergrund, daher
// wird geprüft ob eingebettete Objekte noch weitere Zellen belegen.
for I1 := 1 to xlsFile.ObjectCount do
begin
tmpRow := xlsFile.GetObjectAnchor(I1).Row2;
if tmpRow > rowCount then
begin
rowCount := tmpRow;
end;
end;
// Get the real Pixel width of the to-be-rendered area, so we can scale it to
// the targeted width.
totalWidthInPx := 0.0;
for I1 := 1 to colCount do
begin
totalWidthInPx := totalWidthInPx + (xlsFile.GetColWidth(I1) / TExcelMetrics.ColMult(xlsFile));
end;
scaleFactor := FPrinter.MaxWidth / totalWidthInPx;
image := xlsFile.RenderCells(
1, // row1 As Integer,
1, // col1 As Integer,
rowCount, // row2 As Integer,
colCount, // col2 As Integer,
True, // drawBackground As Boolean, drawBackground true bezieht sich auf den ZellenHintergrund (nicht das
// Hintergrundbild), so kann der zu rendernde Bereich durch Zellenhintergrund erweitert werden.
91.25, // dpi As Double, DPI und scaleFactor legt die Rendergröße fest.
TUISmoothingMode.None, // aSmoothingMode As TUISmoothingMode,
TUIInterpolationMode.Low, // aInterpolationMode As TUIInterpolationMode,
False, // antiAliased As Boolean,
scaleFactor, // aPrintScale As Double,
False, // forceBackgroundTransparent As Boolean,
True // exportObjects As Boolean
);
try
if not Assigned(image) then raise Exception.Create('Error in rendering Cells, image not assigned.');
Bitmap.SetSize(Round(image.Width), Round(Image.Height));
Bitmap.Canvas.BeginScene;
try
image.ToNativeImage(Bitmap.Canvas, 1.00);
finally
Bitmap.Canvas.EndScene;
end;
finally
image.Free;
end;
finally
xlsFile.Free;
end;
end;
Thanks!
Dominik