まず最初に、PHP Reader を使用します读取Excle 内容:
复制番号代码如下:
require("http://www.jb51.net/PHPExcel/Classes/PHPExcel.php");
$file = "D:\datas.xlsx";
if(!file_exists($file)){
die("{$file} にファイルが見つかりません");
}
$datasReader = PHPExcel_IOFactory::load($file);
$sheets = $datasReader->getAllSheets();
//如果には複数のワークブックがあります
$countSheets = count($sheets);
$sheetsinfo = array();
$sheetData = array();
if($countSheets==1){
$sheet = $sheets[0];
$sheetsinfo["rows"] = $sheet->getHighestRow();
$sheetsinfo["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
for($row=1;$rowfor($column=0;$column$sheetData [$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue();
}
}
}else{
foreach ($sheets as $key => $sheet)
{
$sheetsinfo[$key]["rows"] = $sheet->getHighestRow();
$sheetsinfo[$key]["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
for($row=1;$row<=$sheetsinfo[$key]["rows"];$row++){
for($column=0;$column<$sheetsinfo[$key]["column"] ;$column++){
$sheetData[$key][$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue();
}
}
}
}
echo "
"; <br>print_r($sheetData); <br>echo "
";
注: PHP 读取excel文件内容,一般都是処理整理好格式的csv或はexcel,读取xml文件
PHPExcel生成Excel
复制码代码如下:
$sql = sprintf("select * from table where op_id=%d", intval($this->params['id']));
$query = $this->_db->query($sql);
require_once './PHPExcel_1.7.4/Classes/PHPExcel.php';
$objPHPExcel = 新しい PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$objPHPExcel->getActiveSheet()->setCellValue('A1', "{$this->_packInfos['o_id']}");
$objPHPExcel->getActiveSheet()->setCellValue('B1', "体積重量 (kg)");
$objPHPExcel->getActiveSheet()->setCellValue('D1', "実際の重量 (kg)");
$objPHPExcel->getActiveSheet()->setCellValue('A2', "ボックス番号");
$objPHPExcel->getActiveSheet()->setCellValue('B2', "Products");
$objPHPExcel->getActiveSheet()->setCellValue('C2', "配送ボックス");
$objPHPExcel->getActiveSheet()->setCellValue('D2', "システム");
$objPHPExcel->getActiveSheet()->setCellValue('E2', "入力");
$objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->mergeCells("B1:C1");
$objActSheet->mergeCells("D1:E1");
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B2'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C2'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D2'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E2'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
if($this->_db->num_rows($query)>0)
{
$i=3;
while ($row = $this->db->fetch_assoc($query))
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.($i),"BOX ". $row['box_num']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.($i),sprintf("%.2f",$row['volume_weight']));
$objPHPExcel->getActiveSheet()->setCellValue('C'.($i),sprintf("%.2f",$row['box_weight']));
$objPHPExcel->getActiveSheet()->setCellValue('D'.($i),sprintf("%.2f",$row['system_weight']));
$objPHPExcel->getActiveSheet()->setCellValue('E'.($i),sprintf("%.2f",$row['real_weight']));
$objPHPExcel->getActiveSheet()->getStyle('A'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('C'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('E'.($i))->getAlignment()->sethorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$i++;
}
}
$fileName="exportBox.xls";
$filePath = dirname(dirname("__FILE__"))."/template/".$fileName;
$path = "./template/".$ファイル名;
$objWriter = 新しい PHPExcel_Writer_Excel2007($objPHPExcel);
if(file_exists($path)){
chmod($path, 0777);
リンク解除($path);
$objWriter->save($path);
header('application/vnd.ms-excel');
header('Content-Disposition:attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx");
readfile($filePath);
死ぬ();
}
else
{
$objWriter->save($path);
header('application/vnd.ms-excel');
header('Content-Disposition:attachment;filename=weight-'.$this-> ;_packInfos["o_id"].".xlsx");
readfile($filePath);
die();
注: Excel を生成する上記の方法は、A タグ フォームを直接使用します。 Ajax を使用する場合、ヘッダーを使用する必要はありません。単に echo $path、フロントエンド window.location.href=返されたパスを使用します。
http://www.bkjia.com/PHPjc/326403.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326403.html技術記事まず、PHP Reader を使用して Excel コンテンツを読み取ります。 次のようにコードをコピーします。 $file = "D:\datas.xlsx" "; if(!file_exists($fi...