Example #1
0
 /**
  * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
  * x is the width in intrinsic Excel units (measuring width in number of normal characters)
  * This holds for Arial 10
  *
  * @param \PHPExcel\Worksheet $sheet The sheet
  * @param string $col The column
  * @return integer The width in pixels
  */
 public static function sizeCol($sheet, $col = 'A')
 {
     // default font of the workbook
     $font = $sheet->getParent()->getDefaultStyle()->getFont();
     $columnDimensions = $sheet->getColumnDimensions();
     // first find the true column width in pixels (uncollapsed and unhidden)
     if (isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != -1) {
         // then we have column dimension with explicit width
         $columnDimension = $columnDimensions[$col];
         $width = $columnDimension->getWidth();
         $pixelWidth = \PHPExcel\Shared\Drawing::cellDimensionToPixels($width, $font);
     } elseif ($sheet->getDefaultColumnDimension()->getWidth() != -1) {
         // then we have default column dimension with explicit width
         $defaultColumnDimension = $sheet->getDefaultColumnDimension();
         $width = $defaultColumnDimension->getWidth();
         $pixelWidth = \PHPExcel\Shared\Drawing::cellDimensionToPixels($width, $font);
     } else {
         // we don't even have any default column dimension. Width depends on default font
         $pixelWidth = \PHPExcel\Shared\Font::getDefaultColumnWidthByFont($font, true);
     }
     // now find the effective column width in pixels
     if (isset($columnDimensions[$col]) and !$columnDimensions[$col]->getVisible()) {
         $effectivePixelWidth = 0;
     } else {
         $effectivePixelWidth = $pixelWidth;
     }
     return $effectivePixelWidth;
 }
Example #2
0
 /**
  * Write Cols
  *
  * @param    \PHPExcel\Shared\XMLWriter $objWriter XML Writer
  * @param    \PHPExcel\Worksheet $pSheet Worksheet
  * @throws    \PHPExcel\Writer\Exception
  */
 private function writeCols(\PHPExcel\Shared\XMLWriter $objWriter = null, \PHPExcel\Worksheet $pSheet = null)
 {
     // cols
     if (count($pSheet->getColumnDimensions()) > 0) {
         $objWriter->startElement('cols');
         $pSheet->calculateColumnWidths();
         // Loop through column dimensions
         foreach ($pSheet->getColumnDimensions() as $colDimension) {
             // col
             $objWriter->startElement('col');
             $objWriter->writeAttribute('min', \PHPExcel\Cell::columnIndexFromString($colDimension->getColumnIndex()));
             $objWriter->writeAttribute('max', \PHPExcel\Cell::columnIndexFromString($colDimension->getColumnIndex()));
             if ($colDimension->getWidth() < 0) {
                 // No width set, apply default of 10
                 $objWriter->writeAttribute('width', '9.10');
             } else {
                 // Width set
                 $objWriter->writeAttribute('width', \PHPExcel\Shared\StringHelper::formatNumber($colDimension->getWidth()));
             }
             // Column visibility
             if ($colDimension->getVisible() == false) {
                 $objWriter->writeAttribute('hidden', 'true');
             }
             // Auto size?
             if ($colDimension->getAutoSize()) {
                 $objWriter->writeAttribute('bestFit', 'true');
             }
             // Custom width?
             if ($colDimension->getWidth() != $pSheet->getDefaultColumnDimension()->getWidth()) {
                 $objWriter->writeAttribute('customWidth', 'true');
             }
             // Collapsed
             if ($colDimension->getCollapsed() == true) {
                 $objWriter->writeAttribute('collapsed', 'true');
             }
             // Outline level
             if ($colDimension->getOutlineLevel() > 0) {
                 $objWriter->writeAttribute('outlineLevel', $colDimension->getOutlineLevel());
             }
             // Style
             $objWriter->writeAttribute('style', $colDimension->getXfIndex());
             $objWriter->endElement();
         }
         $objWriter->endElement();
     }
 }