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)
上一篇 2025年12月7日 下午11:43
下一篇 2025年12月8日 上午7:22


相关推荐

  • 安卓rom包制作_android原生rom下载

    安卓rom包制作_android原生rom下载制作ROM包是做Android系统工程师的第一步,Android第三方的市场

    2022年10月16日
    5
  • 认识全角字符

    认识全角字符简单地说一个汉字就是一个全角的 它顶两个半角字符的宽窄 全角指一个字符占用两个标准字符位置 全角字符占用 2 个字节位置 汉字字符和规定了全角的英文字符及国标 GB2312 80 中的图形符号和特殊字符都是全角字符 一般的系统命令是不用全角字符的 只是在作文字处理时才会使用全角字符 半角指一字符占用一个标准的字符位置 通常的英文字母 标点符号 特殊符号等 1 全角字符与半

    2026年3月19日
    3
  • 安装与卸载tensorflow-gpu

    安装与卸载tensorflow-gpu安装到Anaconda:安装:pipinstalltensorflow-gpu==1.2.1pipuninstall tensorflow-gpu==1.2.1安装与卸载keras同样:安装:pipinstallkeras==2.0.5pipuninstallkeras=2.0.5

    2022年6月22日
    85
  • pycharm设置主题背景图片[通俗易懂]

    pycharm设置主题背景图片[通俗易懂]一、操作步骤1.双击shift出现搜索框,输入:setbackgroundimage;(或者通过设置路径:File|Settings|Appearance&Behavior|Appearance,选择BackgroundImage)2.选择你喜欢的背景图片上传;3.可通过opacity控件设置透明度image:输入背景图片路径opacity:设置透明度方框:选择图片样式…

    2022年8月25日
    14
  • windows10+nvidia驱动+cuda10.1+cudnn安装教程

    windows10+nvidia驱动+cuda10.1+cudnn安装教程一、显卡驱动提前安装好nvidia驱动,windows一般都自动安装了nvidia驱动了没有安装驱动可以去官网下载驱动https://www.geforce.cn/drivers选择自己对应的显卡驱动,默认安装就可以了。下载之前查看自己显卡驱动和cuda版本号之间的关系,如下图所示,然后进行选择性安装。https://docs.nvidia.com/cuda/cuda-to…

    2022年5月24日
    160
  • python之pandas数据筛选和csv操作

    本博主要总结DaraFrame数据筛选方法(loc,iloc,ix,at,iat),并以操作csv文件为例进行说明1.数据筛选(1)单条件筛选(2)多条件筛选可以使用&(并)与|(

    2021年12月29日
    34

发表回复

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

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