|
@@ -86,11 +86,12 @@ function getExcelDatas($filepath, $sheetIndex = 0) {
|
|
|
* @param type $columns 列标题
|
|
|
* @param type $rows 内容
|
|
|
* @param string $filename 文件名
|
|
|
+ * @param string $settings 样式批设置
|
|
|
* @param type $sheetname sheet标题
|
|
|
* @param type $saveurl 保存位置
|
|
|
* @param type $author 作者
|
|
|
*/
|
|
|
-function export($columns, $rows, $filename = "jjrcw", $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
|
|
|
+function export($columns, $rows, $filename = "jjrcw", $settings = [], $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
|
|
|
$datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
|
|
$spreadsheet->getProperties()->setCreator($author)
|
|
@@ -121,10 +122,91 @@ function export($columns, $rows, $filename = "jjrcw", $sheetname = "sheet1", $sa
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽
|
|
|
+ $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高
|
|
|
+ //样式设置
|
|
|
+ foreach ($settings as $type => $cfg) {
|
|
|
+ switch ($type) {
|
|
|
+ case "width":
|
|
|
+ for ($i = 0; $i < count($cfg); $i++) {
|
|
|
+ $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "height":
|
|
|
+ for ($i = 0; $i < count($cfg); $i++) {
|
|
|
+ $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "background-color":
|
|
|
+ for ($i = 0; $i < count($cfg); $i++) {
|
|
|
+ $objPHPExcel->getStyle($cfg[$i][0])->getFill()->setFillType(PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor($cfg[$i][1])->setARGB($cfg[$i][1]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "border":
|
|
|
+ $objPHPExcel->getStyle($cfg)->applyFromArray(array(
|
|
|
+ "borders" => array(
|
|
|
+ "allBorders" => array(
|
|
|
+ 'borderStyle' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ));
|
|
|
+ break;
|
|
|
+ case "align":
|
|
|
+ for ($i = 0; $i < count($cfg); $i++) {
|
|
|
+ switch ($cfg[$i][1]) {
|
|
|
+ case "left":
|
|
|
+ $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT;
|
|
|
+ break;
|
|
|
+ case "right":
|
|
|
+ $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT;
|
|
|
+ break;
|
|
|
+ case "hCenter":
|
|
|
+ $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER;
|
|
|
+ break;
|
|
|
+ case "hJustify":
|
|
|
+ $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY;
|
|
|
+ break;
|
|
|
+ case "top":
|
|
|
+ $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP;
|
|
|
+ break;
|
|
|
+ case "bottom":
|
|
|
+ $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM;
|
|
|
+ break;
|
|
|
+ case "vCenter":
|
|
|
+ $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER;
|
|
|
+ break;
|
|
|
+ case "vJustify":
|
|
|
+ $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ $align = [];
|
|
|
+ if ($hAlign) {
|
|
|
+ $align["horizontal"] = $hAlign;
|
|
|
+ }
|
|
|
+ if ($vAlign) {
|
|
|
+ $align["vertical"] = $vAlign;
|
|
|
+ }
|
|
|
+ $objPHPExcel->getStyle($cfg[$i][0])->applyFromArray([
|
|
|
+ "alignment" => $align
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "color":
|
|
|
+ for ($i = 0; $i < count($cfg); $i++) {
|
|
|
+ $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "wrap":
|
|
|
+ for ($i = 0; $i < count($cfg); $i++) {
|
|
|
+ $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
header('Content-Type: application/vnd.ms-excel');
|
|
|
- header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
|
|
|
- $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
|
|
|
+ header('Content-Disposition: attachment;filename="' . $filename . '"');
|
|
|
+ $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
|
|
|
$writer->save($saveurl);
|
|
|
//删除临时的sheet
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
@@ -146,6 +228,20 @@ function getExcelColumnByIndex(int $index) {
|
|
|
return $letters[$rowIndex] . $letters[$colIndex];
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 公共的excel设置
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+function getCommonExcelSetting($columns, $rows) {
|
|
|
+ return $settings = [
|
|
|
+ "background-color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "0066CC"]],
|
|
|
+ "color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "FFFFFF"]],
|
|
|
+ "border" => sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1),
|
|
|
+ "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1)],
|
|
|
+ "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "vCenter"]]
|
|
|
+ ];
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
|
|
|
* @param type $mime
|
|
@@ -177,7 +273,7 @@ function getStoragePath($path) {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
-function getFileView($path){
|
|
|
- $complete_path = "https://report.jinjianghc.com/".getStoragePath($path);
|
|
|
- return "https://fileview.jinjianghc.com/onlinePreview?url=".base64_encode($complete_path);
|
|
|
+function getFileView($path) {
|
|
|
+ $complete_path = "https://report.jinjianghc.com/" . getStoragePath($path);
|
|
|
+ return "https://fileview.jinjianghc.com/onlinePreview?url=" . base64_encode($complete_path);
|
|
|
}
|