drzrdc1766788 2012-12-31 15:02
浏览 200
已采纳

导出excel的PHP设置路径

i would like to export excel to specific folder where user can save it wherever they want. currently i already succeed with export to excel but i test it locally and it downloaded to my local server folder. i hope someone can help me. thank you.

$id=$_GET['id'];
$excel=new ExcelWriter("./excel/company".$id.".xls");
if($excel==false)   
echo $excel->error;
$myArr=array("NO", "COMPANY", "ADDRESS1", "ADDRESS2", "ADDRESS3", "POSTCODE", "CITY", "STATE", "PHONE NO", "FAX NO", "EMAIL", "WEBSITE");
$excel->writeLine($myArr);
$qry=mysql_query(" SELECT organizations.*, states.state, cities.city FROM organizations, states, cities WHERE idOrg='$id' AND organizations.state_id=states.idState AND organizations.city_id=cities.idCity");
if($qry!=false)
{
    $i=1;
    while($res=mysql_fetch_array($qry))
    {
        $myArr=array($i,$res['companyName'], $res['add1'], $res['add2'], $res['add3'], $res['postcode'], $res['city'],$res['state'], $res['phoneNo'], $res['faxNo'], $res['email'], $res['website']);
        $excel->writeLine($myArr);
        $i++;
    }
}
  • 写回答

3条回答 默认 最新

  • duankan8739 2012-12-31 15:17
    关注

    Your question title suggests that you are under the assumption that, using PHP, you can save an Excel file on the client's PC. Not exactly. You can save it to your local webserver (as you've already found out). But to transfer that to the client PC, you should offer it up as a download.

    Once the file is done being written (as you are already doing), present the user with a link to the file on your server. They will then be able to choose where it should be downloaded on their PC.

    Another possibility would be to write out the excel file data as the HTTP response and set the Content-Type header to something like application/vnd.ms-excel (or whatever is correct for your file type) and a Content-Disposition of attachment (example).

    I'm not familiar with ExcelWriter, so I don't know if it has some kind of a dump function or not. Perhaps after you write out the file you just do something like this:

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename=company'. $id .'.xls');
    
    echo file_get_contents('./excel/company'. $id .'.xls');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下