Excel API Library for Java - Sample Browser | Document Solutions | Set scale and resolution
[
フレーム]
src="bundle.js">
Refer to the following example code to see how to set the scaling ratio and dpi of the exported image.
// Create a jpg file stream
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("SetScaleAndResolution.jpg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Create a new workbook
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getRange("B1").setValue("Circle");
worksheet.getRange("B1").setHorizontalAlignment(HorizontalAlignment.Center);
IShape circle = worksheet.getShapes().addShape(AutoShapeType.Oval, 20, 20, 100, 100);
worksheet.getRange("E1").setValue("Square");
worksheet.getRange("E1").setHorizontalAlignment(HorizontalAlignment.Center);
IShape square = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 165, 20, 100, 100);
circle.getTextFrame().setHorizontalAnchor(HorizontalAnchor.Center);
circle.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorMiddle);
square.getTextFrame().setHorizontalAnchor(HorizontalAnchor.Center);
square.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorMiddle);
ImageSaveOptions options = new ImageSaveOptions();
// Set the scaling of the exported image
options.setScaleX(2.0);
options.setScaleY(1.5);
// Set the dpi of the exported jpg file
options.setResolution(192);
worksheet.toImage(outputStream, ImageType.JPG, options);
// Close the file stream
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// Create a Image file stream
FileOutputStream("SetScaleAndResolution.png").use {
val outputStream = it
// Create a new workbook
var workbook = Workbook()
val worksheet = workbook.worksheets[0]
worksheet.getRange("B1").value = "Circle"
worksheet.getRange("B1").horizontalAlignment = HorizontalAlignment.Center
val circle = worksheet.shapes.addShape(AutoShapeType.Oval, 20.0, 20.0, 100.0, 100.0)
worksheet.getRange("E1").value = "Square"
worksheet.getRange("E1").horizontalAlignment = HorizontalAlignment.Center
val square = worksheet.shapes.addShape(AutoShapeType.Rectangle, 165.0, 20.0, 100.0, 100.0)
circle.textFrame.horizontalAnchor = HorizontalAnchor.Center
circle.textFrame.verticalAnchor = VerticalAnchor.AnchorMiddle
square.textFrame.horizontalAnchor = HorizontalAnchor.Center
square.textFrame.verticalAnchor = VerticalAnchor.AnchorMiddle
val options = ImageSaveOptions()
// Set the scaling of the exported image
options.scaleX = 2.0
options.scaleY = 1.5
// Set the dpi of the exported jpg file
options.resolution = 192
worksheet.toImage(outputStream, ImageType.JPG, options)
// End using the file stream
}