1、调用类包
require_once dirname(FILE ) . '.../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
2、写入excel行列以及sheet名
php
<?php
/**
* PHPExcel
*
* Copyright (C) 2006 - 2014 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.8.0, 2014-03-02
*/
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$msg_content=$_GET['msg_content'];
$names = preg_split("/<br>/",$msg_content);
//$names = str_replace("<br>", "\n", $msg_content);
$tmp = '';
$i=1;
foreach($names as $name){
// todo something eg: echo $name;
//$tmp .= $name.'
//';
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $name);
$i= $i+1;
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
3、网页端调用代码
html
const excelexport = async () => {
//这里可以正常链接打开excel下载
window.location.href="http://exceldownload:9077/phpexcel-master/test03.php?msg_content="+props.message.content.replace(/\r|\n/g, '<br>');
// const { data, error } = await useMyFetch('/api/excelexport', {
// method: 'POST'
// })
}
网页端代码
html
<script setup>
import copy from 'copy-to-clipboard'
import axios from "axios";
const props = defineProps({
message: {
type: Object,
required: true
},
messageIndex: {
type: Number,
required: true
},
usePrompt: {
type: Function,
required: true
},
deleteMessage: {
type: Function,
required: true
},
toggleMessage: {
type: Function,
required: true
}
})
const snackbar = ref(false)
const snackbarText = ref('')
const showSnackbar = (text) => {
alert("icon------1003----");
snackbarText.value = text
snackbar.value = true
}
const copyMessage = () => {
copy(props.message.content)
alert("---999---");
alert("msgid:"+props.message.id);
alert("content:"+props.message.content);
console.log(props.message);
console.log(props.message.content);
showSnackbar('已复制!')
}
const editMessage = () => {
alert("icon------1002----");
props.usePrompt(props.message.message)
}
//add by grj 20240304 start
// const exportExcel = async () => {
// const { data, error } = await useAuthFetch(`http://exceldownload:9077/phpexcel-master/test03.php`, {
// method: 'POST'
// })
// alert("------------8888888888888--------------");
// }
const excelexport = async () => {
//这里可以正常链接打开excel下载
window.location.href="http://exceldownload:9077/phpexcel-master/test03.php?msg_content="+props.message.content.replace(/\r|\n/g, '<br>');
// const { data, error } = await useMyFetch('/api/excelexport', {
// method: 'POST'
// })
}
//add by grj 20240304 end
const deleteMessage = async () => {
const { data, error } = await useAuthFetch(`/api/chat/messages/${props.message.id}/`, {
method: 'DELETE'
})
alert("------------9001--------------");
if (!error.value) {
props.deleteMessage(props.messageIndex)
showSnackbar('已删除!')
}
showSnackbar('Delete failed')
}
const toggle_message = async() => {
alert("icon------1001----");
const msg = Object.assign({}, props.message)
msg.is_disabled = !msg.is_disabled
const { data, error } = await useAuthFetch(`/api/chat/messages/${props.message.id}/`, {
method: 'PUT',
body: JSON.stringify(msg),
})
if (!error.value) {
props.toggleMessage(props.messageIndex)
}
}
function selectMessageIcon(message) {
if (message.is_bot) return ""
if (message.message_type == 100) {
return "travel_explore"
} else if (message.message_type == 110) {
return "local_library"
} else if (message.message_type == 120) {
return "article"
}
return ""
}
const message_icon = selectMessageIcon(props.message)
</script>
<template>
<v-menu>
<template v-slot:activator="{ props }">
<v-btn
v-bind="props"
v-if="message_icon"
variant="text"
class="ma-2"
>
<v-icon :icon="message_icon"></v-icon>
</v-btn>
</template>
<v-list>
<v-list-item
@click="toggle_message()"
title="toggle"
:prepend-icon="message.is_disabled ? 'toggle_off' : 'toggle_on'"
>
</v-list-item>
</v-list>
</v-menu>
<v-menu
>
<template v-slot:activator="{ props }">
<v-btn
v-bind="props"
icon
variant="text"
class="mx-1 ma-2"
>
<v-icon icon="more_horiz"></v-icon>
</v-btn>
</template>
<v-list>
<v-list-item
@click="copyMessage()"
:title="$t('copy')"
prepend-icon="content_copy"
>
</v-list-item>
<!-- 导出excel add by grj -->
<v-list-item
@click="excelexport()"
:title="$t('excelExport')"
prepend-icon="edit"
>
</v-list-item>
<!--
<v-list-item
@click="deleteMessage()"
:title="$t('delete')"
prepend-icon="delete"
>
</v-list-item>
-->
</v-list>
</v-menu>
<v-snackbar
v-model="snackbar"
location="top"
timeout="2000"
>
{{ snackbarText }}
</v-snackbar>
</template>
<style scoped>
</style>
php rout