ワークシートサイズ変更のための作業ソリューション

背景

OLEフレームの追加に関する記事では、Aspose.Slides for Javaを使用してPowerPointプレゼンテーションにOLEフレームを追加する方法を説明しました。オブジェクト変更の問題に対処するために、選択した領域のワークシート画像をチャートOLEオブジェクトフレームに割り当てました。出力プレゼンテーションでは、ワークシート画像を表示するOLEオブジェクトフレームをダブルクリックすると、Excelチャートがアクティブになります。エンドユーザーは実際のExcelワークブックに望む変更を加え、その後アクティブ化されたExcelワークブックの外をクリックすることで関連するスライドに戻ることができます。スライドに戻ると、OLEオブジェクトフレームのサイズが変更されます。リサイズ係数は、OLEオブジェクトフレームと埋め込まれたExcelワークブックの異なるサイズによって異なります。

リサイズの原因

Excelワークブックには独自のウィンドウサイズがあるため、最初のアクティベーション時に元のサイズを維持しようとします。一方、OLEオブジェクトフレームにも独自のサイズがあります。Microsoftによれば、Excelワークブックがアクティブになると、ExcelとPowerPointはサイズを交渉し、埋め込み操作の一環として正しい比率になるようにします。ExcelウィンドウのサイズとOLEオブジェクトフレームのサイズ/位置による違いに基づいてリサイズが行われます。

作業ソリューション

リサイズ効果を回避するための2つの可能なソリューションがあります。* OLEフレームのサイズをPPT内で目的の行/列数の高さ/幅に合わせてスケールする* OLEフレームのサイズを一定に保ち、参加する行/列のサイズをスケールして選択されたOLEフレームサイズに合わせる

OLEフレームサイズをワークシートの選択された行/列サイズにスケールする

このアプローチでは、埋め込まれたExcelワークブックのOLEフレームサイズをExcelワークシートの参加行および列の合計サイズに等しく設定する方法を学びます。

テンプレートExcelシートが定義されており、それをOLEフレームとしてプレゼンテーションに追加したいとします。このシナリオでは、OLEオブジェクトフレームのサイズは、参加するワークブックの行および列の高さと幅の合計に基づいて最初に計算されます。その後、OLEフレームのサイズをその計算された値に設定します。PowerPointでOLEフレームに対する赤い埋め込まれたオブジェクトメッセージを回避するために、ワークブック内の望ましい行と列の部分の画像も取得し、それをOLEフレームの画像として設定します。

