繁体   English   中英

PHPExcel将整页转换为XLSX

[英]PHPExcel Convert Whole Page to XLSX

我正在尝试使用PHPExcel将报表页面转换为XLSX,但我无法做到。 我的PHP文件如下;

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>

我从官方网站下载了PHPExcel的最新版本,并尝试根据示例文件夹中的“01simple-download-xlsx”制作XLSX文件。 但作为PHP的初学者,我失败了。 根据示例(据我所知),我需要在创建XLSX文件之前格式化其中的每个单元格和数据,但我无法理解如何。 我已经通过stackoverflow进行了搜索,发现了一些基本创建HTML文件并让PHPExcel从中解析数据的例子,我试过这些例子,但它破坏了我的设计。

我的设计看起来像这样; 在此输入图像描述

但是当我创建一个XLSX文件时,它返回到此; 在此输入图像描述

由于页面的语言代码,一些字符丢失了,我知道我可以用UTF-8编码解决这个问题(通常我使用的是latin5)但是你可以看到整个设计正在分解。

我尝试过从示例文件夹中复制XLSX示例的另一件事,我编辑了我的代码,如下所示;

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

通过此修改,它创建了XLSX文件,但是当您尝试打开excel时,表示文件已损坏或无法读取。

有没有办法将整个页面转换为XLSX文件而不破坏设计?

提前致谢。

我不熟悉使用phpexcel,但有一个非常容易使用的PHP库用于Excel和OpenOffice表: https//github.com/box/spout

使用Spout,您还可以设置编码。

我建议尝试这个库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM