PHP导出导入Excel

ini_set('memory_limit', '-1');
  set_time_limit(0);

  $callStartTime = microtime(true);

  PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
     $objExcel = new PHPExcel();

     if (isset($datas) && isset($startdate) && isset($enddate)){
   $title = time();

   $objProps = $objExcel->getProperties();
   $objProps->setCreator("cbsi ");
   $objProps->setLastModifiedBy("report for finance");
   $objProps->setTitle("report for finance");
   $objProps->setSubject("report for finance");
   $objProps->setDescription("report for finance");
   $objProps->setKeywords("report for finance");
   $objProps->setCategory("report for finance");
   $objExcel->setActiveSheetIndex(0);
   $objActSheet = $objExcel->getActiveSheet();
   //设置当前活动sheet的名称
   $objActSheet->setTitle('report for finance');
             /设置标题
   $already_income_amount = 0;
   $invoice_amount = 0;
   $The_invoices_amount = 0;
   foreach ($datas as $key => $val){
    $already_income_amount = $already_income_amount + $val['already_income_amount'];
    $invoice_amount = $invoice_amount + $val['invoice_amount'];
    $The_invoices_amount = $The_invoices_amount + $val['The_invoices_amount'];
   }

   foreach ($datas as $key => $val){
        $key = $key+3;

     //$objActSheet->setCellValue('A'.$key, $val['id']);
     $objActSheet->setCellValue('A'.$key, $key-2);
     $objActSheet->setCellValue('B'.$key, $val['company_name']);
     $objActSheet->setCellValue('C'.$key, $val['bu_name']);
     $objActSheet->setCellValue('D'.$key, $val['customer_name']);
     $objActSheet->setCellValue('E'.$key, $val['contract_number']);
     $objActSheet->setCellValue('F'.$key, $val['already_income_amount']);
     $objActSheet->setCellValue('G'.$key, $val['invoice_amount']);
     $objActSheet->setCellValue('H'.$key, $val['The_invoices_amount']);
     $objActSheet->setCellValue('I'.$key, $val['Differences_note']);
                    $objExcel->getActiveSheet()->setSharedStyle(Custom_ExcelStyle::line(), "A".$key.":I".$key);

   }

        $count = count($datas)+3;  //=27
        $objActSheet->setCellValue("A"."$count", '总计');
        $objActSheet->setCellValue("B"."$count", '-');
        $objActSheet->setCellValue("C"."$count", '-');
  $objActSheet->setCellValue("D"."$count", '-');
  $objActSheet->setCellValue("E"."$count", '-');
  $objActSheet->setCellValue("F"."$count", $already_income_amount);
  $objActSheet->setCellValue("G"."$count", $invoice_amount);
  $objActSheet->setCellValue("H"."$count", $The_invoices_amount);
  $objActSheet->setCellValue("I"."$count", '-');

        $objExcel->getActiveSheet()->setSharedStyle(Custom_ExcelStyle::total(), "A".$count.":I".$count);
        //表格第一行内容
        $objActSheet->setCellValue('A1', 'CCOM月度/季度/年度销售及发票开具情况统计表');
        $objActSheet->setCellValue('A2', '日期:'.$startdate."到".$enddate);
  //显式指定内容类型
  //$objActSheet->setCellValueExplicit('A5', '847475847857487584',PHPExcel_Cell_DataType::TYPE_STRING);
  //合并单元格
  $objActSheet->mergeCells('A1:I1');
  $objActSheet->mergeCells('A2:I2');
  //设置表格内容居中
     //$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  //分离单元格
  //$objActSheet->unmergeCells('B1:C22');
  //*************************************
  //设置宽度
  $objActSheet->getColumnDimension('A')->setWidth(10);
  $objActSheet->getColumnDimension('B')->setWidth(40);
  $objActSheet->getColumnDimension('C')->setWidth(30);
  $objActSheet->getColumnDimension('D')->setWidth(40);
  $objActSheet->getColumnDimension('E')->setWidth(20);
  $objActSheet->getColumnDimension('F')->setWidth(30);
  $objActSheet->getColumnDimension('G')->setWidth(30);
  $objActSheet->getColumnDimension('H')->setWidth(40);
  $objActSheet->getColumnDimension('I')->setWidth(30);

  $objExcel->createSheet();
        $objExcel->getActiveSheet()->setSharedStyle(Custom_ExcelStyle::head(), "A1:I3");

  $objWriter = new PHPExcel_Writer_Excel2007($objExcel);  
  $objWriter->setUseDiskCaching(true);
  $outputFileName = "download/excel/";
  $outputFileName .= Custom_Session::Get('id')."invoice_issue".".xlsx";
        $objWriter->save($outputFileName);
  //$objPHPExcel->disconnectWorksheets();
  unset($objPHPExcel);
  }
  $back =  array(1,$outputFileName);
        return $back;

 

你可能感兴趣的:(技术的成长)