读取和操作Excel 2016图表
Contents
[
Hide
]
可能的使用场景
Aspose.Cells支持读取和操作Microsoft Excel 2016图表,这些图表在Microsoft Excel 2013或更早版本中不存在。
读取和操作Excel 2016图表
以下示例代码加载了包含 Microsoft Excel 2016 图表的 源Excel文件 ,并读取第一个工作表中的所有图表,逐个更改其标题以符合其图表类型。以下截图显示了执行代码之前的源Excel文件。您可以看到,所有图表的图表标题都相同。
以下截图显示了执行代码后的 输出Excel文件 。您可以看到,图表标题已根据其图表类型更改。
示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ReadManipulateExcel2016Charts.class) + "charts/"; | |
// Load source excel file containing excel 2016 charts | |
Workbook wb = new Workbook(dataDir + "excel2016Charts.xlsx"); | |
// Access the first worksheet which contains the charts | |
Worksheet ws = wb.getWorksheets().get(0); | |
//Converting integer enums to string enums | |
HashMap<Integer, String> cTypes = new HashMap<Integer, String>(); | |
cTypes.put(ChartType.BOX_WHISKER, "BoxWhisker"); | |
cTypes.put(ChartType.WATERFALL, "Waterfall"); | |
cTypes.put(ChartType.TREEMAP, "Treemap"); | |
cTypes.put(ChartType.HISTOGRAM, "Histogram"); | |
cTypes.put(ChartType.SUNBURST, "Sunburst"); | |
// Access all charts one by one and read their types | |
for (int i = 0; i < ws.getCharts().getCount(); i++) { | |
// Access the chart | |
Chart ch = ws.getCharts().get(i); | |
// Print chart type | |
String strChartType = cTypes.get(ch.getType()); | |
System.out.println(strChartType); | |
// Change the title of the charts as per their types | |
ch.getTitle().setText("Chart Type is " + strChartType); | |
} | |
// Save the workbook | |
wb.save(dataDir + "out_excel2016Charts.xlsx"); | |
// Print message | |
System.out.println("Excel 2016 Chart Titles changed successfully."); |
控制台输出
以下是执行提供的 源Excel文件 时上述示例代码的控制台输出。
Waterfall
Treemap
Sunburst
Histogram
BoxWhisker