First, use PHP Reader to read Excle content:
Copy codeThe Code is as follows:
Require ("http://www.jb51.net/PHPExcel/Classes/PHPExcel.php ");
$ File = "D :\\ datas.xlsx ";
If (! File_exists ($ file )){
Die ("no file found in {$ file }");
}
$ DatasReader = PHPExcel_IOFactory: load ($ file );
$ Sheets = $ datasReader-> getAllSheets ();
// If multiple workbooks exist
$ 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; $ row <= $ sheetsinfo ["rows"]; $ row ++ ){
For ($ column = 0; $ column <$ sheetsinfo ["column"]; $ 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 "<pre> ";
Print_r ($ sheetData );
Echo "</pre> ";
Note: When using PHP to read the content of an excel file, it is generally used to process the formatted csv or excel file, and you can also read xml files.
PHPExcel generate Exceel
Copy codeThe Code is as follows:
$ 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 = new 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 ', "Volume weight (kg )");
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('d1 ', "Actual weight (kg )");
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('a2 ', "Box No .");
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('b2', "Products ");
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('c2 ', "Shipping Box ");
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('d2 ', "System ");
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('E2', "Input ");
$ 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 _ upload']);
$ 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 ("_ FILE _"). "/template/". $ fileName;
$ Path = "./template/". $ fileName;
$ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel );
If (file_exists ($ path )){
Chmod ($ path, 0777 );
Unlink ($ path );
$ ObjWriter-> save ($ path );
Header ('application/vnd. ms-excel ');
Header ('content-Disposition: attachment; filename = weight-'. $ this-> _ packInfos ["o_id"]. ". xlsx ");
Readfile ($ filePath );
Die ();
}
Else
{
$ ObjWriter-> save ($ path );
Header ('application/vnd. ms-excel ');
Header ('content-Disposition: attachment; filename = weight-'. $ this-> _ packInfos ["o_id"]. ". xlsx ");
Readfile ($ filePath );
Die ();
}
Note: The above php excel generation method is directly in the form of A tag. If ajax is used, you can directly echo $ path without header, front-end window. location. href = the returned path is enough.