我在下面使用一行不换行,并且它可以在excel导出中使用,但是当我尝试使用mPDF导出PDF时,如果有空格并且感觉表格很狭窄,则将换行。有什么想法可以防止这种情况吗?
提到的行是$objWorksheet->getCellByColumnAndRow($excelCol,$excelRow)->getStyle()->getAlignment()->setWrapText(false);
。它适用于excel,但不适用于pdf导出。
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load("excel.xlsx");
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = dirname(__FILE__) . '/mpdf';
$objWorksheet = $objPHPExcel->getActiveSheet();
$excelRow = 1;
$excelCol = 0;
$columns = Array('Title 1','Title 2','Title 3');
foreach($columns as $column_title)
{
$objWorksheet->setCellValueByColumnAndRow($excelCol, $excelRow, $column_title);
$objWorksheet->getCellByColumnAndRow($excelCol,$excelRow)->getStyle()->applyFromArray(
array(
'font' => array(
'bold' => true,
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
),
'borders' => array(
'top' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array('rgb' => '000000')
),
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array('rgb' => '000000')
)
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'd9d9d9')
)
)
);
$objWorksheet->getColumnDimensionByColumn($excelCol)->setAutoSize(true);
$objWorksheet->getCellByColumnAndRow($excelCol,$excelRow)->getStyle()->getAlignment()->setWrapText(false);
$excelCol++;
}
$objWorksheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objWorksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER);
$objWorksheet->getPageSetup()->setFitToPage(true);
$objWorksheet->getPageSetup()->setFitToWidth(1);
$objWorksheet->getPageSetup()->setFitToHeight(0);
$objWorksheet->getPageMargins()->setTop(0.5);
$objWorksheet->getPageMargins()->setRight(0.7);
$objWorksheet->getPageMargins()->setBottom(0.5);
$objWorksheet->getPageMargins()->setLeft(0.75);
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
}
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="pdf.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
exit;