Solution Fonctionnelle pour le Redimensionnement des Feuilles de Calcul

Contexte

Dans l'article Ajouter des Cadres Ole, nous avons expliqué comment ajouter un Cadre Ole dans une présentation PowerPoint en utilisant Aspose.Slides pour Java. Afin de tenir compte du problème de changement d’objet, nous avons attribué l’image de la feuille de calcul de la zone sélectionnée au Cadre d’Objet OLE du graphique. Dans la présentation de sortie, lorsque nous double-cliquons sur le Cadre d’Objet OLE affichant l’image de la feuille de calcul, le graphique Excel est activé. Les utilisateurs finaux peuvent apporter les modifications souhaitées dans le classeur Excel réel puis revenir à la diapositive concernée en cliquant en dehors du classeur Excel activé. La taille du Cadre d’Objet OLE changera lorsque l’utilisateur revient à la diapositive. Le facteur de redimensionnement sera différent pour différentes tailles de Cadre d’Objet OLE et de classeur Excel intégré.

Cause du Redimensionnement

Étant donné que le classeur Excel a sa propre taille de fenêtre, il essaie de conserver sa taille d’origine lors de la première activation. D’autre part, le Cadre d’Objet OLE aura sa propre taille. Selon Microsoft, lors de l’activation du classeur Excel, Excel et PowerPoint négocient la taille et s’assurent qu’elle est dans les bonnes proportions dans le cadre de l’opération d’incrustation. En fonction des différences de taille de la fenêtre Excel et de la taille / position du Cadre d’Objet OLE, le redimensionnement se produit.

Solution Fonctionnelle

Il existe deux solutions possibles pour éviter l’effet de redimensionnement.* Redimensionner la taille du cadre Ole dans PPT pour correspondre à la taille en termes de hauteur / largeur du nombre souhaité de lignes / colonnes dans le Cadre Ole.* Garder la taille du cadre Ole constante et redimensionner la taille des lignes / colonnes participantes pour s’adapter à la taille du cadre Ole sélectionné.

Redimensionner la taille du cadre Ole aux lignes / colonnes sélectionnées de la feuille de calcul

Dans cette approche, nous apprendrons à définir la taille du cadre Ole du classeur Excel intégré équivalente à la taille cumulative du nombre de lignes et colonnes participantes dans la feuille de calcul Excel.

Exemple

Supposons que nous ayons défini un modèle de feuille de calcul Excel et que nous souhaitions l’ajouter à la présentation en tant que cadre Ole. Dans ce scénario, la taille du Cadre d’Objet OLE sera d’abord calculée en fonction de la hauteur cumulative des lignes et des largeurs des colonnes des lignes et colonnes participantes du classeur respectivement. Ensuite, nous définirons la taille du cadre Ole à cette valeur calculée. Pour éviter le message rouge Objet Intégré pour le cadre OLE dans PowerPoint, nous obtiendrons également l’image des portions souhaitées des lignes et colonnes dans le classeur et nous définirons cela comme image du cadre 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();
}

Redimensionner la hauteur des lignes de la feuille de calcul et la largeur des colonnes par rapport à la taille du cadre Ole

Dans cette approche, nous apprendrons comment redimensionner les hauteurs des lignes participantes et la largeur des colonnes participantes en fonction de la taille du cadre ole définie sur mesure.

Exemple

Supposons que nous ayons défini un modèle de feuille de calcul Excel et que nous souhaitions l’ajouter à la présentation en tant que cadre Ole. Dans ce scénario, nous définirons la taille du cadre Ole et redimensionnerons la taille des lignes et des colonnes participant à la zone du Cadre Ole. Nous enregistrerons ensuite le classeur dans un flux pour enregistrer les modifications et le convertir en tableau d’octets pour l’ajouter au cadre Ole. Pour éviter le message rouge Objet Intégré pour le cadre OLE dans PowerPoint, nous obtiendrons également l’image des portions souhaitées des lignes et colonnes dans le classeur et nous définirons cela comme image du cadre 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;
}

Conclusion