2017-02-17 07:30
浏览 67


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;

ini_set('display_errors', 0);
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;
$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));
  <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="" rel="stylesheet">
    font-family: 'Noto Serif', serif;
    font-size: 12px;
    font-weight: bold;
    font-family: 'Noto Serif', serif;
    font-size: 12px;
    font-weight: bold;

$result=mysql_query("select * from murekkep where sipkod='".$_GET["sipkod"]."'");
$tarih=date("d.m.Y", strtotime($data[30]));
$sorgu_hmm=mysql_query("select * from hmm where sipkod='".$_GET["sipkod"]."'");
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>
}//excele aktar parametresi kapalı

}//excele aktar parametresi açık
print "
<table border='1' cellpadding='1' cellspacing='1' align='center' width='100%'>
    <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'>
          <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>
<td align='center' width='400px'>
<table border='0' cellpadding='0' cellspacing='0' align='center' width='100%' bordercolor='green'>
<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>
    <td align='center' width='100px'>HAMMADDE &Delta; DEĞERİ</td>
    <td align='center' width='100px'>MAKİNA</td>
$sorgu_renkler=mysql_query("select * from renkler where sipkod='".$_GET["sipkod"]."'") or die (mysql_error());
  while ($rinf=mysql_fetch_array($sorgu_renkler))
print "
<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%'>
<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>
<td align='center' width='100px'>
<table border='0' cellpadding='0' cellspacing='0' align='center' width='100%'>
<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>
<td align='center' width='100px'>0</td>
<td align='center' width='100px'>".$makina."</td>
  }//sorgu_renkler while
print " 

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/');
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');

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:

    Using Spout, you can also setup Encoding.

    I would suggest to try this library.

    解决 无用
    打赏 举报

相关推荐 更多相似问题