您好,欢迎访问一九零五行业门户网

php如何实现数据的导入与导出xml格式的Excel的图文代码分享

1 简介1.1 导出         在实际的工作项目中,经常需要将一些重要的数据库中存的数据导出成excel,比如导出考勤报表,导出财务报表,导出业绩报表,导出销售报表等。clevercode以前使用了两年的phpexcel来制作excel导出数据,但发现用phpexcel生成excel实在是太麻烦了,特别是控制单元格的颜色,合并单元格,给单元格设置长度等。这些设计一个excel通常会需要花费一天的时间。后来clevercode发现了一个简便的方法php导出xml格式的excel,以前需要一天的工作量,现在半小时就搞定了,实在是事半功倍!
1.2 导入       同时有的项目也需要,将一些excel数据导入到数据库中。比如银行给的银行流水,销售报表导入的数据库中。这个通常的做法都是使用phpexcel。
        虽然读取xml格式的excel可以使用xml parser, simplexml, xmlreader, domdocument等方式,但是clevercode尝试过使用这些方式,发现太复杂,太费劲,没有phpexcel好使。
       所以当需要读取excel的时候(包括xml格式的),clevercode推荐使用phpexcel库。
2 需求    某个集团需要各个地区的负责人将其负责的城市站的订单和销售额导入到数据库。
    1)网站提供一个导入的销售报表模板。
    2)每个负责人只能上传与下载各自负责城市的数据(权限检查)。
    3)只用上传生成当年,当日的拥有的所有季度。比如今天是2015-05-26。那么只有生成2015一季度,与二季度。
        如果是2015-12-01。则需要生成2015一,二,三,四季度。
    4)显示本季度以前季度的数据。
    5)本季度的数据默认全是0。
    6)只能修改本季度的数据。
