yii 导入导出excle

来源:互联网 发布:2016国内旅游数据 编辑:程序博客网 时间:2024/05/22 00:09
/*
导入部门
*/


public function actionreadexcelfile()
{


try {

spl_autoload_unregister(array('YiiBase','autoload'));
header("content-type:text/html; charset=utf-8");
// 获取PHPExcel引用路径
$phpExcelPath = Yii::getPathOfAlias('application.extensions.phpexcel');
$phpExcelPathFile = Yii::getPathOfAlias('application.extensions');
// 关闭YII的自动加载功能,改用手动加载,否则会出错,PHPExcel有自己的自动加载功能
// YII框架对于组件的自动加载,要求类名与文件名一致;
// 而PHPExcel类对应的文件名包含了上级目录名称,如:IOFactory类对应的文件名为PHPExcel_IOFactory.php

include($phpExcelPathFile . DIRECTORY_SEPARATOR . 'PHPExcel.php');
$excel = new PHPExcel();
$path="./a.xls";
$info = pathinfo($path);

$ext = $info['extension'];
$excelReader = null;

if($ext=='xls')
$excelReader = PHPExcel_IOFactory::createReader('Excel5');
else
$excelReader = PHPExcel_IOFactory::createReader('Excel2007');

$phpExcelInstance = $excelReader->load($path);

$sheet = $phpExcelInstance->getSheet(0);

spl_autoload_register(array('YiiBase','autoload'));

// 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数

$highestColumm = $sheet->getHighestColumn(); // 取得总列数
$highestColumm= PHPExcel_Cell::columnIndexFromString($highestColumm); //字母列转换为数字列 如:AA变为27
$insertSql='';
$rowIndex = 2;
/** 循环读取每个单元格的数据 */

$arr = array();
for ($row = 2; $row <= $highestRow; $row++)
{//行数是以第2行开始
$obj = new stdClass();
$obj->yimi_id =trim($sheet->getCellByColumnAndRow(0, $row)->getValue());
$obj->yimi_title=trim($sheet->getCellByColumnAndRow(1, $row)->getValue());
$obj->yimi_code=trim($sheet->getCellByColumnAndRow(2, $row)->getValue());
$obj->yimi_flow_name=trim($sheet->getCellByColumnAndRow(3, $row)->getValue());
$obj->yimi_contents=trim($sheet->getCellByColumnAndRow(4, $row)->getValue());
$obj->yimi_send_name=trim($sheet->getCellByColumnAndRow(5, $row)->getValue());
$obj->yimi_begin_time=trim($sheet->getCellByColumnAndRow(6, $row)->getValue());
$obj->yimi_end_time=trim($sheet->getCellByColumnAndRow(7, $row)->getValue());

array_push($arr, $obj);
}

Tool::dump($arr);
//exit;
//$count = $this->InsertTODB($arr);
//return $count;
}
catch(Exception $e)
{
print_r($e);
exit();
return 0;
}
}


public function actionExportMileStone($uploaddir="./",$hasExp=false)
{


$phpExcelPath = Yii::getPathOfAlias('application.extensions.phpexcel');
$phpExcelPathFile = Yii::getPathOfAlias('application.extensions');
// 关闭YII的自动加载功能,改用手动加载,否则会出错,PHPExcel有自己的自动加载功能
// YII框架对于组件的自动加载,要求类名与文件名一致;
// 而PHPExcel类对应的文件名包含了上级目录名称,如:IOFactory类对应的文件名为PHPExcel_IOFactory.php
spl_autoload_unregister(array('YiiBase','autoload'));
include_once($phpExcelPathFile . DIRECTORY_SEPARATOR . 'PHPExcel.php');
spl_autoload_register(array('YiiBase','autoload'));
set_time_limit(6000);

$excelStone = new PHPExcel();

for($i=0;$i<1;$i++)
{
$fileName =  "合同里程碑信息_".$i.".xlsx";
//$arr = $designTable->GetMileStone($ids,$i,$hasExp);

$excelStone->setActiveSheetIndex(0);
$sheet = $excelStone->getActiveSheet();
spl_autoload_register(array('YiiBase','autoload'));
$sheet->SetCellValue('A1', '合同编号');//0
$sheet->SetCellValue('B1', '节点编号');//1
$sheet->SetCellValue('C1', '节点名称');//2
$sheet->SetCellValue('D1', '节点时间');//3
$sheet->SetCellValue('E1', '约定金额');//5
$sheet->SetCellValue('F1', '计划事由');//4

$sheet->SetCellValue('G1', '支付方式');//6
$sheet->SetCellValue('H1', '实际执行时间');//7
$sheet->SetCellValue('I1', '实际发生金额');//8
$sheet->SetCellValue('J1', '系统编号');//9
// Rename sheet
$sheet->setTitle('$Sheet1');
// Save Excel 2007 file
$arr=array('a','b','c');
$rowIndex=2;
foreach ($arr as $val)
{
$sheet->SetCellValue(PHPExcel_Cell::stringFromColumnIndex(2).$rowIndex,  $val);
$sheet->SetCellValue(PHPExcel_Cell::stringFromColumnIndex(3).$rowIndex, $val);



$rowIndex++;
}
//创建一个新的工作空间(sheet)
$excelStone->createSheet();
$sheet =$excelStone->setactivesheetindex(1);
$sheet->SetCellValue('A1', '合同编号');//0
$sheet->SetCellValue('B1', '节点编号');//1
$sheet->SetCellValue('C1', '节点名称');//2
$sheet->SetCellValue('D1', '节点时间');//3
$sheet->SetCellValue('E1', '约定金额');//5
$sheet->SetCellValue('F1', '计划事由');//4

$sheet->SetCellValue('G1', '支付方式');//6
$sheet->SetCellValue('H1', '实际执行时间');//7
$sheet->SetCellValue('I1', '实际发生金额');//8
$sheet->SetCellValue('J1', '系统编号');//9
// Rename sheet
$sheet->setTitle('$Sheet1');
// Save Excel 2007 file
$arr=array('a','b','c');
$rowIndex=2;
foreach ($arr as $val)
{
$sheet->SetCellValue(PHPExcel_Cell::stringFromColumnIndex(2).$rowIndex,  $val);
$sheet->SetCellValue(PHPExcel_Cell::stringFromColumnIndex(3).$rowIndex, $val);



$rowIndex++;
}
$objPHPExcel = PHPExcel_IOFactory::createWriter($excelStone,'Excel2007');
$xmlFile = $uploaddir.$fileName;

  $objPHPExcel->save($xmlFile);

}

}
原创粉丝点击