How to use PHPEXCEL
This article is about how to use PHPEXCEL. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The first is to read the Excle content using PHP Reader:
The copy code is as follows:
Require ("https://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 there are multiple workbooks
$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;$rowgetValue ()
}
}
} else {
Foreach ($sheets as $key = > $sheet)
{
$sheetsinfo [$key] ["rows"] = $sheet- > getHighestRow ()
$sheetsinfo [$key] ["column"] = PHPExcel_Cell::columnIndexFromString ($sheet- > getHighestColumn ())
For ($row=1;$rowgetValue ()
}
}
}
}
Echo ""
Print_r ($sheetData)
Echo ""
Note: use PHP to read the contents of excel files, usually deal with formatted csv or excel, you can also read xml files
PHPExcel generates Exceel
The copy 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 ('A1neighbors, "{$this- > _ packInfos [' ostenid']}")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('B1Qing, "Volume weight (kg)")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('D1Qing, "Actual weight (kg)")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('A2, "Box No.")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('B2waters, "Products")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('C2, "Shipping Box")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('D2neighbors, "System")
$objPHPExcel- > getActiveSheet ()-> setCellValue ('E2steps, "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 ('A2neighbors. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
$objPHPExcel- > getActiveSheet ()-> getStyle ('B2shipping. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
$objPHPExcel- > getActiveSheet ()-> getStyle ('C2 (($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
$objPHPExcel- > getActiveSheet ()-> getStyle ('D2neighbors. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
$objPHPExcel- > getActiveSheet ()-> getStyle ('E2shipping. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
If ($this- > _ db- > num_rows ($query) > 0)
{
$item3
While ($row = $this- > _ db- > fetch_assoc ($query))
{
$objPHPExcel- > getActiveSheet ()-> setCellValue ('Aids. ($I), "BOX". $row [' box_num'])
$objPHPExcel- > getActiveSheet ()-> setCellValue ('blocked. ($I), sprintf ("% .2f", $row [' volume_weight']))
$objPHPExcel- > getActiveSheet ()-> setCellValue ('paid. ($I), sprintf ("% .2f", $row [' box_weight']))
$objPHPExcel- > getActiveSheet ()-> setCellValue ('Downs. ($I), sprintf (' .2f', $row ['system_weight']))
$objPHPExcel- > getActiveSheet ()-> setCellValue ('eBay. ($I), sprintf ("% .2f", $row [' real_weight']))
$objPHPExcel- > getActiveSheet ()-> getStyle ('Aids. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
$objPHPExcel- > getActiveSheet ()-> getStyle ('baked. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
$objPHPExcel- > getActiveSheet ()-> getStyle ('paid. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
$objPHPExcel- > getActiveSheet ()-> getStyle ('Downs. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
$objPHPExcel- > getActiveSheet ()-> getStyle ('eBay. ($I))-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
$iTunes +
}
}
$fileName= "exportBox.xls"
$filePath = dirname (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 generates excel directly in the form of A tag. If you use ajax, you can directly echo $path without using header, and the path returned by the foreground _ window.location.href= is fine.
Thank you for reading! This is the end of the article on "how to use PHPEXCEL". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!