We are using Aspose.Cells to create some charts. We are getting following error when using the Save method:
workbook.Save(excelFileName, SaveFormat.Xlsx);
Error:
System.Xml.XmlException: ‘’, hexadecimal value 0x02, is an invalid character. Line 1, position 2.
at Aspose.Cells.Workbook.Save(String fileName, SaveOptions saveOptions)
at Aspose.Cells.Workbook.Save(String fileName, SaveFormat saveFormat)
Thanks for providing us the template file and sharing the sample code.
I can find MS Excel displays error message: “Excel found unreadable…” when opening the saved file (that contains a chart by Aspose.Cells APIs) into MS Excel 2007. The chart is also not rendered at all.
I have logged a ticket with an id: CELLSNET-41118 for your issue. We will look into it soon to figure it out.
I have tested your issue using our latest version/fix:Aspose.Cells for .NET (Latest Version) and it works fine. I used the following code and my output file is opened fine.
Sample code:
Workbook wb = new Workbook(@“e:\test2\Book1.xlsx”);
Worksheet wrktest = wb.Worksheets["__ifnet"];
//Adding a new worksheet to the Workbook object
int sheetIndex = wb.Worksheets.Add(SheetType.Chart);
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = wb.Worksheets[sheetIndex];
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 1, 1, 25, 30);
//Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
Cells cell = wrktest.Cells;
//Adding NSeries (chart data source) to the chart ranging from “A1” cell to “B4”
chart.NSeries.Add("__ifnet!B2:I10", true);
chart.NSeries.CategoryData = “__ifnet!A2:A10”;
chart.NSeries.SecondCatergoryData = “__ifnet!A2:A10”;
////Setting the chart type of 2nd NSeries to display as line chart
int i = 0;
for (i = 0; i < 7; i++)
{
string index = ((char)(66 + i)).ToString() + “1”;
chart.NSeries[i].Name = cell[index].Value.ToString();
//chart.NSeries[i].Name = cell[0,i+1].Value.ToString();
chart.NSeries[i].Type = Aspose.Cells.Charts.ChartType.Line;
chart.NSeries[i].Line.Weight = WeightType.HairLine;
DataLabels datalabels = chart.NSeries[i].DataLabels;
datalabels.ShowValue = false;
}
string cellindex = ((char)(66 + i)).ToString() + “1”;
chart.NSeries[i].Name = cell[cellindex].Value.ToString();
//chart.NSeries[i].Name = cell[0, i + 1].Value.ToString();
chart.NSeries[i].Type = Aspose.Cells.Charts.ChartType.Line;
chart.NSeries[i].PlotOnSecondAxis = true;
chart.NSeries[i].Line.IsVisible = true;
chart.SecondCategoryAxis.IsVisible = true;
Console.WriteLine(“Applying Formatting …”);
//Formatting chart area
chart.PlotArea.Area.BackgroundColor = Color.White;
chart.ChartArea.Area.BackgroundColor = Color.White;
chart.PlotArea.Area.ForegroundColor = Color.White;
chart.ChartArea.Area.ForegroundColor = Color.White;
chart.Name = “Sample Sheet”;
chart.Title.Text = “Sample Chart”;
chart.SizeWithWindow = true;
chart.ValueAxis.IsAutomaticMajorUnit = true;
chart.SecondValueAxis.IsAutomaticMajorUnit = true;
chart.SecondValueAxis.IsVisible = true;
chart.ValueAxis.IsAutomaticMaxValue = true;
chart.SecondValueAxis.IsAutomaticMaxValue = true;
chart.Calculate();
wb.Save(@“e:\test2\myabc.xlsx”);
I tried the below code with latest DLL, though the excel is getting saved succeesfully, the chart is not getting genearted. Getting attached error:
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using Aspose.Cells; using Aspose.Cells.Charts; using Aspose.Cells.Drawing;
I have tested your file with the following code and found that issue is not replicable using the latest version: Aspose.Cells for .NET (Latest Version) .
Are you using a Regular .NET Framework or ClientProfile Framework ? Because there are two separate dlls for each of them and the dll that belongs to one version might not work with other version.
I have attached the output xlsx file generated using your source file with the given code and screenshot for your reference.
C#
string filePath = @“F:\Shak-Data-RW\Downloads\testBook1.xlsx”;
Workbook test = new Workbook(filePath);
Worksheet wrktest = test.Worksheets["__testsheet"];
//Adding a new worksheet to the Workbook object
int sheetIndex = test.Worksheets.Add(SheetType.Chart);
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = test.Worksheets[sheetIndex];
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 1, 1, 25, 30);
//Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
//Adding NSeries (chart data source) to the chart ranging from “A1” cell to “B4”
chart.NSeries.Add("__testsheet!B2:EG168", true);
chart.NSeries.CategoryData = “__testsheet!A1:A168”;
chart.NSeries.SecondCatergoryData = “__testsheet!A1:A168”;
////Setting the chart type of 2nd NSeries to display as line chart
for (int i = 0; i < 50; i++)
{
chart.NSeries[i].Type = Aspose.Cells.Charts.ChartType.LineStacked;
chart.NSeries[i].Line.Weight = WeightType.HairLine;
chart.NSeries[i].Name = “®Ù”;
DataLabels datalabels = chart.NSeries[i].DataLabels;
//datalabels.Text = “®Ù”;
datalabels.ShowValue = false;
}
chart.NSeries[10].Name = “Area Two”;
chart.NSeries[10].Type = Aspose.Cells.Charts.ChartType.Line;
chart.NSeries[10].PlotOnSecondAxis = true;
chart.NSeries[10].Line.IsVisible = true;
chart.SecondCategoryAxis.IsVisible = true;
Console.WriteLine(“Applying Formatting …”);
//Formatting chart area
chart.PlotArea.Area.BackgroundColor = Color.White;
chart.ChartArea.Area.BackgroundColor = Color.White;
chart.PlotArea.Area.ForegroundColor = Color.White;
chart.ChartArea.Area.ForegroundColor = Color.White;
chart.Name = “Sample Sheet”;
chart.Title.Text = “Sample Chart”;
chart.SizeWithWindow = true;
chart.ValueAxis.IsAutomaticMajorUnit = true;
chart.SecondValueAxis.IsAutomaticMajorUnit = true;
chart.SecondValueAxis.IsVisible = true;
chart.ValueAxis.IsAutomaticMaxValue = true;
chart.SecondValueAxis.IsAutomaticMaxValue = true;
chart.Calculate();
////Saving the Excel file
test.Save(filePath + @".out.xlsx", SaveFormat.Xlsx);
We were able to reproduce this issue at our end. By adding your line, it makes the chart corrupts.
Below is a simplified sample code that reproduces your exception. I have attached the source used in this code and output xlsx file generated by it.
We have logged this issue in our database. We will look into this issue and fix it. Once the issue is resolved or we have some other update for you, we will let you know asap.
I can notice the chart’s name set as “Area One®Ù”.
I have logged your file with comments to attach with your existing issue “CELLSNET-41240”. Our concerned developer will look into it. Once we have any update on it, we will let you know here.