>();

String[] assetHeadTemp = { "商品名称", "销量", "库存", "周转率", "图片" };

String[] assetNameTemp = { "maktx", "saleCount", "stockCount", "rotationRate", "matnr" };

Workbook wb = new SXSSFWorkbook(100);

Sheet sheet = wb.createSheet("Sheet1");

Row row;

Cell cell;

// 图片字节数组

byte[] imgByte = null;

// excel样式

CellStyle style = wb.createCellStyle();

style.setAlignment(CellStyle.ALIGN_CENTER);// 水平

style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直

// 输出表头

row = sheet.createRow(0);

for (int i = 0; i < assetHeadTemp.length; i++) {

cell = row.createCell(i);

cell.setCellValue(assetHeadTemp[i]);

cell.setCellStyle(style);

}

// 输出内容

int rowIndex = 1;

for (Mapmap : dataList) {

row = sheet.createRow(rowIndex++);

// 设置行高

row.setHeightInPoints((short) 70);

int index = 0;

File file = null;

String imgPath;

for (int i = 0; i < assetNameTemp.length; i++) {

cell = row.createCell(i);

// 设置列宽

sheet.setColumnWidth(i, 256 * 25);

// 输出图片到第5列

if (i == 4) {

imgByte = null;

// 输出图片

imgPath = "E:\\图片测试\\materialThumbnail\\"

+ map.get(assetNameTemp[4]).toString() + ".jpg";

file = new File(imgPath);

if (file.exists()) {

// 图片转化为字节数组

imgByte = IOUtils.toByteArray(new FileInputStream(imgPath));

}

if (imgByte != null) {

// 图片存在即输出图片

int addPicture = wb.addPicture(imgByte, wb.PICTURE_TYPE_JPEG);

Drawing drawing = sheet.createDrawingPatriarch();

CreationHelper helper = wb.getCreationHelper();

ClientAnchor anchor = helper.createClientAnchor();

anchor.setRow1(rowIndex - 1);

anchor.setCol1(i);

// 指定我想要的长宽

double standardWidth = 100;

double standardHeight = 100;

// 计算单元格的长宽

double cellWidth = sheet.getColumnWidthInPixels(cell.getColumnIndex());

double cellHeight = cell.getRow().getHeightInPoints() / 72 * 96;

// 计算需要的长宽比例的系数

double a = standardWidth / cellWidth;

double b = standardHeight / cellHeight;

Picture picture = drawing.createPicture(anchor, addPicture);

picture.resize(a, b);

}

} else {

// 输出文字

cell.setCellValue(

map.get(assetNameTemp[index]) != null ? map.get(assetNameTemp[index]).toString() : "");

cell.setCellStyle(style);

index++;

}

}

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