3 程序设计源码下载4 设计网站页面4.1 显示
4.2 display.php代码<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gbk" /> <title>php导入与导出xml格式的excel</title> <style type="text/css"> body{ font-size:14px;} input{ vertical-align:middle; margin:0; padding:0} .file-box{ position:relative;width:340px} .txt{ height:22px; border:1px solid #cdcdcd; width:180px;} .btn{ background-color:#fff; border:1px solid #cdcdcd;height:24px; width:70px;} .file{ position:absolute; top:0; right:80px; height:24px; filter:alpha(opacity:0); opacity: 0;width:260px } </style> </head> <body> <p class="file-box"> <form action="" method="post" enctype="multipart/form-data"> <input type='text' name='textfield' id='textfield' class='txt' /> <input type='button' class='btn' value='浏览...' /> <input type="file" name="filefield" class="file" id="filefield" size="28" onchange="document.getelementbyid('textfield').value=this.value" /> <input type="submit" name="submit" class="btn" value="上传" /> </form> <a href="export.php">下载销售报表模板</a> </p> </body> </html>
5 php导出xml格式的excel(导出销售报表模板)1)新建一个【销售报表.xlsx】。设计如下。
2)将【销售报表.xlsx】文件另存为【销售报表.xml】
3)打开【销售报表.xml】可以看到xml格式的数据。
4)找到table信息。删除掉ss:expandedcolumncount="5" ss:expandedrowcount="6"。这个限制死了表格的长度和宽度,所以必须去掉。
<table ss:expandedcolumncount="5" ss:expandedrowcount="6" x:fullcolumns="1" x:fullrows="1" ss:styleid="s23" ss:defaultcolumnwidth="54" ss:defaultrowheight="18.75">
改成
<table x:fullcolumns="1" x:fullrows="1" ss:styleid="s23" ss:defaultcolumnwidth="54" ss:defaultrowheight="18.75">
5 php导出excel业务逻辑代码(excel.php)
<?php /** * excel.php * * excel操作 * * copyright (c) 2015 http://blog.csdn.net/clevercode * * modification history: * -------------------- * 2015/5/14, by clevercode, create * */ class excel{ /** * 导出excel * * @param int $userid 用户编号 * @return string $xmlstr */ public static function export($userid){ // 根据不同用户的权限,获取不同的数据 $data = self::getexportdata($personid); // 获取字符串,如果excel的列是固定的可以通过smarty方式获取 // 但是如果excel的列需要通过动态生成,则可以通过php组合字符串。 // $xmlstr = self::getxmlstrbysmarty($data); // 这个需要根据当前日期动态的生成有几个季度 $xmlstr = self::getxmlstrbyphp($data); return $xmlstr; } /** * 生成excel数据 * * @param int $userid 用户编号 * @return array 结果数据 */ public static function getexportdata($userid){ if (!is_int($userid)) { return array(); } $infobj = array( 'city' => '北京', 'order_1' => 100, 'money_1' => 10000, 'order_2' => 200, 'money_2' => 40000 ); $infotj = array( 'city' => '天津', 'order_1' => 50, 'money_1' => 1000, 'order_2' => 100, 'money_2' => 2000 ); $infogz = array( 'city' => '广州', 'order_1' => 50, 'money_1' => 1000, 'order_2' => 100, 'money_2' => 2000 ); // 根据不同用户的权限,获取不同的数据 if (is_admin($userid)) { $data[] = $infobj; $data[] = $infotj; $data[] = $infogz; } else { $data[] = $infobj; } return $data; } /** * 通过smarty方式获取xml字符串 * * @param array $data 结果集 * @return string $xmlstr */ public static function getxmlstrbysmarty($data){ require_once 'smarty.class.php'; $smarty = new smarty(); $tpl = 'file/export.tpl'; $smarty->assign('list', $data); // capture the output // 捕获输出 $xml = $smarty->fetch($tpl); return $xml; } /** * 通过php组合字符串方式获取xml字符串(可以动态扩展列) * * @param array $data 结果集 * @return string $xmlstr */ public static function getxmlstrbyphp($data){ $xml = ' <?xml version="1.0"?> <?mso-application progid="excel.sheet"?> <workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" ............ </style> </styles> <worksheet ss:name="sheet1"> <table x:fullcolumns="1" x:fullrows="1" ss:styleid="s16" ss:defaultcolumnwidth="54" ss:defaultrowheight="18.75"> '; //可以根据季度的多少动态扩展列,这里不做说明,请自行尝试。 $xml. = ' <row ss:autofitheight="0"> <cell ss:mergedown="1" ss:styleid="m42513364"><data ss:type="string">城市</data></cell> <cell ss:mergeacross="1" ss:styleid="s25"><data ss:type="string">2015一季度</data></cell> <cell ss:mergeacross="1" ss:styleid="m42513344"><data ss:type="string">2015二季度</data></cell> </row> <row ss:autofitheight="0"> <cell ss:index="2" ss:styleid="s17"><data ss:type="string">订单</data></cell> <cell ss:styleid="s17"><data ss:type="string">销售额</data></cell> <cell ss:styleid="s17"><data ss:type="string">订单</data></cell> <cell ss:styleid="s17"><data ss:type="string">销售额</data></cell> </row> '; // 输出数据 foreach ( $data as $row ) { $xml .= ' <row ss:autofitheight="0"> <cell ss:styleid="s18"><data ss:type="string">' . $row['city'] . '</data></cell> <cell ss:styleid="s19"><data ss:type="number">' . $row['order_1'] . '</data></cell> <cell ss:styleid="s19"><data ss:type="number">' . $row['money_1'] . '</data></cell> <cell ss:styleid="s19"><data ss:type="number">' . $row['order_2'] . '</data></cell> <cell ss:styleid="s19"><data ss:type="number">' . $row['money_2'] . '</data></cell> </row> '; } $xml .= ' <worksheetoptions xmlns="urn:schemas-microsoft-com:office:excel"> ........... </workbook> '; return $xml; } }
6 php导出excel客户端代码(export.php)
<?php /** * export.php * * 导出excel * * copyright (c) 2015 http://blog.csdn.net/clevercode * * modification history: * -------------------- * 2015/5/14, by clevercode, create * */ // excel类 include_once ('excel.php'); /* * 客户端类 * 让客户端和业务逻辑尽可能的分离,降低客户端和业务逻辑算法的耦合, * 使业务逻辑的算法更具有可移植性 */ class client{ public function main(){ // 获取xml格式字符串 $xmlstr = excel::export(1); // 头部 $filename = '销售报表模板'; header("content-type: application/vnd.ms-excel; charset=utf-8"); header("content-disposition: inline; filename=\"$filename.xls\""); header("content-transfer-encoding: binary"); header("pragma: public"); header("cache-control: must-revalidate, post-check=0, pre-check=0"); // 输出字符串 echo $xmlstr; exit(); } } /** * 程序入口 */ function start(){ $client = new client(); $client->main(); } start(); ?>
6 php导入xml格式的excel1)下载销售模板后,填写其中的数据,然后点击页面中的上传按钮,上传excel数据。
2) php导入的excel业务逻辑代码(excel.php)。这里使用了phpexcel库。
<?php /** * excel.php * * excel操作 * * copyright (c) 2015 http://blog.csdn.net/clevercode * * modification history: * -------------------- * 2015/5/14, by clevercode, create * */ class excel{ /* * 读取excel格式的数据(可以读取xml格式数据) * * @param string $filename excel文件 * @param string $startrow 开始行 * @param string $endrow 结束行 * @param string $startcolumn 开始列 * @param string $endcolumn 结束列 * @return array excel结果集数据 */ public static function read($filename, $startrow = 1, $endrow = null, $startcolumn = 0, $endcolumn = null){ $exceldata = array(); if (!file_exists($filename)) { return $exceldata; } require_once 'phpexcel/phpexcel.php'; require_once 'phpexcel/phpexcel/iofactory.php'; // 加载excel文件 $objphpexcel = phpexcel_iofactory::load($filename); // 获取焦点sheet $objworksheet = $objphpexcel->getactivesheet(); // 获取总行 $totalrows = $objworksheet->gethighestrow(); // 获取总行数 // 获取总列 $highestcolumn = $objworksheet->gethighestcolumn(); $totalcolumns = phpexcel_cell::columnindexfromstring($highestcolumn); // 开始行 if (!is_int($startrow) || $startrow < 1) { $startrow = 1; } // 结束行 if ($endrow == null || !is_int($endrow) || $endrow > $totalrows) { $endrow = $totalrows; } // 开始列 if (!is_int($startcolumn) || $startcolumn < 0) { $startcolumn = 0; } // 结束列 if ($endcolumn == null || !is_int($endcolumn) || $endcolumn > $totalcolumns) { $endcolumn = $totalcolumns; } // 读取数据 for($rownum = $startrow; $rownum <= $endrow; $rownum++) { for($colnum = $startcolumn; $colnum < $endcolumn; $colnum++) { $item = $objworksheet->getcellbycolumnandrow($colnum, $rownum); $exvalue = trim($item->getvalue()); $exceldata[$rownum][$colnum] = $exvalue; } } return $exceldata; } }
3) php导入的excel客户端代码(import.php)
<?php /** * import.php * * 导入excel * * copyright (c) 2015 http://blog.csdn.net/clevercode * * modification history: * -------------------- * 2015/5/14, by clevercode, create * */ // excel类 include_once ('excel.php'); /* * 客户端类 * 让客户端和业务逻辑尽可能的分离,降低客户端和业务逻辑算法的耦合, * 使业务逻辑的算法更具有可移植性 */ class client{ public function main(){ if (!$_files['file']) { exit(); } // 从第3行开始读取excel数据 $datas = excel::read($_files['file']['tmp_name'], 3); // 将$datas保存到数据库 // .... } } /** * 程序入口 */ function start(){ $client = new client(); $client->main(); } start(); ?
以上就是php如何实现数据的导入与导出xml格式的excel的图文代码分享的详细内容。
其它类似信息

推荐信息