如何每30条记录放置换行符

时间:2015-10-12 09:13:54

标签: php mysql excel phpexcel

我有一个程序通过PHPExcel将MySQL数据写入Excel并且我没有错误,但问题是他们没有每30条记录中有任何换行符。

如何在PHPExcel中以编程方式实现此目的?

这是我的代码:

require 'PHPExcel.php';
require 'PHPExcel/Writer/Excel2007.php';
require 'PHPExcel/IOFactory.php';

$user = "root";
$pass = "admin";
$host = "localhost";
$db = "hrtms";
$dept = "WAREHOUSE/DRIVER";
$TimeIn = "";
$LunchOut = "";
$LunchIn = "";
$TimeOut = "";
$objPHPExcel = new PHPExcel();
try{
$con = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$con->exec('SET NAMES "utf8"');
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}

try{
$query = $con->prepare("SELECT * FROM emptb WHERE Department = :dept ORDER BY id ASC");
$query->bindParam(':dept',$dept);
$query->execute();
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}
$objPHPExcel->setActiveSheetIndex(0);
//set default font
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Verdana');
//set titles
$objPHPExcel->getActiveSheet()->SetCellValue('B1','EMPLOYEE ATTENDANCE LOGS');
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setWrapText(true);
//merge cells
$objPHPExcel->getActiveSheet()->mergeCells('B1:T1');
$objPHPExcel->getActiveSheet()->mergeCells('C4:D4');
$objPHPExcel->getActiveSheet()->mergeCells('E8:F8');