try {
WorkbookDesigner workbookDesigner = new WorkbookDesigner();
Workbook workbook = new Workbook("AsposeTest.xls");
workbookDesigner.setWorkbook(workbook);
Presentation presentation = new Presentation("AsposeTest.ppt");
ISlide slide = presentation.getSlides().get_Item(0);
//Setting Ole frame according to Row, Columns
AddOLEFrame(slide, 0, 15, 0, 3, 0, 300, 1100, 0, 0, presentation, workbookDesigner, true, 0, 0);
presentation.save("AsposeTest_Ole.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}
private static void AddOLEFrame(ISlide slide, int startRow, int endRow, int startCol, int endCol,
int dataSheetIdx, int x, int y, double OleWidth, double OleHeight,
Presentation presentation, WorkbookDesigner workbookDesigner,
boolean onePagePerSheet, int outputWidth, int outputHeight) throws Exception//String sheetName,
{
String path="D:\\";
String tempFileName = path +"tempImage";
if (startRow == 0)
{
startRow++;
endRow++;
}
//Setting active sheet index of workbook
workbookDesigner.getWorkbook().getWorksheets().setActiveSheetIndex (dataSheetIdx);
SetWorkBookArea(startRow, endRow, startCol, endCol, dataSheetIdx, workbookDesigner);
//Getting Workbook and selected worksheet
Workbook workbook = workbookDesigner.getWorkbook();
Worksheet work=workbook.getWorksheets().get(dataSheetIdx);
Dimension SlideOleSize=SetOleAccordingToSelectedRowsCloumns(workbook, startRow, endRow, startCol, endCol, dataSheetIdx);
OleWidth = SlideOleSize.getWidth();
OleHeight = SlideOleSize.getHeight();
//Set Ole Size in Workbook
workbook.getWorksheets().setOleSize(startRow, endRow, startCol, endCol);
work.setGridlinesVisible( false);
//Setting Image Options to take the worksheet Image
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.setImageFormat(com.aspose.cells.ImageFormat.getBmp());
imageOrPrintOptions.setOnePagePerSheet( onePagePerSheet);
SheetRender render = new SheetRender(work, imageOrPrintOptions);
String ext = ".bmp";
render.toImage(0, tempFileName + ext);
BufferedImage tempImage = ImageIO.read(new File(tempFileName + ext));
BufferedImage image = ScaleImage(tempImage, outputWidth, outputHeight);
String newTempFileName = "NewTemp";
ImageIO.write(image,"bmp", new File(newTempFileName+ext));
//Adding Image to slide picture collection
//Creating a stream to hold the image file
InputStream iStream = new BufferedInputStream(new FileInputStream(newTempFileName+ext));
IPPImage imgx = null;
imgx = presentation.getImages().addImage(iStream);
//Saving worbook to stream and copying in byte array
ByteArrayOutputStream mstream = new ByteArrayOutputStream ();
workbook.save(mstream,com.aspose.cells.SaveFormat.EXCEL_97_TO_2003);
//Adding Ole Object frame
IOleObjectFrame oleObjectFrame = slide.getShapes().addOleObjectFrame(x, y, (int)OleWidth,(int)OleHeight, "Excel.Sheet.8", mstream.toByteArray());
//Setting ole frame Imnae and Alternative Text property
oleObjectFrame.getSubstitutePictureFormat().getPicture().setImage(imgx);
oleObjectFrame.setAlternativeText( "image." + imgx.getContentType());
}
private static java.awt.Dimension SetOleAccordingToSelectedRowsCloumns(Workbook workbook, int startRow, int endRow, int startCol,
int endCol, int dataSheetIdx)
{
Worksheet work = workbook.getWorksheets().get(dataSheetIdx);
double actualHeight = 0, actualWidth = 0;
for (int i = startRow; i <= endRow; i++)
actualHeight += work.getCells().getRowHeightInch(i);
for (int i = startCol; i <= endCol; i++)
actualWidth += work.getCells().getColumnWidthInch(i);
//Setting new Row and Column Height
double width=0, height=0;
width=actualWidth * 576;
height=actualHeight * 576;
return new java.awt.Dimension((int)(width), (int)(height));
}
private static BufferedImage ScaleImage(BufferedImage image, int outputWidth, int outputHeight)
{
if (outputWidth == 0 && outputHeight == 0)
{
outputWidth = image.getWidth();
outputHeight = image.getHeight();
}
//BufferedImage outputImage=new BufferedImage();
BufferedImage resized = new BufferedImage(outputWidth, outputHeight, image.getType());
Graphics2D g = resized.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, outputWidth, outputHeight, 0, 0,image.getWidth(), image.getHeight(), null);
g.dispose();
return resized;
}
private static void SetWorkBookArea(int startRow, int endRow, int startCol, int endCol,
int dataSheetIdx, WorkbookDesigner workbookDesigner)
{
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setPrintArea( PrintArea(startRow, endRow, startCol, endCol));
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setBottomMargin(0);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setFooterMargin(0);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setTopMargin (0);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setLeftMargin ( 0);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setRightMargin ( 0);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setZoom (100);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setPrintGridlines(false);
workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx).getPageSetup().setPrintQuality( 600);
}
private static String PrintArea(int startRow, int endRow, int startCol, int endCol)
{
return ExcelColumnLetter(startCol) + startRow + ":" + ExcelColumnLetter(endCol) + endRow;
}
private static String ExcelColumnLetter(int intCol)
{
int intFirstLetter = ((intCol) / 26) + 64;
int intSecondLetter = (intCol % 26) + 65;
char letter1 = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
String tem=Character.toString(letter1) + Character.toString((char)intSecondLetter);
return tem.trim();
}

OLEフレームサイズに応じてワークシートの行の高さと列の幅をスケールする

このアプローチでは、カスタム設定されたOLEフレームサイズに従って参加行の高さと参加列の幅をスケールする方法を学びます。

テンプレートExcelシートが定義されており、それをOLEフレームとしてプレゼンテーションに追加したいとします。このシナリオでは、OLEフレームのサイズを設定し、そのOLEフレームエリアに参加する行と列のサイズをスケールします。その後、変更を保存するためにワークブックをストリームに保存し、OLEフレームに追加するためにバイト配列に変換します。PowerPointでOLEフレームに対する赤い埋め込まれたオブジェクトメッセージを回避するために、ワークブック内の望ましい行と列の部分の画像も取得し、それをOLEフレームの画像として設定します。

