admin管理员组文章数量:1130349
spire
想实现代码改变数据,然后刷新透视图,然后同时刷新根据透视图生成的图表,保存为图片。使用pt.calculateData()刷新,有问题,个别文档生效,个别文档无效,不知道为什么,还是老的数据。反馈给厂商,客服说是BUG,已经提交,现在使用aspose代替
设置文档打开时刷新透视表数据
//获取透视表,刷新数据PivotTable pivotTable = (PivotTable) sheet.getPivotTables().get(0);pivotTable.getCache().isRefreshOnLoad(true);
代码刷新透视表数据
XlsPivotTable pt= (XlsPivotTable) wb.getWorksheets().get(0).getPivotTables().get(0);pt.calculateData();
图表
获取图表类型
Workbook workbook = new Workbook();workbook.loadFromFile("C:\\Users\\w4523\\Desktop\\新建文件夹 (3)\\chart2_66.xlsm");ExcelChartType chartType = workbook.getWorksheets().get(0).getCharts().get(0).getChartType();System.out.println(chartType.toString());
创建图表
//创建一个新的chartChart chart = sheet.getCharts().add();//设置图表的类型chart.setChartType( ExcelChartType.ColumnClustered);//chart数据源的范围chart.setDataRange(sheet.getCellRange("E10:F17"));chart.setSeriesDataFromRange(false);//set 生成图表的位置chart.setLeftColumn(4);chart.setTopRow(2);chart.setRightColumn(12);chart.setBottomRow(22);//chart 标题chart.setChartTitle("Market share by country");chart.getChartTitleArea().isBold(true );chart.getChartTitleArea().setSize(12);for( int i =0; i<chart.getSeries().getCount();i++){ChartSerie cs = chart.getSeries().get(i);cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasPercentage(true);}//设置图例不显示chart.getLegend().setPosition( LegendPositionType.NotDocked);chart.hasChartTitle(false);//save the documentworkbook.saveToFile("d:\\保存的位置", ExcelVersion.Version2013);
保存chart为图片
//创建实例Workbook wb = new Workbook();//加载Excelwb.loadFromFile("C:\\Users\\w4523\\Desktop\\新建文件夹 (3)\\spireSample\\createPviotTable.xlsx");//将Excel文档第一个工作表中的第一个图表保存为图片BufferedImage image = wb.saveChartAsImage(wb.getWorksheets().get(0), 0);//new File()输出位置ImageIO.write(image, "png", new File("C:\\Users\\w4523\\Desktop\\新建文件夹 (3)\\123.png"));
spire官网API:.html
spire
想实现代码改变数据,然后刷新透视图,然后同时刷新根据透视图生成的图表,保存为图片。使用pt.calculateData()刷新,有问题,个别文档生效,个别文档无效,不知道为什么,还是老的数据。反馈给厂商,客服说是BUG,已经提交,现在使用aspose代替
设置文档打开时刷新透视表数据
//获取透视表,刷新数据PivotTable pivotTable = (PivotTable) sheet.getPivotTables().get(0);pivotTable.getCache().isRefreshOnLoad(true);
代码刷新透视表数据
XlsPivotTable pt= (XlsPivotTable) wb.getWorksheets().get(0).getPivotTables().get(0);pt.calculateData();
图表
获取图表类型
Workbook workbook = new Workbook();workbook.loadFromFile("C:\\Users\\w4523\\Desktop\\新建文件夹 (3)\\chart2_66.xlsm");ExcelChartType chartType = workbook.getWorksheets().get(0).getCharts().get(0).getChartType();System.out.println(chartType.toString());
创建图表
//创建一个新的chartChart chart = sheet.getCharts().add();//设置图表的类型chart.setChartType( ExcelChartType.ColumnClustered);//chart数据源的范围chart.setDataRange(sheet.getCellRange("E10:F17"));chart.setSeriesDataFromRange(false);//set 生成图表的位置chart.setLeftColumn(4);chart.setTopRow(2);chart.setRightColumn(12);chart.setBottomRow(22);//chart 标题chart.setChartTitle("Market share by country");chart.getChartTitleArea().isBold(true );chart.getChartTitleArea().setSize(12);for( int i =0; i<chart.getSeries().getCount();i++){ChartSerie cs = chart.getSeries().get(i);cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasPercentage(true);}//设置图例不显示chart.getLegend().setPosition( LegendPositionType.NotDocked);chart.hasChartTitle(false);//save the documentworkbook.saveToFile("d:\\保存的位置", ExcelVersion.Version2013);
保存chart为图片
//创建实例Workbook wb = new Workbook();//加载Excelwb.loadFromFile("C:\\Users\\w4523\\Desktop\\新建文件夹 (3)\\spireSample\\createPviotTable.xlsx");//将Excel文档第一个工作表中的第一个图表保存为图片BufferedImage image = wb.saveChartAsImage(wb.getWorksheets().get(0), 0);//new File()输出位置ImageIO.write(image, "png", new File("C:\\Users\\w4523\\Desktop\\新建文件夹 (3)\\123.png"));
spire官网API:.html
本文标签: spire
版权声明:本文标题:spire 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1731089789a1057146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论