使用PHPExcel时,为什么自动调整大小会拉伸我的图像?

时间:2013-07-22 01:25:42

标签: php phpexcel

我正在使用PHPExcel Class创建电子表格。我有几列数据。我正在使用autosize方法,以使它们获取每列中最长数据值的大小。

编辑 - 以下是我的完整代码,以显示更多详细信息:

require_once '../../Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

// Adding values for Header Row
$objPHPExcel->getActiveSheet()
            ->setCellValue('A3', '#')
            ->setCellValue('B3', 'Name')
            ->setCellValue('C3', 'Email')
            ->setCellValue('D3', 'Phone')
            ->setCellValue('E3', 'Address')
            ->setCellValue('F3', 'Date Registered');

//Setting column values to the Listers Spreadsheet  
$counter = 4;
$entries_num = 1;          
//while($listers_export_row = $listers_export_result->fetch_assoc()) {
while($listers_export_row = $listers_export_result->fetch(PDO::FETCH_ASSOC)) {
    //Adding Numbers to Listers Spreadsheet (Column A)
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$counter, $entries_num);

    //Adding Names to Listers Spreadsheet (Column B)
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$counter, $listers_export_row['first_name'].' '.$listers_export_row['last_name']);

    //Adding Email to Listers Spreadsheet (Column C) 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$counter, $listers_export_row['email']);

    //Adding Phone to Listers Spreadsheet (Column D)
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$counter, '('.$listers_export_row['phone_prefix'].')'.$listers_export_row['phone_first'].'-'.$listers_export_row['phone_last']);

    //Adding Address to Listers Spreadsheet (Column E) 
    $address = $listers_export_row['address'];
        if($listers_export_row['apt'] != '') { $address .= ' '.$listers_export_row['apt']; }
    $address .= ', '.$listers_export_row['city'].', '.$listers_export_row['state'].' '.$listers_export_row['zip_1'];

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$counter, $address);

    //Adding Date Registered to Listers Spreadsheet (Column F)  
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$counter, date('m/d/y',$listers_export_row['date_created']));

    $counter++;
    $entries_num++;
}

// Forces the spreadsheet to take the size of the longest value             
for ($col = 'A'; $col != 'G'; $col++) { //Runs through all cells between A and E and sets to autosize
    $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
}

// Add a drawing to the worksheet

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setResizeProportional(false);
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('../images/crl_logo.png');
$objDrawing->setHeight(35);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Accounts - '.date('m.d.y',time()).'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

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

这就是发生的事情(它应该只与第一列一样宽): enter image description here

我删除了自动调整大小的方法并且它的大小正确,所以我知道它会影响图像的大小。

如何从图像中删除自动调整大小但是将其应用于数据列?

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,我找到了解决方法。 在渲染文档之前,手动初始化计算,读取列的调整宽度,并手动设置。

$sheet = $this->phpExcelObject->getActiveSheet();
$sheet->calculateColumnWidths();
$columnDimension = $sheet->getColumnDimension('B');
$columnDimension->setAutoSize(false)->setWidth($columnDimension->getWidth());

答案 1 :(得分:1)

您是否尝试过设置固定图像宽度。

或者设置图像的偏移量以设置正确的位置。

PHPExcel_Worksheet_BaseDrawing::setOffsetX()
PHPExcel_Worksheet_BaseDrawing::setOffsetY()

更多详细信息,请参阅:http://www.cmsws.com/examples/applications/phpexcel/Documentation/API/PHPExcel_Worksheet/PHPExcel_Worksheet_Drawing.html

答案 2 :(得分:0)

在使用writerType xls创建文件时,PHPSpreadsheet中也存在同样的问题,只要将单元格设置为自动调整大小,图像就会自动拉伸。对于setResizeProportional,setWidthAndHeight,setOffsetX等常见建议,我不会碰运气。

将其更改为xslx(Excel2007)立即解决了图像问题。但是,当 打开生成的文件,将出现错误消息:

We found a problem with some content in <filename>.xlsx. Do you want us to try and 
recover as much as we can? If you trust the source of this workbook, click Yes.

PhpSpreadsheet is corrupting files上alex所回答,可以通过在调用save之后添加骰子来解决,例如:

...
$writer->save('php://output');
die;

希望这将帮助遇到相同问题的任何人。