doubi1624 2017-02-17 07:30
浏览 68
已采纳

PHPExcel将整页转换为XLSX

I'm trying to use PHPExcel to convert a report page to XLSX but i couldn't be able to do it. My PHP file as below;

error_reporting(E_ALL);
ini_set('display_errors', 0);
include("SqlSettings.php");
function date_tr($f, $zt = 'now'){
    $z = date("$f", strtotime($zt));
    $donustur = array(
        'Monday'    => 'Pazartesi',
        'Tuesday'   => 'Salı',
        'Wednesday' => 'Çarşamba',
        'Thursday'  => 'Perşembe',
        'Friday'    => 'Cuma',
        'Saturday'  => 'Cumartesi',
        'Sunday'    => 'Pazar',
        'January'   => 'Ocak',
        'February'  => 'Şubat',
        'March'     => 'Mart',
        'April'     => 'Nisan',
        'May'       => 'Mayıs',
        'June'      => 'Haziran',
        'July'      => 'Temmuz',
        'August'    => 'Ağustos',
        'September' => 'Eylül',
        'October'   => 'Ekim',
        'November'  => 'Kasım',
        'December'  => 'Aralık',
        'Mon'       => 'Pts',
        'Tue'       => 'Sal',
        'Wed'       => 'Çar',
        'Thu'       => 'Per',
        'Fri'       => 'Cum',
        'Sat'       => 'Cts',
        'Sun'       => 'Paz',
        'Jan'       => 'Oca',
        'Feb'       => 'Şub',
        'Mar'       => 'Mar',
        'Apr'       => 'Nis',
        'Jun'       => 'Haz',
        'Jul'       => 'Tem',
        'Aug'       => 'Ağu',
        'Sep'       => 'Eyl',
        'Oct'       => 'Eki',
        'Nov'       => 'Kas',
        'Dec'       => 'Ara',
    );
    foreach($donustur as $en => $tr){
        $z = str_replace($en, $tr, $z);
    }
    if(strpos($z, 'Mayıs') !== false && strpos($f, 'F') === false) $z = str_replace('Mayıs', 'May', $z);
    return $z;
}
date_default_timezone_set('Europe/Istanbul');
$timestamp = strtotime(date('H:i:s')) + 60*60;
$ssaat = date('H:i:s', $timestamp);
$dun=date('d.m.Y',strtotime("-1 days"));
$dun=date("Y-m-d", strtotime($dun));
$dun="2017-02-02";
?>
<html>
<head>
  <title>Mürekkep Raporu</title>
<META http-equiv=content-type content=text/html;charset=iso-8859-9>
<META http-equiv=content-type content=text/html;charset=windows-1254>
<META http-equiv=content-type content=text/html;charset=x-mac-turkish>  
<link href="https://fonts.googleapis.com/css?family=Noto+Serif" rel="stylesheet">
<style>
body
{
    font-family: 'Noto Serif', serif;
    font-size: 12px;
    font-weight: bold;
}
table
{
    font-family: 'Noto Serif', serif;
    font-size: 12px;
    font-weight: bold;

}
</style>
</head>
<body>
<?php
$result=mysql_query("select * from murekkep where sipkod='".$_GET["sipkod"]."'");
$data=mysql_fetch_row($result);
$isemri=$data[32];
$tarih=date("d.m.Y", strtotime($data[30]));
$makina=$data[37];
$musteri=$data[34];
$urun=$data[35];
$sorgu_hmm=mysql_query("select * from hmm where sipkod='".$_GET["sipkod"]."'");
$veri=mysql_fetch_row($sorgu_hmm);
$hozellik=$veri[2];
$hmiktar=$veri[3];
$hgecsay=$veri[4];
$hgecyon=$veri[5];
if ($_GET["excel"]<>1)
{
print "
<p align='left'>
<a href='Rapor.php?sipkod=".$_GET["sipkod"]."&excel=1'><img src='excel.png' width='48px' height='48px' border='0' alt='Excel Dosyasına Aktar' title='Excel Dosyasına Aktar' style='cursor:pointer;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;background-color:#FFFFFF;-webkit-box-shadow: #B3B3B3 5px 5px 5px;-moz-box-shadow: #B3B3B3 5px 5px 5px; box-shadow: #B3B3B3 5px 5px 5px;'></a>
</p>
";
}//excele aktar parametresi kapalı
else
{

}//excele aktar parametresi açık
print "
<table border='1' cellpadding='1' cellspacing='1' align='center' width='100%'>
  <tr>
    <td align='center' width='100px'>İŞ EMRİ NO</td>
    <td align='center' width='100px'>TARİH</td>
    <td align='center' width='100px'>MÜŞTERİ</td>
    <td align='center' width='100px'>ÜRÜN</td>
    <td align='center' width='400px'>
    <center>HAM MADDE</center>
      <table border='0' cellpadding='0' cellspacing='0' align='center' width='100%' bordercolor='black'>
        <tr>
          <td align='center' width='100px'>ÖZELLİĞİ</td>
          <td align='center' width='100px'>MİKTARI(Tabaka)</td>
          <td align='center' width='100px'>GEÇİŞ SAYISI</td>
          <td align='center' width='100px'>GEÇİŞ YÖNÜ</td>
        </tr>  