//set text alignment
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(18);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('#333');
//set column width
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6.14);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(0.67);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10.86);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(1.43);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(9.71);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(1.43);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(6.71);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(1.57);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(2);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(9);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(0.58);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(0.92);
$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(9);
$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(1.71);
$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(2.43);
$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(6.14);
$objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(1.71);
$objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(9.29);
$objPHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(4);
$objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(0.67);
$objPHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(0.67);
//set text alignment
$rowCount = 4;
while($row = $query->fetch())
{
    $rowTitle = $rowCount + 2;
    $rowTitle1 = $rowCount + 4;
    //data label
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->mergeCells('C'.($rowCount).':D'.($rowCount));
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getFont()->setName('Arial');
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'ID No:');

    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->mergeCells('C'.($rowTitle).':D'.($rowTitle));
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle)->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle)->getFont()->setName('Arial');
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowTitle,'Name:');

    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->mergeCells('I'.($rowCount).':J'.($rowCount));
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowCount)->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowCount)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowCount)->getFont()->setName('Arial');
    $objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowCount,'Dept:');

    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->mergeCells('C'.($rowTitle1).':D'.($rowTitle1));
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle1)->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle1)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle1)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$rowTitle1)->getFont()->setName('Arial');
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowTitle1,'Section:');

    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowTitle1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->mergeCells('I'.($rowTitle1).':J'.($rowTitle1));
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowTitle1)->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowTitle1)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowTitle1)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('I'.$rowTitle1)->getFont()->setName('Arial');
    $objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowTitle1,'Line:');
    //data contents
    $objPHPExcel->getActiveSheet()->mergeCells('E'.($rowCount).':G'.($rowCount));
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,$row['EmpID']);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getFont()->setName('Arial');

    $objPHPExcel->getActiveSheet()->mergeCells('E'.($rowTitle).':S'.($rowTitle));
    $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowTitle,$row['Lastname'] . ', ' . $row['Firstname']);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowTitle)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowTitle)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowTitle)->getFont()->setName('Arial');

    $objPHPExcel->getActiveSheet()->mergeCells('L'.($rowCount).':S'.($rowCount));
    $objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowCount,$row['Department']);
    $objPHPExcel->getActiveSheet()->getStyle('L'.$rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('L'.$rowCount)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('L'.$rowCount)->getFont()->setName('Arial');

    $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowTitle1,$row['SectionName']);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowTitle1)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowTitle1)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('E'.$rowTitle1)->getFont()->setName('Arial');

    $objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowTitle1,$row['LineName']);
    $objPHPExcel->getActiveSheet()->getStyle('L'.$rowTitle1)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('L'.$rowTitle1)->getFont()->setSize(12);
    $objPHPExcel->getActiveSheet()->getStyle('L'.$rowTitle1)->getFont()->setName('Arial');
    $rowCount++;

    try{
        $subquery = $con->prepare("SELECT c.dt,a.TimeIn,a.LunchOut,a.LunchIn,a.RNDOUT FROM cal c LEFT JOIN attendance a ON a.ValidDate = c.dt
        AND a.EmpID = :id WHERE c.dt BETWEEN DATE('2015-08-01') AND DATE('2015-08-30') GROUP BY c.dt ORDER BY c.dt ASC");
        $subquery->bindParam(':id',$row['EmpID']);
        $subquery->execute();
    }
    catch(PDOException $e){
        echo $e->getMessage();
        exit();
    }
    $rowCount += 6;
    while($subrow = $subquery->fetch())
    {

        if($subrow['TimeIn'] == "00:00:00")
        {
            $TimeIn = "";
        }
        else
        {
            $TimeIn = $subrow['TimeIn'];
        }

        if($subrow['LunchOut']=="00:00:00")
        {
            $LunchOut = "";
        }
        else
        {
            $LunchOut = $subrow['LunchOut'];
        }

        if($subrow['LunchIn']=="00:00:00")
        {
            $LunchIn = "";
        }
        else
        {
            $LunchIn = $subrow['LunchIn'];
        }

        if($subrow['RNDOUT']=="00:00:00")
        {
            $TimeOut = "";
        }
        else
        {
            $TimeOut = $subrow['RNDOUT'];
        }
        $titleRow = 10;
        $objPHPExcel->getActiveSheet()->getStyle('C'.$titleRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('C'.$titleRow)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('C'.$titleRow)->getFont()->setSize(11);
        $objPHPExcel->getActiveSheet()->getStyle('C'.$titleRow)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('C'.$titleRow,'Date');
        $objPHPExcel->getActiveSheet()->getStyle('E'.$titleRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E'.$titleRow)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('E'.$titleRow)->getFont()->setSize(11);
        $objPHPExcel->getActiveSheet()->getStyle('E'.$titleRow)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('E'.$titleRow,'TimeIn');
        $objPHPExcel->getActiveSheet()->getStyle('G'.$titleRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$titleRow)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$titleRow)->getFont()->setSize(11);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$titleRow)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('G'.$titleRow,'LunchOut');
        $objPHPExcel->getActiveSheet()->getStyle('J'.$titleRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$titleRow)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$titleRow)->getFont()->setSize(11);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$titleRow)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('J'.$titleRow,'LunchIn');
        $objPHPExcel->getActiveSheet()->getStyle('M'.$titleRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('M'.$titleRow)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('M'.$titleRow)->getFont()->setSize(11);
        $objPHPExcel->getActiveSheet()->getStyle('M'.$titleRow)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('M'.$titleRow,'TimeOut');
        $titleRow++;

        $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('C'.$rowCount)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,$subrow['dt']);
        $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('E'.$rowCount)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,$TimeIn);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$rowCount)->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$rowCount)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('G'.$rowCount,$LunchOut);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$rowCount)->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$rowCount)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('J'.$rowCount,$LunchIn);
        $objPHPExcel->getActiveSheet()->getStyle('M'.$rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('M'.$rowCount)->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('M'.$rowCount)->getFont()->setName('Arial');
        $objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount,$TimeOut);
        $rowCount++;
    }
}
$excelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
header('Content-Type:application/ms-excel');
header('Content-Disposition:attachment;filename="'.$dept.'.xlsx"');
header('Cache-Control:max-age=0');
$excelWriter->save('php://output');

2 个答案:

答案 0 :(得分:3)

如果您指的是电子表格的每30行,请按照in the documentation所述使用打印设置选项:

$objPHPExcel->getActiveSheet()
    ->setBreak('A10' , PHPExcel_Worksheet::BREAK_ROW);

将在第10行设置打印中断

因此,只需在构建工作表

时每隔30行设置这些断点即可

答案 1 :(得分:0)

如果您想准确控制换行的位置,则只需使用 \ n

示例:

$objPHPExcel->getActiveSheet()->setCellValue('H5', "Hello\nWorld");

在PHP字符串中添加转义序列时,应始终使用双引号。