PhpSpreadsheet_php file_put_contents

PhpSpreadsheet_php file_put_contentsphp用PhpSpreadsheet对Excel进行读取、写入、修改十分便捷,下面将我在工作中用到过的操作进行总结。1、读取Excel:见文章PhpSpreadsheet读取单元格内容的坑2、修改Excel:示例代码(只保留了基础代码):$this->view->title=’填写Excel’;$abs_excel_path=$_SERVER[‘DOCUMENT_ROO…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

php用PhpSpreadsheet对Excel进行读取、写入、修改十分便捷,下面将我在工作中用到过的操作进行总结。文档参考百度搜索和原始文档:https://phpspreadsheet.readthedocs.io/en/latest/(只有英文版)
首先要引入php模块: composer require phpoffice/phpspreadsheet
1、读取Excel:见文章PhpSpreadsheet读取单元格内容的坑
2、修改Excel:示例代码(只保留了基础代码):

$this->view->title = '填写Excel';
$abs_excel_path = $_SERVER['DOCUMENT_ROOT'] . '/data_be_writen.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($abs_excel_path);
$count = 0;
// 三个sheet都进行修改
for ($sheetIndex=0; $sheetIndex<3; $sheetIndex++) { 
   
    $worksheet = $spreadsheet->getSheet($sheetIndex);
    $highestRow = $worksheet->getHighestRow(); // 总行数1, 2,3
    for ($rowIndex = 1; $rowIndex <= $highestRow; $rowIndex++) { 
   
    	$count++;
    	// 写入的数据只是个示例,具体项目中根据需求获取数值写入
        $worksheet->getCellByColumnAndRow(1, $rowIndex)->setValue($rowIndex);
        $worksheet->getCellByColumnAndRow(2, $rowIndex)->setValue($rowIndex * 10);
    }
}
echo "填写了${count}条数据";
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($abs_excel_path);

3、写入Excel,下面的代码提现了一个导出账号的过程,是基于ThinkPHP5.0的:

$head = ['部门', '姓名', '职位', '手机号', '邮箱'];
$count = count($head);

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

$worksheet->getColumnDimension('A')->setWidth(20);
$worksheet->getColumnDimension('B')->setWidth(12);
$worksheet->getColumnDimension('C')->setWidth(12);
$worksheet->getColumnDimension('D')->setWidth(18);
$worksheet->getColumnDimension('E')->setWidth(20);

$rowIndex = 1;
foreach ($head as $colIndex => $title) { 
   
    $worksheet->getCellByColumnAndRow($colIndex + 1, $rowIndex)->setValue($title);
}

$admins = $this->order('id asc')->select();
foreach ($admins as $admin) { 
   
    $rowIndex++;
    $worksheet->getCellByColumnAndRow(1, $rowIndex)->setValue(model('Department')->get_name_by_id($admin['department_id']));
    $worksheet->getCellByColumnAndRow(2, $rowIndex)->setValue($admin['username']);
    $worksheet->getCellByColumnAndRow(3, $rowIndex)->setValue($admin['position']);
    $worksheet->getCellByColumnAndRow(4, $rowIndex)->setValue($admin['phone']);
    $worksheet->getCellByColumnAndRow(5, $rowIndex)->setValue($admin['email']);
}

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="账号.xlsx"');
header('Cache-Control: max-age=0');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('php://output');

//删除清空:
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;

4、修改单元格样式的,代码进行了精简,具体要标记哪个,根据项目实际判断:

$this->view->title = '填写Excel';
$abs_excel_path = $_SERVER['DOCUMENT_ROOT'] . '/2.xlsx';
$styleArray = [
    'borders' => [
        'outline' => [
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => ['argb' => 'FFFF0000'],
        ],
    ],
];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($abs_excel_path);
$count = 0;
for ($sheetIndex=0; $sheetIndex<3; $sheetIndex++) { 
   
    $worksheet = $spreadsheet->getSheet($sheetIndex);
    $highestRow = $worksheet->getHighestRow(); // 总行数1, 2,3
    $excel_data = new \app\admin\model\ExcelData2;
    for ($rowIndex = 2; $rowIndex <= $highestRow; $rowIndex++) { 
   
        $worksheet->getStyle("B{ 
     $rowIndex}")->applyFromArray($styleArray);
    }
}
echo "发现了${count}条数据";
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($_SERVER['DOCUMENT_ROOT'] . '/2标记.xlsx');

注意:对比2、4可以发现:读取了Excel以后,如果要改写,只需要两行代码,在save的时候填写的绝对路径跟读取的一样,则为改写,如果不同,则为新建。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192799.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python批量修改文件后缀名_python程序的文件扩展名为

    python批量修改文件后缀名_python程序的文件扩展名为importosdirname=”C:\\Users\\admin\\Desktop\\新建文件夹”#文件所在的目录li=os.listdir(dirname)#将所有图片名放入到lil列表中foriinrange(len(li)):qianzui=os.path.splitext(li[i])[0]#将文件名和扩展名分隔开houzui=os.path.splitext(li[i])[1]print(qianzui)if.

    2022年9月22日
    0
  • 《渔歌子》之景

    《渔歌子》之景

    2021年7月30日
    75
  • .net的ValidateRequest 属性

    .net的ValidateRequest 属性ValidateRequest属性转载 2009年10月17日12:44:00标签:html /asp.net /正则表达式 /设计模式 /公告 /c#1220               在ASP.NET1.1中,@Page指令上的ValidateRequest属性被打开后,将检查以确定用户没有在查询字符串、Cooki

    2022年5月29日
    27
  • QtreeWidget_遍历qtreewidget

    QtreeWidget_遍历qtreewidget设置右键菜单并实现添加一个子项删除一个子项的功能这样有两个缺点1.只能添加特定的子项。2.不能实现不同层级节点的不同菜单。dialog.cppwidget.cppwidget.h还可以通过TYPE属性来确定每一个节点的层级。代码实现mainwindow.cppmainwindow.hdialog.cppdialog.h参考博客…

    2022年10月1日
    2
  • 将SOP进行到底 (转载)

    将SOP进行到底 (转载)将SOP进行到底(转载)前段时期,为了强化公司中高层经理人员的“职业经理人”意识和提升大家的“职业经理人”能力,公司组织了中高层经理人员进行学习余世维先生“职业经理人”讲座的演讲交流活动,并组织相关经理人员参加了余世维先生在南京丁山香格里拉举行的“经理人常犯的11种错误”

    2022年5月26日
    30
  • docker dockerfile详解_docker exec 进入容器

    docker dockerfile详解_docker exec 进入容器前言Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。Dockerfile简介Dockerfile是用来构建Docker镜像的构建文件,是由一系列

    2022年7月28日
    4

发表回复

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

关注全栈程序员社区公众号