Home » excel » excel – Disable popup message in php

excel – Disable popup message in php

Posted by: admin May 14, 2020 Leave a comment

Questions:

When i upload a excel file, i have used COM() to open and automate converting it to xml.

It works fine, But when i run it, it always shows the message from Microsoft Excel:
A file named ”’ already exists in this location. Do you want to replace it?
I can choose between Yes No and Cancel.
normally i would choose Yes. But i dont want users to click on Yes each time.
Can i disable this?
Please inform me if any relevant codes need to be posted.
Thanks
UPDATE Here’s part of my code using unlink().

$workbook = $_FILES['file']['tmp_name']
$sheet = "Sheet1";
$ext = substr($workbook, strrpos($workbook, '.') + 1);

$ex = new COM("Excel.sheet") or die("Did not connect");

//Open the workbook that we want to use.
$wkb = $ex->application->Workbooks->Open($workbook) or die("Did not open");
$path = "D:\b2\test1.xml";
$format = 46;
unlink($path);
$path = "D:\b2\test1.xml";
//Create a copy of the workbook, so the original workbook will be preserved.
$ex->Application->ActiveWorkbook->SaveAs($path, $format);

Is it the right way to use it? Because it does not seem to work

How to&Answers:

If you’re always going to overwrite, the simplest way is probably just to delete the file first before you get Excel to open it. The PHP function to delete a file is unlink()

Answer:

I solved it by adding this line.

$ex->application->displayAlerts = 0;