// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('B1', 'test1111111111111111111111')
->setCellValue('C1', 'test1111111111111')
->setCellValue('D1', 'test1111111')
->setCellValue('E1', 'test11111')
->setCellValue('F1', 'test1')
->setCellValue('G1', 'test1');
foreach($objPHPExcel->getActiveSheet()->getColumnDimension() as $col) {
$col->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->calculateColumnWidths();
위의 코드는 doesn& # 39, 빗나갔다. # 39 doesn& 열 크기 변경하십시오 텍스트에 맞게 된다.
열 경우) 으로 설정되었습니다 페페스켈 계산됩니까 값을 계산하는 데 따라 열 너버 시도_횟수 AutoSize, 열 (드릴링됩니다 결과 모든 육팔면체의 절반이다) 등의 형식을 통해 추가된 문자 및 기타 못하니이는 마스킹하고 구분.
기본적으로 이 백업이었습니다 '추정' 너버: 보다 정확한 계산 방법을 사용할 수 있고, GD 처리할 수도 있는 스타일 기능 사용에 따른 글꼴색 굵은꼴 및 기울임꼴의. 그러나 이는 훨씬 큰 부담 때문에 기본적으로 꺼져 있습니다. 더 정확한 계산을 통해 사용할 수 있습니다
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
그러나 모든 작성기에 t # 39, autosize doesn& 적용하십시오 형식식. 예를 들어 CSV. # 39, t # 39 를 사용하여, re you& 어떤 작성기에 언급하십시오 don& 있습니다.
열 파악해야 하는 크기: 설정할 수 있을 뿐만 아니라
foreach(range('B','G') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}
> 제콜럼드리멘시온 () ',' $ objphpexcel-> 그레이스티베시스 (), id 열 수 있을 것으로 기대하고 있다.
() ',' $ objPHPExcel-> 그레이스티베시스 (), 제콜럼드리멘시언스 -> 반환되므로 배열입니다 정의한 모든 열 치수처리 기록. 하지만 그동안 창조하 기록하십시오 명시적으로 열 한 치수 (아마도 로드하여 템플리트와 제콜럼드리멘시온 () '또는' 에서 수동으로 호출하십시오) 그런 다음, t # 39 won& 타임코드가 (메모리 저장).
그렇게 할 경우 여러 번 반복할 수 있는 방법을 여기에 여러 열 시트 및 각 시트마다 모두야:
// Auto size columns for each worksheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$objPHPExcel->setActiveSheetIndex($objPHPExcel->getIndex($worksheet));
$sheet = $objPHPExcel->getActiveSheet();
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
/** @var PHPExcel_Cell $cell */
foreach ($cellIterator as $cell) {
$sheet->getColumnDimension($cell->getColumn())->setAutoSize(true);
}
}
이는 모든 열을 워크시트를 에서 사용하는 방법을 예:
$sheet = $PHPExcel->getActiveSheet();
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
$cellIterator->setIterateOnlyExistingCells( true );
/** @var PHPExcel_Cell $cell */
foreach( $cellIterator as $cell ) {
$sheet->getColumnDimension( $cell->getColumn() )->setAutoSize( true );
}
이 코드는 스니펫은 자동 크기를 포함하는 모든 열이 data-in 모든 시트요 Setter 와 getter 스티브 주트 사용할 필요가 없습니다.
// In my case this line didn't make much of a difference
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
// Iterating all the sheets
/** @var PHPExcel_Worksheet $sheet */
foreach ($objPHPExcel->getAllSheets() as $sheet) {
// Iterating through all the columns
// The after Z column problem is solved by using numeric columns; thanks to the columnIndexFromString method
for ($col = 0; $col <= PHPExcel_Cell::columnIndexFromString($sheet->getHighestDataColumn()); $col++) {
$sheet->getColumnDimensionByColumn($col)->setAutoSize(true);
}
}
피프스프레더시스 대한.
$sheet = $spreadsheet->getActiveSheet(); // $spreadsheet is instance of PhpOffice\PhpSpreadsheet\Spreadsheet
foreach (
range(
1,
Coordinate::columnIndexFromString($sheet->getHighestColumn(1))
) as $column
) {
$sheet
->getColumnDimension(Coordinate::stringFromColumnIndex($column))
->setAutoSize(true);
}
이 경우 누가 찾고 있었습니다.
결의안은 또 그들의 새로운 버전의 페페스켈 피프스프레더시스 ',' 아래 작동합니다.
// assuming $spreadsheet is instance of PhpOffice\PhpSpreadsheet\Spreadsheet
// assuming $worksheet = $spreadsheet->getActiveSheet();
foreach(range('A',$worksheet->getHighestColumn()) as $column) {
$spreadsheet->getColumnDimension($column)->setAutoSize(true);
}
>. 참고: 제하이스트콜럼너 제하이스트다타콜럼너 () '또는' () '' 마지막 실제 열 대체할 수 있다.
이러한 방법을 어떻게 해야 할 것:
($ 행일 = null) '-' 제하이스트콜럼너 프레젠테이션이든 최고 워크시트를 얻어맞았다.
($ 행일 = null) '-' 열 수 있는 최고의 워크시트를 제하이스트다타콜럼너 데이터가 있습니다.
($ 열 = null) '-' 제하이스트로 최고 워크시트를 행일 확보하십시오.
'-' 제하이스트다타로프 ($ 열 = null) 데이터를 포함하는 최고의 워크시트를 확보하십시오 행일.
반복할 수 있는 '을 할 경우 ($ col = 2. # 39, & # 39 AC& = $ col <;; ++ $ col) '또는' 포리치.} 와 {(범위 (& # 39 A& # 39, & # 39 AC& # 39;;;;) 달러 정도로 col) {} it from A to Z, 하지만 실패한 것입니다. '열에 대한 pass the Z (Ex. 사이에 A& # 39, & # 39 는. # 39, & # 39 를 AC&;).
$MAX_COL = $sheet->getHighestDataColumn();
$MAX_COL_INDEX = PHPExcel_Cell::columnIndexFromString($MAX_COL);
for($index=0 ; $index <= $MAX_COL_INDEX ; $index++){
$col = PHPExcel_Cell::stringFromColumnIndex($index);
// do something, like set the column width...
$sheet->getColumnDimension($col)->setAutoSize(TRUE);
}
또한 열 파악해야 하는 크기: 설정할 수 있습니다.
foreach (range('A', $phpExcelObject->getActiveSheet()->getHighestDataColumn()) as $col) {
$phpExcelObject
->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
열 수 있다는 것을 알고 있지만 방법을 모르는 아타드저스트 반복자가 지난 apiu 버전, 기본적으로 열 객체에는 I& # 39 에서 만든 검색하기를 루프를 자체 it, ve 처음 사용된 열을 진짜 진짜 마지막으로 사용한 하나.
자 발표한다.
//Just before saving de Excel document, you do this:
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
//We get the util used space on worksheet. Change getActiveSheet to setActiveSheetIndex(0) to choose the sheet you want to autosize. Iterate thorugh'em if needed.
//We remove all digits from this string, which cames in a form of "A1:G24".
//Exploding via ":" to get a 2 position array being 0 fisrt used column and 1, the last used column.
$cols = explode(":", trim(preg_replace('/\d+/u', '', $objPHPExcel->getActiveSheet()->calculateWorksheetDimension())));
$col = $cols[0]; //first util column with data
$end = ++$cols[1]; //last util column with data +1, to use it inside the WHILE loop. Else, is not going to use last util range column.
while($col != $end){
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
$col++;
}
//Saving.
$objWriter->save('php://output');
$col = 'A';
while(true){
$tempCol = $col++;
$objPHPExcel->getActiveSheet()->getColumnDimension($tempCol)->setAutoSize(true);
if($tempCol == $objPHPExcel->getActiveSheet()->getHighestDataColumn()){
break;
}
}