</table>    
</td>
<td align='center' width='400px'>
<center>MÜREKKEP</center>
<table border='0' cellpadding='0' cellspacing='0' align='center' width='100%' bordercolor='green'>
<tr>
<td align='center' width='100px'>LOT NO</td>
<td align='center' width='100px'>RENK</td>
<td align='center' width='100px'>MİKTAR (KG)</td>
<td align='center' width='100px'>MİKTAR (GR)</td>
</tr>  
</table>    
    </td>
    <td align='center' width='100px'>HAMMADDE &Delta; DEĞERİ</td>
    <td align='center' width='100px'>MAKİNA</td>
  </tr>
";
$sorgu_renkler=mysql_query("select * from renkler where sipkod='".$_GET["sipkod"]."'") or die (mysql_error());
  while ($rinf=mysql_fetch_array($sorgu_renkler))
  {
print "
<tr>
<td align='center' width='100px'>".$isemri."</td>
<td align='center' width='100px'>".$tarih."</td>
<td align='center' width='100px'>".$musteri."</td>
<td align='center' width='100px'>".$urun."</td>
<td align='center' width='100px'>
<table border='0' cellpadding='0' cellspacing='0' align='center' width='100%'>
<tr>
<td align='center' width='100px'>".$hozellik."</td>
<td align='center' width='100px'>".number_format($hmiktar, 0, ',', '.')."</td>
<td align='center' width='100px'>".$hgecsay."</td>
<td align='center' width='100px'>".$hgecyon."</td>
</tr>
</table>
</td>
<td align='center' width='100px'>
<table border='0' cellpadding='0' cellspacing='0' align='center' width='100%'>
<tr>
<td align='center' width='100px'>".$rinf[2]."</td>
<td align='center' width='100px'>".$rinf[1]."</td>
<td align='center' width='100px'>".$rinf[3]."</td>
<td align='center' width='100px'>".$rinf[4]."</td>
</tr>
</table>
</td>
<td align='center' width='100px'>0</td>
<td align='center' width='100px'>".$makina."</td>
</tr>
";    
  }//sorgu_renkler while
print " 
</table>  
";
</body>
</html>

I have downloaded PHPExcel's latest version from official web site and tried to make an XLSX file according to "01simple-download-xlsx" in the examples folder. But as a beginner in PHP i have failed. According to examples(as far as i understand) i need to format every cell and data in it before create an XLSX file but i couldn't understand how. I have searched via stackoverflow and found some examples which basicly creating a HTML file and make PHPExcel parse the data from it, i have tried those examples but it's breaking my design.

My design looks like this; enter image description here

But when i create a XLSX file it returns to this; enter image description here

Some of the characters went missing because of the language code of page and i know i can fix that with UTF-8 coding (usually i'm using latin5) but as you can see whole desing is breaking up.

Another thing i have tried that to copy XLSX example from examples folder and i have edited my code as below;

//added this codes to top of the page
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Murekkep_Raporu_'.$_GET["sipkod"].'.xls"');
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')

//and added this code bottom of the page
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

With this modification it's creating XLSX file but when you try to open excel says file is either corrupted or can not readable.

Is there any way to convert whole page to XLSX file without breaking the design ?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsh12544 2017-02-17 09:47
    关注

    I'm not familiar using phpexcel, but there is a really easy to use php library for Excel and OpenOffice sheets: https://github.com/box/spout

    Using Spout, you can also setup Encoding.

    I would suggest to try this library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