gpt4 book ai didi

PHPExcel:以像素为单位设置行高

转载 作者:可可西里 更新时间:2023-11-01 13:00:00 29 4
gpt4 key购买 nike

我正在使用 PHPExcel 通过 PHP 将一组图像写入 Excel 文件。插入图像似乎不是问题,但是我在设置适当的行高以匹配图像高度时遇到了一些困难。

我设法使用 pixelsToCellDimension 来计算正确的列宽,但这似乎不适合计算行高。在检查了 PHPExcel_Shared_Drawing 类中的其余函数并谷歌搜索之后,我对如何执行此操作感到目瞪口呆。

到目前为止我的代码:

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("creator")
->setLastModifiedBy("modifiedby")
->setTitle("Title")
->setSubject("Subject")
->setDescription("Description")
->setKeywords("keyword1 keyword2")
->setCategory("category");

// Get default font
$defaultFont = $objPHPExcel->getDefaultStyle()->getFont();

[生成我的图像并将它们的路径添加到数组的一些代码]

foreach( $filePaths as $i => $filePath ) {
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorkSheet( $objPHPExcel->getActiveSheet() );
$objDrawing->setName("name");
$objDrawing->setDescription("Description");
$objDrawing->setPath( $filePath );

$size = getimagesize($filePath);
$columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($size[0], $defaultFont);
$rowHeight = PHPExcel_Shared_Drawing::pixelsToCellDimension($size[1], $defaultFont);

$objDrawing->setCoordinates('A' . ($i+1) );
$objDrawing->setOffsetX(0);
$objDrawing->setOffsetY(0);

// set row/column (cell) sizes to match image size
$objPHPExcel->getActiveSheet()
->getColumnDimension('A')
->setWidth( $columnWidth );
$objPHPExcel->getActiveSheet()
->getRowDimension( $i+1 )
->setRowHeight( $rowHeight );
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Worksheet 1');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="myMCS Scancode Export.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

最佳答案

我使用 pixelsToPoints 作为高度

$default_font = $spreadsheet->getDefaultStyle()->getFont();
$image_height = imageSY($image);
$image_width = imageSX($image);
$image_height_pt = Drawing::pixelsToPoints($image_height);
$image_width_pt = Drawing::pixelsToCellDimension($image_width,$default_font);

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
$drawing->setImageResource($image);
$drawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG);
$drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
$drawing->setCoordinates($col.$row);
$drawing->setHeight($image_height);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
$spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight($image_height_pt);
$spreadsheet->getActiveSheet()->getColumnDimension($col)->setWidth($image_width_pt);

版本 1.8.0

关于PHPExcel:以像素为单位设置行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042925/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com