try {
WorkbookDesigner workbookDesigner = new WorkbookDesigner();
Workbook workbook = new Workbook("AsposeTest.xls");
workbookDesigner.setWorkbook(workbook);
Presentation presentation = new Presentation("AsposeTest.ppt");
ISlide slide = presentation.getSlides().get_Item(0);
//Setting Ole frame according to Row, Columns
AddOLEFrame(slide, 0, 15, 0, 3, 0, 300, 1100, 0, 0, presentation, workbookDesigner, true, 0, 0);
presentation.save("AsposeTest_Ole.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}
private static void SetOleAccordingToCustomHeighWidth(Workbook workbook, int startRow,
int endRow, int startCol, int endCol, double slideWidth, double slideHeight, int dataSheetIdx)
{
Worksheet work = workbook.getWorksheets().get(dataSheetIdx);
double actualHeight = 0, actualWidth = 0;
double newHeight = slideHeight;
double newWidth = slideWidth;
double tem = 0;
double newTem = 0;
for (int i = startRow; i <= endRow; i++)
actualHeight += work.getCells().getRowHeightInch(i);
for (int i = startCol; i <= endCol; i++)
actualWidth += work.getCells().getColumnWidthInch(i);
///Setting new Row and Column Height
for (int i = startRow; i <= endRow; i++)
{
tem = work.getCells().getRowHeightInch(i);
newTem = (tem / actualHeight) * newHeight;
work.getCells().setRowHeightInch(i, newTem);
}
for (int i = startCol; i <= endCol; i++)
{
tem = work.getCells().getColumnWidthInch(i);
newTem = (tem / actualWidth) * newWidth;
work.getCells().setColumnWidthInch(i, newTem);
}
}
private static void AddOLEFrame(ISlide slide, int startRow, int endRow, int startCol, int endCol,
int dataSheetIdx, int x, int y, int OleWidth, int OleHeight,
Presentation presentation, WorkbookDesigner workbookDesigner,
Boolean onePagePerSheet, int outputWidth, int outputHeight)
{
try {
String path = "D:\\";
String tempFileName = path +"tempImage";
if (startRow == 0)
{
startRow++;
endRow++;
}
//Setting active sheet index of workbook
workbookDesigner.getWorkbook().getWorksheets().setActiveSheetIndex(dataSheetIdx);
//Getting Workbook and selected worksheet
Workbook workbook = workbookDesigner.getWorkbook();
Worksheet work=workbook.getWorksheets().get(dataSheetIdx);
//Scaling rows height and coluumns width according to custom Ole size
double height = OleHeight / 576f;
double width = OleWidth / 576f;
SetOleAccordingToCustomHeighWidth(workbook, startRow, endRow, startCol, endCol, width, height, dataSheetIdx);
//Set Ole Size in Workbook
workbook.getWorksheets().setOleSize(startRow, endRow, startCol, endCol);
workbook.getWorksheets().get(0).setGridlinesVisible(false);
//Setting Image Options to take the worksheet Image
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.setImageFormat(ImageFormat.getBmp());
imageOrPrintOptions.setOnePagePerSheet(onePagePerSheet);
SheetRender render = new SheetRender(workbookDesigner.getWorkbook().getWorksheets().get(dataSheetIdx), imageOrPrintOptions);
String ext = ".bmp";
render.toImage(0, tempFileName + ext);
BufferedImage tempImage = ImageIO.read(new File(tempFileName + ext));
BufferedImage image = ScaleImage(tempImage, outputWidth, outputHeight);
String newTempFileName = "NewTemp";
ImageIO.write(image,"bmp", new File(newTempFileName+ext));
//Adding Image to slide picture collection
//Creating a stream to hold the image file
InputStream iStream = new BufferedInputStream(new FileInputStream(newTempFileName+ext));
IPPImage imgx = null;
imgx = presentation.getImages().addImage(iStream);
//Saving worbook to stream and copying in byte array
ByteArrayOutputStream mstream = new ByteArrayOutputStream ();
workbook.save(mstream,com.aspose.cells.SaveFormat.EXCEL_97_TO_2003);
//Adding Ole Object frame
OleObjectFrame oleObjectFrame = (OleObjectFrame) slide.getShapes().addOleObjectFrame(x, y, (int)OleWidth,(int)OleHeight, "Excel.Sheet.8", mstream.toByteArray());
//Setting ole frame Imnae and Alternative Text property
oleObjectFrame.getSubstitutePictureFormat().getPicture().setImage(imgx);
oleObjectFrame.setAlternativeText( "image." + imgx.getContentType());
} catch (Exception e) {
}
}
private static BufferedImage ScaleImage(BufferedImage image, int outputWidth, int outputHeight)
{
if (outputWidth == 0 && outputHeight == 0)
{
outputWidth = image.getWidth();
outputHeight = image.getHeight();
}
//BufferedImage outputImage=new BufferedImage();
BufferedImage resized = new BufferedImage(outputWidth, outputHeight, image.getType());
Graphics2D g = resized.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, outputWidth, outputHeight, 0, 0,image.getWidth(), image.getHeight(), null);
g.dispose();
return resized;
}

結論