如何阅读大量的csv文件并将它们写入xls的一部分。用phpexcel文件

时间:2016-09-05 07:52:50

标签: php excel csv

我有一个5 csv文件,我将转换为excel文件。 我需要这个,因为我必须将它们保存为我的格式,如图像和标题。 现在,我正在使用phpexcel。

这是我的代码

public function parse_csv_to_excel() {
    $gaug = FCPATH . "./assets/uploads/EGT_GAUG.CSV"; /*Will become a sheet named : gauge*/
    $in = FCPATH . "./assets/uploads/EGT_IN.CSV"; /*Will become a sheet named : in_record*/
    $out = FCPATH . "./assets/uploads/EGT_OUT.CSV"; /*Will become a sheet named : out_record*/
    $stok = FCPATH . "./assets/uploads/EGT_STOK.CSV"; /*Will become a sheet named : stock_record*/
    $strg = FCPATH . "./assets/uploads/EGT_STRG.CSV"; /*Will become a sheet named : storage_record*/


    if (is_file($gaug) && is_file($in) && is_file($out) && is_file($stok) && is_file($strg)):
        $this->load->library('excel');

        $style_merge_cell = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            )
        );


        $this->excel->setActiveSheetIndex(0);
        $this->excel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10); //default font

        $inputFileType = 'CSV';
        $inputFileNames = array($in, $stok, $gaug, $strg, $out);

        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $inputFileName = array_shift($inputFileNames);
        $objPHPExcel = $objReader->load($inputFileName);
        $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName, PATHINFO_BASENAME));
        foreach ($inputFileNames as $sheet => $inputFileName) {
            $objReader->setSheetIndex($sheet + 1);
            $objReader->loadIntoExisting($inputFileName, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName, PATHINFO_BASENAME));
        }


        $loadedSheetNames = $objPHPExcel->getSheetNames();
        foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
            $objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
            $this->excel->getActiveSheet()->setTitle('Movement In');
            $this->excel->createSheet();

            $this->excel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
            $this->excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);

            $this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(1.29);

            $logo = new PHPExcel_Worksheet_Drawing();
            $logo->setPath('./assets/img/logo_tras_excel.png');
            $logo->setWidth(325);
            $logo->setHeight(59);

            $logo->setCoordinates('B2');
            $logo->setWorksheet($this->excel->getActiveSheet());
            $this->excel->getActiveSheet()->setCellValue('B6', 'To');
            $this->excel->getActiveSheet()->setCellValue('B7', 'ATTN');
            $this->excel->getActiveSheet()->setCellValue('B8', 'Customer');
            $this->excel->getActiveSheet()->setCellValue('B9', 'From');
            $this->excel->getActiveSheet()->setCellValue('B10', 'Activity Date');


            $this->excel->getActiveSheet()->mergeCells('B12:Q12');
            $this->excel->getActiveSheet()->setCellValue('B12', 'EAGLETAINER DAILY MOVEMENT REPORT ');
            $this->excel->getActiveSheet()->getStyle('B12')->applyFromArray($style_merge_cell);

            $this->excel->getActiveSheet()->setCellValue('B14', 'Subject');
            $this->excel->getActiveSheet()->setCellValue('D14', ': MOVE IN');
            $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
            $this->excel->getActiveSheet()->fromArray($sheetData, NULL, 'B16');
        }
$filename = 'Daily report.xls'; //save our workbook as this file name

        header('Content-Type: application/vnd.ms-excel'); //mime type
        header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
        header('Cache-Control: max-age=0'); //no cache
        //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
        //if you want to save it as .XLSX Excel 2007 format
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');

        //force user to download the Excel file without writing it to server's HD
        $objWriter->save('php://output');
    else :
        echo " ONE OF FILE IS NOT FOUND";
    endif;
}

请告知我的情况:

  1. 如何创建一张名为“我需要的顶级代码
  2. ”的工作表
  3. 我有5个文件,但创建的工作表是6个。
  4. 现在,csv只是在第一张表中编写,其中所有元素来自基于5个csv文件的数组,其他则不是。
  5. 谢谢, 请告知

0 个答案:

没有答案