Heim > Artikel > Backend-Entwicklung > PHPExcel 向单元格插入图片就换行的有关问题
PHPExcel 向单元格插入图片就换行的问题
要用phpexcel从数据库中将表导出为excel格式,表中存放有图片路径现在我的代码如下:
<?php <br /><br>require_once 'PHPExcel.php'; <br>require_once 'PHPExcel/Writer/Excel5.php'; <br>include_once 'DB_connmssql.php';<br><br>$sql="select.....";<br><br><br><br>$query = mssql_query($sql);<br>$items = array();<br><br>// 创建一个处理对象实例 <br>$objExcel = new PHPExcel(); <br> <br>// 创建文件格式写入对象实例, uncomment <br>$objWriter = new PHPExcel_Writer_Excel5($objExcel); <br> <br>//设置文档基本属性 <br>$objProps = $objExcel->getProperties(); <br>$objProps->setCreator("tt"); <br>$objProps->setLastModifiedBy("tt"); <br>$objProps->setTitle("tt"); <br>$objProps->setSubject("tt "); <br>$objProps->setDescription("tt "); <br>$objProps->setKeywords("tt "); <br>$objProps->setCategory("变动报表"); <br> <br>//************************************* <br>//设置当前的sheet索引,用于后续的内容操作。 <br>//一般只有在使用多个sheet的时候才需要显示调用。 <br>//缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0 <br>$objExcel->setActiveSheetIndex(0); <br>$objActSheet = $objExcel->getActiveSheet(); <br> <br>//设置当前活动sheet的名称 <br>$objActSheet->setTitle('tttt'); <br> <br>//************************************* <br>// <br>//设置宽度,这个值和EXCEL里的不同,不知道是什么单位,略小于EXCEL中的宽度<br>$objActSheet->getColumnDimension('A')->setWidth(20); <br>$objActSheet->getColumnDimension('B')->setWidth(15); <br><br><br>$objActSheet->getRowDimension(1)->setRowHeight(30); <br>$objActSheet->getRowDimension(2)->setRowHeight(27); <br>$objActSheet->getRowDimension(3)->setRowHeight(16); <br> <br>//设置单元格的值 <br>$objActSheet->setCellValue('A1', 'Titile'); <br>//合并单元格<br>$objActSheet->mergeCells('A1:N1'); <br>//设置样式<br>$objStyleA1 = $objActSheet->getStyle('A1'); <br>$objStyleA1->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>$objFontA1 = $objStyleA1->getFont(); <br>$objFontA1->setName('宋体'); <br>$objFontA1->setSize(18); <br>$objFontA1->setBold(true); <br><br>//设置居中对齐<br>$objActSheet->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>$objActSheet->getStyle('B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br><br>$objActSheet->setCellValue('A2', 'Shoes Picture'); <br>$objActSheet->setCellValue('B2', 'Product code'); <br>//设置边框<br>$objActSheet->getStyle('A2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('A2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('A2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('A2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> <br><br>$i=1;<br><br>$query = mssql_query($sql);<br>$query1 = mssql_query($sql);<br>$items=array();<br>$sales_sum=$sales_sum+$row1["total_sales"];<br>while($row=mssql_fetch_array($query)){<br> $row["Photo"]="../picture/".$row["Photo"];<br> $n=$i+2;<br> $objActSheet->getStyle('B'.$n)->getNumberFormat()->setFormatCode('@');<br> $objActSheet->getStyle('E'.$n)->getNumberFormat()->setFormatCode('@');<br> $objActSheet->getRowDimension($n)->setRowHeight(16);<br> $objActSheet->getStyle('A'.$n)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('A'.$n)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('A'.$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('A'.$n)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B'.$n)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B'.$n)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B'.$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<br> $objActSheet->getStyle('B'.$n)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );<div class="clear"> </div>