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

相关推荐
booboosui9 分钟前
安卓 8.1 也能装了:代价是一条黄字和两个静默 bug
android
.痞老板.23 分钟前
java ee--网络编程套接字
网络·java-ee·php
Ehtan_Zheng3 小时前
Jetpack AppState Transform:让 Compose 的响应式状态走出 UI 层
android
yyuuuzz5 小时前
nginx 502 排查,先看上游超时那一行
android·java·运维·nginx·502
用户0332126663678 小时前
使用 Python 删除 Excel 中的空白行或空白列
python·excel
恋猫de小郭9 小时前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter
alexhilton19 小时前
MVI 让屏幕一次只有一个状态
android·kotlin·android jetpack
二流小码农1 天前
鸿蒙开发:证书只能创建3个,第4个应用怎么办
android·ios·harmonyos
阿杰m1 天前
Android源码第十七章:PowerManager 与后台任务——锁屏后怎么跑?
android
JMchen1231 天前
【Android 性能优化实战 60 讲】10 Jetpack App Startup 内核魔改:手写异步懒加载启动框架,根治串行初始化耗时
android·性能优化·源码分析·启动优化·kotlin协程·app startup