Hi Aspose Team,
I’m having an issue with some charts where I’m inserting it into a word document and one of the footnotes for the chart comes in as #NAME? even though the name range from which it is pulled into the chart exists.
Below is the sample code I’ve used. Another thing I’d like to point out is the fact that the images stored separately as EMF File (Chart_Demographics_D137_O169, Chart_Demographics_D205_O237, etc) have missing information even though the same thing that is inserted into word document looks to be complete aside from the #NAME? error.
static void TestChartInsertion()
{
string dir = @"Insert you directory path here";
List<string> PrintAreas = new() { "Demographics!R137C4:R169C15", "Demographics!R205C4:R237C15", "Demographics!R476C4:R508C15", "Demographics!R510C4:R542C15" };
Document doc = new Document();
DocumentBuilder Builder = new DocumentBuilder(doc);
using (Workbook TestWB = new Workbook(dir + @"Chart_Template.xlsm"))
{
foreach (var coordinates in PrintAreas)
{
Stream ImgStream = ProcessOleObject_Aspose(TestWB, coordinates, dir);
if (ImgStream == Stream.Null)
{
continue;
}
Builder.InsertImage(ImgStream);
Builder.InsertBreak(BreakType.PageBreak);
}
}
doc.Save(dir + @"TestDoc_Aspose.docm");
}
static Stream ProcessOleObject_Aspose(Workbook sourceWB, string imgCoordinates, string dir)
{
string ProcessName = "Convert excel range to image";
string ExceptionType = "P";
string StartCell = string.Empty;
string EndCell = string.Empty;
try
{
//Check to see if image coordinate matches the regex.
//Regex Pattern = new Regex(@"^[a-zA-Z0-9]+![\s]*R[0-9]+C[0-9]+:R[0-9]+C[0-9]+$");
Regex Pattern = new Regex(@"^.*![\s]*R[0-9]+C[0-9]+:R[0-9]+C[0-9]+$");
if (!Pattern.IsMatch(imgCoordinates))
{
throw new Exception($"Invalid range encountered: {imgCoordinates}");
}
string[] CoordinateParts = imgCoordinates.Trim().Split('!', ':');
// Here's an example of what an imgCoordinates looks like RespRate!R5C1:R8C2
string WSName = CoordinateParts[0].Trim(); //RespRate
string StartAddress = CoordinateParts[1].Trim(); //R5C1
string EndAddress = CoordinateParts[2].Trim(); //R8C2
//Processing Start Address
string[] StartDigits = StartAddress.Split('R', 'C').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
int.TryParse(StartDigits[0], out int startRow);
int.TryParse(StartDigits[1], out int startCol);
//Process End Address
string[] EndDigits = EndAddress.Split('R', 'C').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
int.TryParse(EndDigits[0], out int endRow);
int.TryParse(EndDigits[1], out int endCol);
// Coordinates cannot be zero by any chance
if (startRow != 0 || startCol != 0 || endRow != 0 || endRow != 0)
{
// Convert the starting coordinates to cell address
StartCell = CellsHelper.CellIndexToName(startRow - 1, startCol - 1);
// Convert the ending coordinates to cell address
EndCell = CellsHelper.CellIndexToName(endRow - 1, endCol - 1);
}
else
{
throw new Exception($"Linked coordinate contain 0 as a coordinate.\n Cannot convert the image coordinate -\n {imgCoordinates}\n to an image");
}
//Open the source workbook
sourceWB.CalculateFormula();
Worksheet sourceWS = sourceWB.Worksheets[WSName];
//Set print area
sourceWS.PageSetup.PrintArea = $"{StartCell}:{EndCell}";
sourceWS.PageSetup.LeftMargin = 0;
sourceWS.PageSetup.RightMargin = 0;
sourceWS.PageSetup.TopMargin = 0;
sourceWS.PageSetup.BottomMargin = 0;
// Clear any header/footer as they'll be captured when converting the worksheet to image
sourceWS.PageSetup.ClearHeaderFooter();
// Set OnePagePerSheet option as true
ImageOrPrintOptions options = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = Aspose.Cells.Drawing.ImageType.Emf,
HorizontalResolution = 100,
VerticalResolution = 100,
OnlyArea = true,
TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
};
// Take the image of your worksheet
SheetRender Render = new SheetRender(sourceWS, options);
// Create a stream to save the image in
MemoryStream Stream = new MemoryStream();
Render.ToImage(0, Stream);
Render.ToImage(0, dir + $"Chart_{WSName}_{StartCell}_{EndCell}.emf");
return Stream;
}
catch (Exception ex)
{
return Stream.Null;
}
}
Files used for testing:
Test_12112025.zip (2.2 MB)