php代码
导出```
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('PRC');
/** 引入PHPExcel */
require_once dirname(FILE) . './Classes/PHPExcel.php';
// 创建Excel文件对象
objPHPExcel->getProperties()->setCreator("作者简庆旺")
->setLastModifiedBy("最后更改者")
->setTitle("文档标题")
->setSubject("文档主题")
->setDescription("文档的描述信息")
->setKeywords("设置文档关键词")
->setCategory("设置文档的分类");
//根据excel坐标,添加数据
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '你好')
->setCellValue('B2', '世界')
->setCellValue('C1', '你好')
->setCellValue('D2', '世界');
// 混杂各种符号, 编码为UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true);
objPHPExcel->getActiveSheet()->setCellValue('A10', objPHPExcel->getActiveSheet()->getRowDimension(10)->setRowHeight(-1);
objPHPExcel->getActiveSheet()->getStyle('A10')->setQuotePrefix(true);
// 重命名工作sheet
$objPHPExcel->getActiveSheet()->setTitle('第一个sheet');
// 设置第一个sheet为工作的sheet
$objPHPExcel->setActiveSheetIndex(0);
// 保存Excel 2007格式文件,保存路径为当前路径,名字为export.xlsx
objPHPExcel, 'Excel2007');
$objWriter->save( 'export.xlsx');
// 保存Excel 95格式文件,,保存路径为当前路径,
objPHPExcel, 'Excel5');
$objWriter->save('export.xls');
phpexcel```
public function doWebHaomadaoru() {
//echo __FILE__;die;/www/wwwroot/xyka.sdlrwl.com/addons/lr_txwzjhzs/site.php
$PHPExcel = dirname(__FILE__).'/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
$xlsdir = dirname(__FILE__);
require_once $PHPExcel;
$inputFileName = $xlsdir.'/test.xls';
date_default_timezone_set('PRC');
// 读取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
echo "请确认模块根目录下存在test.xls文件!<br>";
die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// 获取一行的数据(从一开始,一是表头)
for ($row = 2; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
//这里得到的rowData都是一行的数据,得到数据后自行处理,我们这里只打出来看看效果
var_dump($rowData);
echo "<br>";
// array(1) { [0]=> array(7) { [0]=> string(14) "问题(必填)" [1]=> string(15) "选项A(必填)" [2]=> string(15) "选项B(必填)" [3]=> string(15) "选项C(必填)" [4]=> string(15) "选项D(必填)" [5]=> string(14) "答案(必填)" [6]=> string(18) "这行请勿删除" } }
// array(1) { [0]=> array(7) { [0]=> string(18) "哪个肉好吃?" [1]=> string(6) "天鹅" [2]=> string(9) "娃娃鱼" [3]=> string(6) "孔雀" [4]=> string(9) "丹顶鹤" [5]=> string(1) "A" [6]=> NULL } }
// array(1) { [0]=> array(7) { [0]=> string(18) "哪个好点着?" [1]=> string(6) "酒精" [2]=> string(6) "煤气" [3]=> string(6) "汽油" [4]=> string(6) "柴油" [5]=> string(1) "B" [6]=> NULL } }
}
include $this->template('index');
}
========================================================
找不到字段,可能是将要写入的表弄错了
public function doMobileRegister()
{
global $_W,$_GPC;
if($_W['ispost'])
{
if(empty($_GPC['username']) || empty($_GPC['userid']) || empty($_GPC['phonenumber']) || empty($_GPC['selectnumber']) )
{
message(error(1,"输入不能为空"), '', 'ajax');
die();
}
$formdata['myno'] = date('Ymdhis',time()).rand(100,999);
// var_dump($formdata['order_no']);
// die();
$formdata['username'] = $_GPC['username'];
$formdata['userid'] = $_GPC['userid'];
$formdata['phonenumber'] = $_GPC['phonenumber'];
$formdata['selectednumber'] = $_GPC['selectnumber'];
$res = pdo_insert("lr_order", $formdata, $replace = false);
var_dump($res);die;
if (!empty($res)) {
$uid = pdo_insertid();
message('添加用户成功,UID为' . $uid);
die();
}
//message(error(0,$formdata), '', 'ajax');
die('nothing');
}
else
{
include $this->template('register');
}
}
转载于:https://www.jianshu.com/p/555e5115d029