1 使用fgetcsv函数
array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape = '\\' ]]]] )
- <?php
-
$row = 1;
-
if (($handle = fopen("test.csv", "r")) !== FALSE) {
-
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
-
$num = count($data);
-
echo "
$num fields in line $row:
\n";
-
$row++;
-
for ($c=0; $c < $num; $c++) {
-
echo $data[$c] . "
\n";
-
}
-
}
-
fclose($handle);
-
}
-
?>
2 phpExcelReader
3 PHPExcel
3.1 使用PHPExcel_IOFactory读取
- <?php
-
require_once 'PHPExcel/IOFactory.php';
-
$objPHPExcel = PHPExcel_IOFactory::load("test.xlsx");
-
//$sheet = $objPHPExcel->getSheet(0);
-
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
-
$worksheetTitle = $worksheet->getTitle();
-
$highestRow = $worksheet->getHighestRow(); // e.g. 10
-
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
-
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
-
$nrColumns = ord($highestColumn) - 64;
-
echo "
The worksheet ".$worksheetTitle." has ";
-
echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
-
echo ' and ' . $highestRow . ' row.';
-
echo '
Data: ';
-
for ($row = 1; $row <= $highestRow; ++ $row) {
-
echo '
';
-
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
-
$cell = $worksheet->getCellByColumnAndRow($col, $row);
-
$val = $cell->getValue();
-
$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
-
echo '
' . $val . ' (Typ ' . $dataType . ') | ';
-
}
-
echo '
';
-
}
-
echo '
';
-
}
3.2使用PHPExcel_Reader_Excel2007读取
- <?php
-
include 'PHPExcel.php';
-
include 'PHPExcel/Reader/Excel2007.php';
-
include 'PHPExcel/Reader/Excel5.php';
-
$fileName = 'test.xlsx';
-
-
$PHPExcel = new PHPExcel();
-
-
$PHPReader = new PHPExcel_Reader_Excel2007();
-
if (!$PHPReader->canRead($fileName))
-
{
-
$PHPReader = new PHPExcel_Reader_Excel5();
-
if (!$PHPReader->canRead($fileName))
-
{
-
echo 'no Excel';
-
exit;
-
}
-
}
-
-
$PHPExcel = $PHPReader->load($fileName);
-
$currentSheet = $PHPExcel->getSheet(0);
-
$allColumn = $currentSheet->getHighestColumn();
-
$allRow = $currentSheet->getHighestRow();
-
-
for($currentRow = 2; $currentRow<=$allRow; $currentRow++)
-
{
-
for($currentColumn='A'; $currentColumn<=$allColumn; $currentColumn++)
-
{
-
$address = $currentColumn.$currentRow;
-
echo $currentSheet->getCell($address)->getValue()."\t";
-
}
-
echo "
";
-
}
3.3 使用RowIterator和CellIterator迭代读取
- <?php
-
-
error_reporting(E_ALL);
-
ini_set('include_path', ini_get('include_path').';./Classes/');
-
-
include 'PHPExcel.php';
-
include 'PHPExcel/Reader/Excel2007.php';
-
-
$objReader = new PHPExcel_Reader_Excel2007();
-
$objReader->setReadDataOnly(true);
-
$objPHPExcel = $objReader->load('test.xlsx');
-
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
-
$sheet = $objPHPExcel->getActiveSheet();
-
$array_data = array();
-
-
foreach($rowIterator as $row){
-
$cellIterator = $row->getCellIterator();
-
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
-
if(1 == $row->getRowIndex ()) continue;//skip first row
-
$rowIndex = $row->getRowIndex ();
-
$array_data[$rowIndex] = array('A'=>'', 'B'=>'','C'=>'','D'=>'');
-
-
foreach ($cellIterator as $cell) {
-
if('A' == $cell->getColumn()){
-
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
-
} else if('B' == $cell->getColumn()){
-
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
-
} else if('C' == $cell->getColumn()){
-
$array_data[$rowIndex][$cell->getColumn()] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD');
-
} else if('D' == $cell->getColumn()){
-
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
-
}
-
}
-
/*
-
$rowIndex = $row->getRowIndex ();
-
$array_data[$rowIndex] = array('A'=>'', 'B'=>'','C'=>'','D'=>'');
-
-
$cell = $sheet->getCell('A' . $rowIndex);
-
$array_data[$rowIndex]['A'] = $cell->getCalculatedValue();
-
$cell = $sheet->getCell('B' . $rowIndex);
-
$array_data[$rowIndex]['B'] = $cell->getCalculatedValue();
-
$cell = $sheet->getCell('C' . $rowIndex);
-
$array_data[$rowIndex]['C'] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD');
-
$cell = $sheet->getCell('D' . $rowIndex);
-
$array_data[$rowIndex]['D'] = $cell->getCalculatedValue();*/
-
}
-
var_dump($array_data);
3.4 使用PHPExcel_Writer_Excel2007写入
- <?php
-
-
error_reporting(E_ALL);
-
ini_set('include_path', ini_get('include_path').';./Classes/');
-
-
include 'PHPExcel.php';
-
include 'PHPExcel/Writer/Excel2007.php';
-
//include 'PHPExcel/Writer/Excel5.php';
-
-
$objPHPExcel = new PHPExcel();
-
/* set properties */
-
//$objPHPExcel->getProperties()->setCreator("Maarten Balliauw"); // 作者
-
//$objPHPExcel->getProperties()->setLastModifiedBy("Jone Smith");
-
//$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document"); //标题
-
//$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); //主题
-
//$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes."); //备注
-
//$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php"); //关键字
-
//$objPHPExcel->getProperties()->setCategory("Test result file"); //类别
-
-
/* set content */
-
//$objPHPExcel->setActiveSheetIndex(0);
-
//$objPHPExcel->getActiveSheet()->setTitle('Simple');
-
//$objPHPExcel->getActiveSheet()->setCellValue('A1', 'String');
-
//$objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
-
//$objPHPExcel->getActiveSheet()->setCellValue('A3', true);
-
//$objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)');
-
//$objPHPExcel->getActiveSheet()->setCellValue('B8', '=MIN(B2:C5)');
-
//$objPHPExcel->getActiveSheet()->mergeCells('A18:E22'); //合并单元格
-
//$objPHPExcel->getActiveSheet()->unmergeCells('A28:B28'); //分离单元格
-
//$objPHPExcel->getActiveSheet()->setCellValueExplicit('A5', '847475847857487584', PHPExcel_Cell_DataType::TYPE_STRING);
-
-
/* 保护cell */
-
//$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
-
//$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
-
-
/* cell 数字格式 */
-
//$objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
-
//$objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
-
//$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );
-
-
/* column width */
-
//$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
-
//$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
-
//$ojStyleA5 = $objPHPExcel->getActiveSheet()->getStyle('A5');
-
-
/* set font */
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
-
//$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
-
//$objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true);
-
//$objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true);
-
-
/* set align */
-
//$objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
-
//$objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
-
//$objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
-
//$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
-
//$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
-
-
/* set column border */
-
//$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB(’FF993300′);
-
//$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB(’FF993300′);
-
//$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB(’FF993300′);
-
//$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB(’FF993300′);
-
//$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB(’FF993300′);
-
//$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB(’FF993300′);
-
//$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
-
/*
-
$default_border = array(
-
'style' => PHPExcel_Style_Border::BORDER_THIN,
-
'color' => array('rgb'=>'1006A3')
-
);
-
$style_header = array(
-
'borders' => array(
-
'bottom' => $default_border,
-
'left' => $default_border,
-
'top' => $default_border,
-
'right' => $default_border,
-
),
-
'fill' => array(
-
'type' => PHPExcel_Style_Fill::FILL_SOLID,
-
'color' => array('rgb'=>'E1E0F7'),
-
),
-
'font' => array(
-
'bold' => true,
-
)
-
);
-
-
$sheet->getStyle('A1:A2')->applyFromArray( $style_header );
-
$sheet->getStyle('B1:B2')->applyFromArray( $style_header );
-
*/
-
-
/* fill color */
-
//$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
-
//$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB(’FF808080′);
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
-
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB(’FF808080′);
-
-
/* copy style */
-
//$objPHPExcel->getActiveSheet()->duplicateStyle($objStyleA5, 'B1:C22');
-
-
/* add image */
-
/*
-
$objDrawing = new PHPExcel_Worksheet_Drawing();
-
$objDrawing->setName(’Logo’);
-
$objDrawing->setDescription(’Logo’);
-
$objDrawing->setPath(’./images/officelogo.jpg’);
-
$objDrawing->setHeight(36);
-
$objDrawing->setCoordinates('B15');
-
$objDrawing->setOffsetX(110);
-
$objDrawing->setRotation(25);
-
$objDrawing->getShadow()->setVisible(true);
-
$objDrawing->getShadow()->setDirection(45);
-
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
-
*/
-
-
/* add a new worksheet */
-
//$objPHPExcel->createSheet();
-
//$objPHPExcel->getSheet(1)->setTitile('sheet2');
-
//$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
-
//$objWriter->save('php://output');
-
-
/* add summary/total row *//
-
/*
-
//Adding total column
-
$ranged_total_coordinate = chr($starting_pos+$index_pos) . '1:' . chr($starting_pos+$index_pos) . '2';
-
$sheet->setCellValue(chr($starting_pos+$index_pos) . '1', 'Total');
-
$sheet->mergeCells($ranged_total_coordinate);
-
$sheet->getStyle(chr($starting_pos+$index_pos) . '1')->getAlignment()
-
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
-
->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
-
$sheet->getStyle($ranged_total_coordinate)->applyFromArray( $style_header );
-
$rowcounter = 2;
-
foreach($array_data_user as $k1=>$v1){
-
$rowcounter++;
-
$sheet->setCellValue(chr($starting_pos+$index_pos) . $rowcounter, "=SUM(C{$rowcounter}:".chr(($starting_pos+($index_pos-1)))."{$rowcounter})");
-
$sheet->getStyle(chr($starting_pos+$index_pos) . $rowcounter)->applyFromArray( $style_body_content );
-
}
-
*/
-
-
/* parsing formula */
-
//$sheet->getCell('K3')->getValue() . " -> " . $sheet->getCell('K3')->getCalculatedValue();
-
-
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
-
//$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
-
-
/* create empty Excel */
-
$objWriter->save('xxx.xlsx');
-
-
/* force download */
-
/*
-
header('Pragma: public');
-
header('Expires: 0');
-
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
-
header('Content-Type:application/force-download');
-
header('Content-Type:application/vnd.ms-execl');
-
header('Content-Type:application/octet-stream');
-
header('Content-Type:application/download');;
-
header('Content-Disposition:attachment;filename="resume.xlsx"');
-
header('Content-Transfer-Encoding:binary');
-
$objWriter->save('php://output');*/
3.5 合并多个excel文件至一个
- <?php
-
-
include 'PHPExcel/IOFactory.php';
-
-
$inputFileType = 'CSV';
-
$inputFileNames = array('./example1.csv','./example2.csv','./example3.csv','./example4.csv');
-
-
$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);
-
echo $loadedSheetName,PHP_EOL;
-
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
-
var_dump($sheetData);
-
echo PHP_EOL;
-
}
3.6 格式转换
- <?php
-
-
$cell = $objWorksheet->getCellByColumnAndRow(3, 5);
-
$cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
-
echo $cell_value;
3.5 格式转换
- <?php
-
-
$objPHPExcel = PHPExcel_IOFactory::load("XMLTest.xml");
-
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
-
$objWriter->save('covertedXml2Xlsx.xlsx');
阅读(1155) | 评论(0) | 转发(0) |