Xinu

php调用excel中的宏
        之前有个项目,需要...
扫描右侧二维码阅读全文
12
2017/10

php调用excel中的宏

        之前有个项目,需要调用excel做运算,算法如果使用php实现过于麻烦,就用php来调用excel。想用phpExcel,但是测试了好久,宏始终无法运行,最终使用的php调用com组件
环境要求:

  1. php开启com组件支持,根据php版本的不同,开启方式不同,请自行百度
  2. 服务器环境要求windows,并安装正版excel,wps不可以。
    上代码:
<?php
//调用的excel文件必须是绝对路径
$file_path = realpath("./assets/excel/tmp_excel/" . $file_name);
//实例化COM组件
$excel = new COM("excel.application") or die("实例化COM组件失败");
$excel->Application->WorkBooks->Open($file_path) or die('excel文件打开失败');
//为某个表格赋值
$excel->Worksheets("Strategic Asset Allocation")->Range("C2")->Value = "Custom";
//执行名字为Solver2_loop的宏计算
$excel->run("Solver2_loop");
//不显示提示
$excel->DisplayAlerts = false;
//保存运行结果
$excel->ActiveWorkBook->save();
//关闭sheet
$excel->ActiveWorkBook->close();
//关闭工作簿
$excel->Quit();
//清空对象
$excel = null;

如果调用宏的过程中,出现如下错误:
System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file 'D:RadioSQANLRWIP'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook. at Excel.WorkbookClass.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout) at Reports_WIP.btnWIP_Click(Object sender, EventArgs e)
解决方案:
创建目录“C: Windows SysWOW64 config systemprofile Desktop ”(对于64位Windows)或“C: Windows System32 config systemprofile Desktop ”(对于32位Windows)
原文链接:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d6c383a-94eb-4898-9d22-aa4bb69be25b/excel-interop-systemruntimeinteropservicescomexception-0x800a03ec-microsoft-office-excel?forum=vbgeneral</p>

Last modification:November 2nd, 2017 at 08:52 pm

2 comments

  1. rr

  2. yaoxinyu

    ?

Leave a Comment