phpexcel中文手册php实现在线excel

前言:

在业务开发中,导入导出表的功能非常常见。但是这里我们主要是用PhpOffice类库来介绍导入表数据的功能。

00-1010的导入功能大部分是通过点击按钮上传表单,然后在后台读取表单数据并根据业务直接插入数据库,最后返回前端。但是,如果表单数据庞大,业务逻辑复杂,导入的块就会臃肿,难以维护。

冲突:

的处理方法是将导入和业务数据插入分开,所以在它们之间添加一个队列就足够了。Import只负责将表数据存储在队列中。部分业务可以是单独的系统,最后是消费队列中的数据。这样不仅提高了导入速度,而且导入与系统解耦,其他业务不会因为异常而受到影响。

00-1010 1.下载PhpOffice。

作曲:repuire PHP office/PHP电子表格2。进出口代码。

?服务器端编程语言(Professional Hypertext Preprocessor的缩写)

命名空间app \ common \ helper

使用PhpOffice \ PhpSpreadsheet \电子表格;

使用PhpOffice \ PhpSpreadsheet \ Writer \ Xlsx;

使用PhPoffice \ PhPSPredssheet \ IOFactory;

使用PhpOffice \ phpspredadssheet \ Cell \ Coordinate;

使用think \ Exception

Excel类

{

//导出

公共输出$ data,$ columns,$ table=’ export file ‘)

{

$电子表格=新的电子表格);

$ sheet=$电子表格-GetActivisSheet);

//设置第一列的标题

foreach$列为$ k=$ v){ 0

$sheet-setCellValue$k . ‘1 ‘,$ v[‘ title ‘]);

}

//从第二行开始设置内容

$ BaseRow=2;//数据从N-1线向下输出。这是为了避免标题信息被覆盖。

foreach$数据为$ key=$ value){ 0

foreach$列为$ k1=$ v1){ 0

$ i=$ key $ baseRow

$sheet-setCellValue$k1。$i,$ value[$ v1[‘ field ‘]]);

}

}

$ writer=new Xlsx$电子表格);

$文件名=$table。日期’ Y-m-d ‘,时间))。’_’ .时间)。xlsx ‘;

$writer-save’。/excel/’。$ filename);

返回“/excel/”。$文件名;

}

//导入

公共函数importExcel$file=’ ‘,$sheet=0,$ columnCnt=0,$options=[])

{

尝试{

$file=iconv’utf-8 ‘,’ gb2312 ‘,$ file);

if空$file) OR!file _ exists$ file)){ 0

抛出新的\异常’文件不存在!’);

}

$ ObjRead=IOfactory : create Reader’ Xlsx ‘);

if!$ ObjRead-CanRead$ file)){ 0

$ ObjRead=IOfactory : create Reader’ Xls ‘);

if!

$objRead->canRead$file)) {
throw new \Exception’只支持导入Excel文件!’);
}
}

/* 如果不需要获取特殊操作,则只读内容,可以大幅度提升读取Excel效率 */
empty$options) && $objRead->setReadDataOnlytrue);
/* 建立excel对象 */
$obj = $objRead->load$file);

/* 获取指定的sheet表 */
$currSheet = $obj->getSheet$sheet);
//$currSheet = $obj->getSheetByName$sheet); // 根据名字

if isset$options[‘mergeCells’])) {
/* 读取合并行列 */
$options[‘mergeCells’] = $currSheet->getMergeCells);
}

if 0 == $columnCnt) {
/* 取得最大的列号 */
$columnH = $currSheet->getHighestColumn);
/* 兼容原逻辑,循环时使用的是小于等于 */
$columnCnt = Coordinate::columnIndexFromString$columnH);
}

/* 获取总行数 */
$rowCnt = $currSheet->getHighestRow);
$data = [];

/* 读取内容 */
for $_row = 1; $_row <= $rowCnt; $_row++) {
$isNull = true;

for $_column = 1; $_column <= $columnCnt; $_column++) {
$cellName = Coordinate::stringFromColumnIndex$_column);
$cellId = $cellName . $_row;
$cell = $currSheet->getCell$cellId);

if isset$options[‘format’])) {
/* 获取格式 */
$format = $cell->getStyle)->getNumberFormat)->getFormatCode);
/* 记录格式 */
$options[‘format’][$_row][$cellName] = $format;
}

if isset$options[‘formula’])) {
/* 获取公式,公式均为=号开头数据 */
$formula = $currSheet->getCell$cellId)->getValue);

if 0 === strpos$formula, ‘=’)) {
$options[‘formula’][$cellName . $_row] = $formula;
}
}

if isset$format) && ‘m/d/yyyy’ == $format) {
/* 日期格式翻转处理 */
$cell->getStyle)->getNumberFormat)->setFormatCode’yyyy/mm/dd’);
}

$data[$_row][$cellName] = trim$currSheet->getCell$cellId)->getFormattedValue));

if !empty$data[$_row][$cellName])) {
$isNull = false;
}
}

if $isNull) {
unset$data[$_row]);
}
}

return $data;
} catch \Exception $e) {
throw $e;
}
}

}

3. 抽取指定的字段格式化Excel数据。

return [

// 导入的表格标题
“bidding” => [
“stock_no” => “编号”,
“price” => “价格”,
“mobile” => “手机”,
“nickname” => “姓名”
]

];

// 格式化指定列数据默认第一行表头)
public static function formattingCellsarray $data, array $cellConfig)
{
$res = array_values$data);

// 表头
$header = $res[0];

$cellKeys = [];
foreach $header as $key => $value) {
foreach $cellConfig as $k => $v) {
if $value == $v) {
$cellKeys[$key] = $k;
}
}
}

if count$cellKeys) != count$cellConfig)) {
throw new Exception’表格不完整’);
}

// 需要添加过滤
$temp = [];
for $i = 1; $i <= count$res) – 1; $i++) {
foreach $cellKeys as $m => $n) {
$temp[$i][$n] = $res[$i][$m];
}
}

return array_values$temp);
}

4. 导入部分,上传接口。

// 导入表格,上传接口
public function importExcel)
{
$upload_file = $_FILES[‘files’][‘tmp_name’];
$input = $this->input;

// ID
$id = isset$input[‘id’]) ? $input[‘id’] : 0;

// 默认取第一工作表
$excelData = new Excel))->importExcel$upload_file, 0);

// 取Excel字段
$config = config’excel_export.bidding’);

$price_offer = Excel::formattingCells$excelData, $config);

// 判断每条记录的手机和价格格式
// ……

$jsonList = json_encodecompact’id’, ‘price_offer’));
//$jsonList = json_encode$price_offer);

// 入MQ
$host = config”mq.host”);
$options = config”mq.price_offer_import”);

try {
$mq = new ProductMQ$host, $options);

$mq->publish$jsonList);

$mq->close);

} catch \Exception $e) {
return $this->jsonData200, $e->getMessage));
}
// 入MQ

return $this->jsonData200, ‘导入成功’);
}

5. 消费业务逻辑。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注