工作表调整大小的有效解决方案

背景

添加 Ole 框架文章 中,我们解释了如何使用 Aspose.Slides for PHP via Java 在 PowerPoint 演示文稿中添加 Ole 框架。为了适应 对象改变问题,我们将选定区域的工作表图像分配给图表 OLE 对象框。在输出演示文稿中,当我们双击显示工作表图像的 OLE 对象框时,Excel 图表被激活。最终用户可以在实际的 Excel 工作簿中进行任何所需的更改,然后通过点击激活的 Excel 工作簿外部返回到相关幻灯片。当用户返回幻灯片时,OLE 对象框的大小会发生变化。对于不同大小的 OLE 对象框和嵌入的 Excel 工作簿,调整大小的因子将会不同。

调整大小的原因

由于 Excel 工作簿具有其自己的窗口大小,它在第一次激活时会尝试保持原始大小。另一方面,OLE 对象框会有其自己的大小。根据微软的说法,在激活 Excel 工作簿时,Excel 和 PowerPoint 会协商大小,并确保它在嵌入操作中处于正确的比例。根据 Excel 窗口大小与 OLE 对象框大小/位置的差异,会发生调整大小。

有效解决方案

避免重新调整大小效果有两种可能的解决方案。* 将 PPT 中的 Ole 框架大小调整为与 Ole 框架中所需行/列的高度/宽度相匹配* 保持 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;
}

结论