6

在我的应用程序中,我需要以xls预定义的格式导出到文件。

所以我只是整合php_excel2007。我正在使用一个具有预定义格式的模板。

这里的问题是单元格数据可能会动态变化。如果数据远大于单元格高度,则数据正在崩溃。

那么无论如何要根据单元格的内容增加单元格的高度(在 XLX 而不是 xlsx 中)?

4

3 回答 3

5

唯一的方法是在这个主题的上一个问题的答案中描述:将行高设置为自动调整,并将单元格对齐设置为换行。无论您使用 Excel5 Writer 还是 Excel2007 Writer,这应该以相同的方式工作。

$objPHPExcel = new PHPExcel();

// Set some long string values in some cells
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue("This is a large block of text,\ncomprising several lines,\nthat will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A2')->setValue("This is a large block of text that will NOT be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('B1')->setValue("This is another large block of text without any line breaks, that will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A3')->setValue("This is another large block of text,\ncomprising several lines,\nthat will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A4')->setValue("This is another large block of text without any line breaks, that will be set to autofit but not wrap.");
// Fix the column width to a reasonable size
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
//  Set text wrap for cells A1 and B1. This forces the text to wrap, but doesn't adjust the height of the row
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setWrapText(true);
// Set rows 1, 3 and 4 to autofit the height to the size of text
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getRowDimension(4)->setRowHeight(-1);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('testAutoHeight.xls');

编辑

在代码示例中添加了注释,以解释每个被调用方法的实际作用

于 2010-10-12T11:23:23.243 回答
1
<?php
error_reporting(E_ALL);
require_once ROOT.'/PHPExcel.php';

function down($details)
{
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();
    $objReader = PHPExcel_IOFactory::createReader('Excel5');
    $objPHPExcel = $objReader->load(ROOT."/templates/project_report1.xls");

    // Set properties
    $p_i=5;$alpa="B";$row_no=5;$mil="";$d_cel="B";
    $objPHPExcel->setActiveSheetIndex()
                ->setCellValue('K1', $details['report_details']['cur_time']);
    $objPHPExcel->setActiveSheetIndex()
                ->setCellValue('C2', 'REPORT START DATE:'.$details['report_details']['start_date'])
                ->setCellValue('H2', $details['report_details']['details'])
                ->setCellValue('C3', 'SHOWING:'.$details['report_details']['showing']);
    foreach($details as $p_name=>$date){
        //thisis to display date at the top 
        foreach($date as $p1=>$m_name1){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($d_cel.'4', $p1);$d_cel++;//to display date in the top
        }
        break;
    }

    foreach($details as $p_name=>$date){
        if($p_name=="report_details")break;
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$p_i, $p_name);
        $objPHPExcel->getActiveSheet(0)->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A5'), 'A'.$p_i );
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$p_i)->getAlignment()->setWrapText(true);    

        foreach($date as $p=>$m_name){
            $mil=$tic=$st=" ";

            foreach($m_name as $name=>$val){

            if($name=="milestone")
            foreach($val as $in_det=>$det){
                if($det && isset($det['start_date']))
                    $mil.=$det['name']."\n".$det['start_date']."\n";
                else
                    $mil.=$det['name'];
            }
            if($name=="ticket")
                foreach($val as $in_det=>$det){                             
                    if($det)
                        $tic.=$det['name']." ".$det['start_date']."\n";
                }
            if($name=="task")
                foreach($val as $in_det=>$det){
                    if($det)
                        $st.=$det['name']." ".$det['start_date']."\n";
                }
            }
            $summary=$mil.$tic.$st;
            $objPHPExcel->getActiveSheet(0)->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('B5'), $alpa.$p_i );
            $objPHPExcel->getActiveSheet(0)->getRowDimension($p_i)->setRowHeight();
            $objPHPExcel->getActiveSheet(0)->getStyle('A'.$p_i.':'.'M'.$p_i)->getAlignment()->setWrapText(true);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle($alpa.$p_i)->getFont()->setSize(5);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($alpa.$p_i, $summary);
            $alpa++;
        }
        $alpa="B";
        $p_i++;
    }
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle('Report');
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet

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

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
}
?>
于 2010-10-12T11:46:20.093 回答
1

setRowHeight 为 -1 以生成自动高度(基于单元格数据)

// auto-size on row 1

$objWorksheet->getRowDimension('1')->setRowHeight(-1);
于 2011-05-11T05:02:34.127 回答