如何在xssfworkbook的同一单元格中获取图像和数据

问题描述 投票:0回答:1

我正在尝试使用apcahe poi xssf工作簿在同一单元格中插入iamge和一些数据。我已经尝试过类似下面的内容

        Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet)wb.createSheet();
    Row row = sheet.createRow(rowNum++);
       row.setHeight((short) 1000);
   String logoPath = confBean.getFacilityLogoByfacilityId(usersession);

    /* Read input PNG / JPG Image into FileInputStream Object*/
    InputStream logo = new FileInputStream(logoPath);
    /* Convert picture to be added into a byte array */
    byte[] bytes = IOUtils.toByteArray(logo);
    /* Add Picture to Workbook, Specify picture type as PNG and Get an Index */
    int my_picture_id = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
    /* Close the InputStream. We are ready to attach the image to workbook now */
    logo.close();
    /* Create the drawing container */
    XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
    /* Create an anchor point */
    //============= Inserting image - END

    //========adding image START
    XSSFClientAnchor my_anchor = new XSSFClientAnchor();
    /* Define top left corner, and we can resize picture suitable from there */

    my_anchor.setCol1(1); 
    my_anchor.setRow1(1); 
    my_anchor.setCol2(2); 
    my_anchor.setRow2(4); 

    /* Invoke createPicture and pass the anchor point and ID */
    XSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
    Row row1=sheet.createRow(1);
    row1.setHeight((short) 1000);
    row1.createCell(1).setCellValue(proxy.getAddress()+","+proxy.getCity()+","+proxy.getState()+","+proxy.getCountry()+"-"+proxy.getZip()+".\n Phone:"+proxy.getPhone()+"EMAIL:"+proxy.getEmail());

我不确定是否会得到如下所示的输出,请有人帮忙。

enter image description here

excel apache-poi workbox xssf apache-poi-4
1个回答
0
投票

Excel中的图片不是单元格内容。它们位于称为图纸的单独图层中,仅锚定到图纸的单元格上。因此,图片会悬停在单元格上方,也会悬停在单元格内容上方。如果文本和图片不应重叠,则必须相应地放置或对齐文本。

所以您的屏幕快照显示的是固定在A1并调整大小的图片,因此它适合前4行的高度。对于文本,范围A1:H4被合并。合并范围显示左上方单元格的内容。因此,文本必须设置在单元格A1中。需要一种用于设置水平对齐方式,垂直对齐方式和自动换行的单元格样式。这种样式导致在合并范围A1:H4中水平和垂直居中的文本。因此,文字和图片不会重叠。

完整示例:

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;

class CreateExcelPictureAndText {

 public static void main(String[] args) throws Exception {

  //Workbook workbook = new HSSFWorkbook(); String filePath = "./Excel.xls";
  Workbook workbook = new XSSFWorkbook(); String filePath = "./Excel.xlsx";

  Sheet sheet = workbook.createSheet();
  Row row = null;

  //create cell style horizontal alignment - center, vertical alignment - center, wrap text
  CellStyle cellStyle = workbook.createCellStyle();
  cellStyle.setAlignment(HorizontalAlignment.CENTER);
  cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  cellStyle.setWrapText(true);

  //insert picture's media into workbook
  InputStream inputStream = new FileInputStream("./logo.png");
  byte[] imageBytes = IOUtils.toByteArray(inputStream);
  int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
  inputStream.close();

  //insert picture anchored over the cells of the sheet
  CreationHelper helper = workbook.getCreationHelper();
  Drawing drawing = sheet.createDrawingPatriarch();
  ClientAnchor anchor = helper.createClientAnchor();
  anchor.setCol1(0); //col A
  anchor.setRow1(0); //row 1
  Picture pict = drawing.createPicture(anchor, pictureureIdx);
  pict.resize(); //now picture is anchored at A1 and sized to it's original size

  //get picture's original size
  int pictOriginalWidthInPixels = pict.getImageDimension().width;
  int pictOriginalHeightInPixels = pict.getImageDimension().height;

  //get height of row 1 to 4
  float rowHeightInPixels = 0f;
  for (int r = 0; r < 4; r++) {
   row = sheet.getRow(r); if (row == null) row = sheet.createRow(r);
   float rowHeightInPoints = row.getHeightInPoints(); 
   rowHeightInPixels += rowHeightInPoints * Units.PIXEL_DPI / Units.POINT_DPI;
  }
  //we want scaling in aspect ratio
  float scale = rowHeightInPixels / pictOriginalHeightInPixels;
  pict.resize(scale, scale); //now picture is resized to fit into the first 4 rows

  //create merged cells for heading
  sheet.addMergedRegion(new CellRangeAddress(0,3,0,7)); //merged region A1:H4

  //set text for merged region in A1
  row = sheet.getRow(0);
  Cell cell = row.createCell(0);
  cell.setCellValue("Golden Heights, 9/1, sector 3, Huda Techno Enclave,\n"
                   +"Madhapur (HITEC city), Hyderabad, Telangana - 500 081, India.\n"
                   +"Phone: 91 40.23116868 Email: [email protected]"); 
  cell.setCellStyle(cellStyle);

  //set column widths
  for (int c = 0; c < 8; c++) {
   sheet.setColumnWidth(c, 15*256); //column width 15 default character widths
  }

  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  workbook.close();

 }
}

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.