php PHPExcel 使第一行加粗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14746332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel Make first row bold
提问by Rakesh
I am trying to make cells in first row are bold.
我试图使第一行的单元格为粗体。
This is the method I have created for that purpose.
这是我为此目的创建的方法。
function ExportToExcel($tittles,$excel_name)
{
$objPHPExcel = new PHPExcel();
$objRichText = new PHPExcel_RichText();
// Set properties
$objPHPExcel->getProperties()->setCreator("SAMPLE1");
$objPHPExcel->getProperties()->setLastModifiedBy("SAMPLE1");
$objPHPExcel->getProperties()->setTitle("SAMPLE1");
$objPHPExcel->getProperties()->setSubject("SAMPLE1");
$objPHPExcel->getProperties()->setDescription("SAMPLE1");
// Add some data
$objPHPExcel->setActiveSheetIndex(0);
$letters = range('A','Z');
$count =0;
$cell_name="";
foreach($tittles as $tittle)
{
$cell_name = $letters[$count]."1";
$count++;
$value = $tittle;
$objPHPExcel->getActiveSheet()->SetCellValue($cell_name, $value);
// Make bold cells
$objPHPExcel->getActiveSheet()->getStyle($cell_name)->getFont()->setBold(true);
}
// Save Excel 2007 file
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
//$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$objWriter->save($excel_name.".xlsx");
}
The problem is in output excel file the cells are not bold.
问题是在输出 excel 文件中,单元格不是粗体。
回答by Ali Mezal
Try this for range of cells:
试试这个单元格范围:
$from = "A1"; // or any value
$to = "B5"; // or any value
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold( true );
or single cell
或单细胞
$cell_name = "A1";
$objPHPExcel->getActiveSheet()->getStyle( $cell_name )->getFont()->setBold( true );
hope that helps
希望有帮助
回答by Sadikhasan
Try this
尝试这个
$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);
回答by ednincer
$objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);
That way you get the complete first row
这样你就得到完整的第一行
回答by boojer
Assuming headers are on the first row of the sheet starting at A1, and you know how many of them there are, this was my solution:
假设标题位于从 A1 开始的工作表的第一行,并且您知道其中有多少个,这就是我的解决方案:
$header = array(
'Header 1',
'Header 2'
);
$objPHPExcel = new PHPExcel();
$objPHPExcelSheet = $objPHPExcel->getSheet(0);
$objPHPExcelSheet->fromArray($header, NULL);
$first_letter = PHPExcel_Cell::stringFromColumnIndex(0);
$last_letter = PHPExcel_Cell::stringFromColumnIndex(count($header)-1);
$header_range = "{$first_letter}1:{$last_letter}1";
$objPHPExcelSheet->getStyle($header_range)->getFont()->setBold(true);
回答by Tom
Use this:
用这个:
$sheet->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);
回答by Vivek
These are some tips to make your cells Bold, Big font, Italic
这些都是一些提示,使您的细胞Bold,Big font,Italic
Let's say I have columns from Ato L
假设我有从A到的列L
A1- is your starting cell
A1- 是你的起始单元格
L1- is your last cell
L1- 是你的最后一个细胞
$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setSize(16);
$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setItalic(true);
回答by Arie
$objPHPExcel->getActiveSheet()->getStyle("A1:".$objPHPExcel->getActiveSheet()->getHighestDataColumn()."1")->getFont()->setBold(true);
I found this to be a working solution, you can replace the two instances of 1with the row number. The HighestDataColumnfunction returns for example C or Z, it gives you the last/highest column that's in the sheet containing any data. There is also getHighestColumn(), that one would include cells that are empty but have styling or are part of other functionality.
我发现这是一个可行的解决方案,您可以用1行号替换两个实例。该HighestDataColumn函数返回例如 C 或 Z,它为您提供包含任何数据的工作表中的最后/最高列。还有getHighestColumn(),那将包括空但具有样式或属于其他功能的一部分的单元格。
回答by datasn.io
This iterates through a variable number of columnsof a particular row, which in this case is the 1st row:
这将遍历特定行的可变列数,在本例中是第一行:
$rownumber = 1;
$row = $this->objPHPExcel->getActiveSheet()->getRowIterator($rownumber)->current();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
$cell->getStyle()->getFont()->setBold(true);
}
回答by suman chauhan
Try this
尝试这个
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'No');
$sheet->setCellValue('B1', 'Job ID');
$sheet->setCellValue('C1', 'Job completed Date');
$sheet->setCellValue('D1', 'Job Archived Date');
$styleArray = array(
'font' => array(
'bold' => true
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
$sheet->getStyle('B1')->applyFromArray($styleArray);
$sheet->getStyle('C1')->applyFromArray($styleArray);
$sheet->getStyle('D1')->applyFromArray($styleArray);
$sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);
This is give me output like below link.(https://www.screencast.com/t/ZkKFHbDq1le)
这是给我如下链接的输出。(https://www.screencast.com/t/ZkKFHbDq1le)
回答by user3808975
You can try
你可以试试
$objPHPExcel->getActiveSheet()->getStyle(1)->getFont()->setBold(true);

