ai聊天消息内容调用PHP写到excel中

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

相关推荐
消失的旧时光-194344 分钟前
Android USB 通信开发
android·java
吃汉堡吃到饱1 小时前
【Android】浅析View.post()
android
咕噜企业签名分发-淼淼1 小时前
开发源码搭建一码双端应用分发平台教程:逐步分析注意事项
android·ios
Bruce_Liuxiaowei1 小时前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
betazhou2 小时前
mariadb5.5.56在centos7.6环境安装
android·数据库·adb·mariadb·msyql
痴人说梦梦中人7 小时前
自建 dnslog 回显平台:渗透测试场景下的隐蔽回显利器
web安全·网络安全·渗透测试·php·工具
doublelixin7 小时前
AOSP (Android11) 集成Google GMS三件套
android
沉到海底去吧Go10 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
xzkyd outpaper10 小时前
onSaveInstanceState() 和 ViewModel 在数据保存能力差异
android·计算机八股
CYRUS STUDIO11 小时前
FART 脱壳某大厂 App + CodeItem 修复 dex + 反编译还原源码
android·安全·逆向·app加固·fart·脱壳