使用aspose.cells生成png形式的统计图图片时,如何指定分辨率
时间: 2023-05-26 08:01:51 浏览: 257
使用Aspose.Cells生成png形式的统计图图片时,可以使用ImageOrPrintOptions类中的setHorizontalResolution和setVerticalResolution方法来指定分辨率。
例如,以下代码片段设置水平和垂直分辨率为150。
```
Chart chart = worksheet.getCharts().get(0);
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setHorizontalResolution(150);
options.setVerticalResolution(150);
chart.toImage("chart.png", options);
```
这将生成一个名为“chart.png”的图像文件,其水平和垂直分辨率都为150。
相关问题
使用aspose.cells生成统计图,输出为图片并压缩
A:可以使用Aspose.Cells来生成统计图,然后将它输出为图片并压缩。下面是一个示例代码:
```
// 新建工作簿
Workbook workbook = new Workbook();
// 获取第一个工作表
Worksheet worksheet = workbook.getWorksheets().get(0);
// 插入一些数据
worksheet.getCells().get("A1").setValue("Month");
worksheet.getCells().get("B1").setValue("Sales");
worksheet.getCells().get("A2").setValue("Jan");
worksheet.getCells().get("B2").setValue(500);
worksheet.getCells().get("A3").setValue("Feb");
worksheet.getCells().get("B3").setValue(800);
worksheet.getCells().get("A4").setValue("Mar");
worksheet.getCells().get("B4").setValue(1000);
// 新建一个统计图
int chartIndex = worksheet.getCharts().add(ChartType.COLUMN, 10, 0, 30, 10);
Chart chart = worksheet.getCharts().get(chartIndex);
// 设置图表的标题和数据区域
chart.getTitle().setText("Sales Report");
chart.getNSeries().add("B2:B4", true);
chart.getNSeries().setCategoryData("A2:A4");
// 将图表输出为图片
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setImageFormat(ImageFormat.getPng());
options.setHorizontalResolution(300);
options.setVerticalResolution(300);
chart.toImage("chart.png", options);
// 压缩图片
Pictures pictures = worksheet.getPictures();
Picture picture = pictures.get(pictures.getCount() - 1);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(picture.getImage(), "png", outputStream);
byte[] bytes = outputStream.toByteArray();
outputStream.close();
outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
gzipOutputStream.write(bytes);
gzipOutputStream.finish();
bytes = outputStream.toByteArray();
gzipOutputStream.close();
outputStream.close();
```
这个代码将会生成一个柱形图,并将它输出为PNG格式的图片。然后将这个图片压缩并保存到一个字节数组中,你可以将它存储到磁盘中或者通过网络传输。
阅读全文
相关推荐













