一个很好用excel导出类
2013-11-27 13:35
330 查看
<?php
class PHPExcel
{
/**
* Header of excel document (prepended to the rows)
*/
public $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?\>
<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
/**
* Footer of excel document (appended to the rows)
*/
public $footer = "</Workbook>";
public $lines = array();
public $worksheet_title = "Table1";
/**
* Add a single row to the $document string
*/
public function addRow($array)
{
$cells = "";
foreach ($array as $k => $v)
{
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
/**continue;
if (is_numeric($v)) {
// 防止首字母为 0 时生成 excel 后 0 丢失
if (substr($v, 0, 1) == 0) {
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
} else {
$cells .= "<Cell><Data ss:Type=\"Number\">" . $v . "</Data></Cell>\n";
}
} else {
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
}*/
}
$this->lines[] = "<Row>\n" . $cells . "</Row>\n";
}
/**
* Add an array to the document
*/
public function addArray($array)
{
foreach ($array as $k => $v):
$this->addRow($v);
endforeach;
}
/**
* Set the worksheet title
* @access public
* @param string $title Designed title
*/
public function setWorksheetTitle($title)
{
// strip out special chars first
$title = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
$title = substr($title, 0, 31);
$this->worksheet_title = $title;
}
/**
* Generate the excel file
*/
public function generateXML($filename)
{
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
echo stripslashes($this->header);
echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n";
echo "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>\n";
echo implode("\n", $this->lines);
echo "</Table>\n</Worksheet>\n";
echo $this->footer;
}
}
class PHPExcel
{
/**
* Header of excel document (prepended to the rows)
*/
public $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?\>
<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
/**
* Footer of excel document (appended to the rows)
*/
public $footer = "</Workbook>";
public $lines = array();
public $worksheet_title = "Table1";
/**
* Add a single row to the $document string
*/
public function addRow($array)
{
$cells = "";
foreach ($array as $k => $v)
{
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
/**continue;
if (is_numeric($v)) {
// 防止首字母为 0 时生成 excel 后 0 丢失
if (substr($v, 0, 1) == 0) {
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
} else {
$cells .= "<Cell><Data ss:Type=\"Number\">" . $v . "</Data></Cell>\n";
}
} else {
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
}*/
}
$this->lines[] = "<Row>\n" . $cells . "</Row>\n";
}
/**
* Add an array to the document
*/
public function addArray($array)
{
foreach ($array as $k => $v):
$this->addRow($v);
endforeach;
}
/**
* Set the worksheet title
* @access public
* @param string $title Designed title
*/
public function setWorksheetTitle($title)
{
// strip out special chars first
$title = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
$title = substr($title, 0, 31);
$this->worksheet_title = $title;
}
/**
* Generate the excel file
*/
public function generateXML($filename)
{
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
echo stripslashes($this->header);
echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n";
echo "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>\n";
echo implode("\n", $this->lines);
echo "</Table>\n</Worksheet>\n";
echo $this->footer;
}
}
相关文章推荐
- 从DataGrid导出Excel产生乱码的一个很好的解决方案
- 一个很好的通用 excel 导出工具类
- 一个很好的用C#导出数据到Excel模板的方法
- 从DataGrid导出Excel产生乱码的一个很好的解决方案
- 一个绝对很好的将DataTable中的数据导出到Excel文件中的类源代码
- 一个将数据导出到EXCEL的存储过程-数据库专栏,SQL Server
- delphi 的一个导出excel的方法的类
- 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)
- 分享一个导出Excel时页面不跳转的小技巧
- PHP导出Excel一个方法轻松搞定
- ThinkPHP 整合 PHPExcel ,数据导出功能实现,解决Invalid cell coordinate PHPExcel想必大家都不陌生,是用来操作Office Excel 文档的一个PHP
- SQL大圣之路笔记——把数据库中表里的数据导出成Excel(Copy时,会因分隔符问题导致许多数据存放在一个单元格中)
- 一个高效的导出Excel方法,并且已解决导出会有样式文件夹的问题
- c# linq的高级用法:分页查询和导出Excel共用一个方法
- php 导出excel 很简单很好用
- ExportToExcel 一个通用的把dataset导出至EXCEL的类
- 传入一个table,文件名,导出Excel的方法
- jeesite框架导出zip压缩包(包含一个excel文件和一张缩略图以及多个附件)
- Jxls导出excel的若干方式总结(十一)-- 同一个sheet中显示来自不同数据表的对象记录
- 转发一个通用从数据库导出excel、excel导入数据库组件