gpt4 book ai didi

PHPExcel 单元格相互重叠 >setRowHeight(-1) 自动单元格高度不起作用

转载 作者:可可西里 更新时间:2023-10-31 22:14:21 25 4
gpt4 key购买 nike


更新

Tested using PHPSpreadsheet I have this code below that I have tried. It seems to work on MSOffice Excel if I use xls when I write the file. Note not working with Libre Office does not auto resize row need it to work with libre office as well.

$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) {
$rowID->setRowHeight(-1);
}

新 Controller

<?php

require(APPPATH . 'vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Events extends MX_Controller {

public function test() {
$spreadsheet = new Spreadsheet();

$spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
$spreadsheet->getDefaultStyle()->getFont()->setSize(24);

foreach(range('A','B') as $columnID) {
$spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}

$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) {
$rowID->setRowHeight(-1);
}

$spreadsheet->setActiveSheetIndex(0)
->setCellValue("A1",'Firstname')
->setCellValue("B1",'Lastname');

$spreadsheet->getActiveSheet()->setTitle('Users Information');

$spreadsheet->setActiveSheetIndex(0);

/* Here there will be some code where you create $spreadsheet */

// redirect output to client browser
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="myfile.xls"');
header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');

exit;

}
}

原始问题

我是 phpexcel 的新手,当我下载我的文件时,单元格相互重叠。

正如您在图像中看到的那样,当字体大时,它们全部聚在一起。

enter image description here

Question How can I make sure the cells are not over lapping each other. I tried file in libreoffice & msoffice excel and same issue.

我试过了还是没有变化

foreach(range('A','D') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}

foreach (range('A', $objPHPExcel->getActiveSheet()->getHighestDataColumn()) as $col) {
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
}

foreach(range(1, 4) as $rowID) {
$objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight(-1);
}

Controller

<?php

class Events extends MX_Controller {

public function generate_excel() {

$query = $this->db->get('event');
$excelresults = $query->result_array();

require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("");
$objPHPExcel->getProperties()->setLastModifiedBy("");
$objPHPExcel->getProperties()->setSubject("");
$objPHPExcel->getProperties()->setCreator("");
$objPHPExcel->getProperties()->setDescription("");

$objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getActiveSheet()->SetCellValue("A1", 'Event');
$objPHPExcel->getActiveSheet()->SetCellValue("B1", 'Event Title');
$objPHPExcel->getActiveSheet()->SetCellValue("C1", 'Event Date');
$objPHPExcel->getActiveSheet()->SetCellValue("D1", 'Event Start Time');

$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

foreach(range('A','D') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}

$headerstyle = array(
'font' => array(
'size' => 25,
'name' => 'Candara'
)
);

$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($headerstyle);


foreach (range('A', $objPHPExcel->getActiveSheet()->getHighestDataColumn()) as $col) {
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
}

$excelrow = 2;

foreach ($excelresults as $excelresult => $excelvalue) {

$columnstyle = array(
'font' => array(
'size' =>25,
'name' => 'Candara'
)
);

$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($columnstyle);


$objPHPExcel->getActiveSheet()->SetCellValue("A" . $excelrow, $excelvalue['event']);
$objPHPExcel->getActiveSheet()->SetCellValue("B" . $excelrow, $excelvalue['event_title']);
$objPHPExcel->getActiveSheet()->SetCellValue("C" . $excelrow, $excelvalue['event_date']);
$objPHPExcel->getActiveSheet()->SetCellValue("D" . $excelrow, $excelvalue['event_start_time']);

$excelrow++;
}

///exit();

$filename = 'Bowling-Events-For-' . date('Y') . '.xlsx';

$objPHPExcel->getProperties()->setTitle("Riwaka Bowling Club Events");

header("Content-Type: application/vnd.ms-excel; charset=utf-8"); # Important
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

header("Content-Disposition: attachment; filename=".$filename."");
header("Content-Transfer-Encoding: binary");

header("Pragma: no-cache");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

exit();
}
}

最佳答案

请检查下面的代码,对我有用:

$excel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

要将所有行的高度更改为自动,您可以这样做:

foreach($xls->getActiveSheet()->getRowDimensions() as $rowID) { 
$rowID->setRowHeight(-1);
}

关于PHPExcel 单元格相互重叠 >setRowHeight(-1) 自动单元格高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com