2014-02-06 55 views
0

如何根據數據長度在我的excel報告中使用PHPexcel調整列寬?....以及如何使excel中的所有數據居中?居中,並根據數據長度調整excel列

enter image description here

當前代碼:

<?php 
if (!isset($_POST['send'])) { ?> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <title>test</title> 
    </head> 
    <body> 
<?php } else { 
    require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php'; 
    $filename = 'file.xlsx'; 
    $title = $_POST['title']; 
    mysql_connect("localhost","root","") or die ("cant connect!"); 
    mysql_select_db("test") or die ("cant find database!"); 

    $objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    $objReader->setReadDataOnly(true); 

    $objPHPExcel = $objReader->load($filename); 
    $objWorksheet = $objPHPExcel->getActiveSheet(); 
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 

    $result = mysql_query("SELECT * FROM score"); 
    if(isset($_POST['send'])){ 

     $headings = array(
      'ID', 
      'NAME', 
      'SCORE 1', 
      'SCORE 2', 
      'OTHER QUALITIES', 
      'INTERVIEW', 
      'TOTAL', 
      'AIC', 
      'BATCHCODE', 
     ); 
     $objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1'); 
     $row = 2; 
     while($rows = mysql_fetch_row($result)){ 
      $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row); 
      $row++; 
     } 
    } 

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
    header('Content-Disposition: attachment;filename="'.$title.'.xlsx"'); 
    header('Cache-Control: max-age=0'); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
    $objWriter->save('php://output'); 
} 
if (!isset($_POST['send'])) { ?> 

    <form id="form1" name="form1" method="post" action="" > 
    <input name="title" type="text" id="title" value="title" /> 
    <input type="submit" name="send" value="send to excel" id="send" /> 
    </form> 
    </body> 
    </html> 
<?php } 

我想是......列將根據存儲的數據長度自動調節...,使DATAS在中心。

回答

1

顯影劑文件的第4.6.28(題爲「設置列的寬度」)示出了如何設置一個列寬,與設置它的一個例子,以自動調整其寬度來擬合數據:

如果您希望PHPExcel執行自動寬度計算,請使用以下代碼。 PHPExcel將近似列的寬度爲最寬列值。

$objPHPExcel->getActiveSheet() 
    ->getColumnDimension('E')->setAutoSize(true); 

對齊(如中心)可以按照顯影劑文件的第4.6.18的指令(題爲「格式化單元」)被設置

$objPHPExcel->getActiveSheet() 
    ->getStyle('C2:I5') 
    ->getAlignment() 
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);