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

相关推荐
爱串门的小马驹15 分钟前
exel带单位求和,统计元素个数
excel
GitLqr34 分钟前
Android - 云游戏本地悬浮输入框实现
android·开源·jitpack
周周的Unity小屋35 分钟前
Unity实现安卓App预览图片、Pdf文件和视频的一种解决方案
android·unity·pdf·游戏引擎·webview·3dwebview
金融小白数据分析之路3 小时前
python自动移除excel文件密码(升级v2版本)
开发语言·python·excel
单丽尔3 小时前
Gemini for China 大更新,现已上架 Android APP!
android
酷爱码4 小时前
WordPress作品设计素材图片站资讯文章教程uigreat主题
php
pkowner4 小时前
phpcms 升级php8.3.8
php·apache
liuxin334455664 小时前
深入掌握Symfony与Composer:PHP依赖管理的艺术
php·composer·symfony
JerryHe4 小时前
Android Camera API发展历程
android·数码相机·camera·camera api
Synaric5 小时前
Android与Java后端联调RSA加密的注意事项
android·java·开发语言