我使用PHPExcel和codeigniter生成.xlx,.xlxs文件,除文本格式外,每件事情都很完美。这是html视图的屏幕截图,我将传递给PHPExcel库以生成.xlx文件。
如您所见,删除了文本缩进和样式。这是我用于生成输出的代码。
public function html_to_excel_download($filename, $data=null){
if ($data != null) {
// proper encoding of data UTF-8 for unicode characters
$data = chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $data);
// Put the data into a temporary file
$tmpfile = time().'.html';
file_put_contents($tmpfile, $data);
// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
// Excel Writer
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
// Download File
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
unlink($tmpfile);
exit();
}
}
如何将标题文字缩进到中心?
答案 0 :(得分:3)
你可以尝试另一种方式来做到这一点...... 步骤1)定义" $ this-> excel-> setActiveSheetIndex(0);"在功能的顶部
步骤2)定义样式数组并将其添加为具有excel对象的变量,如
<div id="firstDiv">
<div id="secondDiv">
<div id="thirdDiv">
</div>
</div>
</div>
第3步)$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '2F4F4F')
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
)
);
注意:您必须在代码中使用getStyle()才能在列/行上应用特定样式
答案 1 :(得分:0)
// single column A, setting e.g. horizontal alignment
$objWorksheet->getStyle('A')->getAlignment()->setHorizontal(...);
// range of columns A to K
$objWorksheet->getStyle('A:K')->getAlignment()->setHorizontal(...);
您可以在此网站上查看有关格式化SHEET的完整文档 HERE