php 将ecxel里面的内容导出成图片

1 篇文章 0 订阅

php 将ecxel里面的内容导出成图片

	//	$params
    public function create_table()
    {
        $data = array();//生成演示数据
        for ($i = 0; $i < 30; $i++) {
            $data[] = [
                'nice_name' => '测试名' . $i,
                'zhuang' => 100,
                'xian' => 50,
                'he' => 50,
                'zhuang_dui' => 500,
                'xian_dui' => 5000,
            ];
        }

        $params = [
            'row' => count($data)+1,//数据的行数
            'file_name' => 'xuju.png',
            'title' => '数据核对表',
            'table_time' => '2018-4-29 22:50:43',
            'data' => $data
        ];
        $base = [
            'border' => 10,//图片外边框
            'file_path' => 'public/create_image/check/12/',//图片保存路径
            'title_height' => 30,//报表名称高度
            'title_font_size' =>16,//报表名称字体大小
            'font_ulr' => realpath('./default/conmon/font/simkai.ttf'),//字体文件路径
            'text_size' => 12,//正文字体大小
            'row_hight' => 30,//每行数据行高
            'filed_id_width' => 60,//序号列的宽度
            'filed_name_width' => 120,//玩家名称的宽度
            'filed_data_width' => 100,//数据列的宽度
            'table_header' => ['序号', '昵称', '数据1', '数据2', '数据3', '数据4', '数据5'],//表头文字
            'column_text_offset_arr' => [45, 70, 70, 55, 55, 65, 65],//表头文字左偏移量
            'row_text_offset_arr' => [50, 110, 90, 90, 90, 90, 90],//数据列文字左偏移量
        ];
        $base['img_width'] = $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5 + $base['border'] * 2;//图片宽度
        $base['img_height'] = $params['row'] * $base['row_hight'] + $base['border'] * 2 + $base['title_height'];//图片高度
        $border_top = $base['border'] + $base['title_height'];//表格顶部高度
        $border_bottom = $base['img_height'] - $base['border'];//表格底部高度
        $base['column_x_arr'] = [
            $base['border'] + $base['filed_id_width'],//第一列边框线x轴像素  70/2+10
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'],//第二列边框线x轴像素  190
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 1,//第三列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 2,//第四列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 3,//第五列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 4,//第五列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5,//第五列边框线x轴像素
        ];
        echo  "<pre>";
        var_dump( $base['column_x_arr']);
        $img = imagecreatetruecolor($base['img_width'], $base['img_height']);//创建指定尺寸图片
        $bg_color = imagecolorallocate($img, 255, 255, 255);//设定图片背景色
        $text_coler = imagecolorallocate($img, 0, 0, 0);//设定文字颜色
        $border_coler = imagecolorallocate($img, 0, 0, 0);//设定边框颜色
//        $white_coler = imagecolorallocate($img, 255, 255, 255);//设定边框颜色
        imagefill($img, 0, 0, $bg_color);//填充图片背景色
        //先填充一个黑色的大块背景
        imagefilledrectangle($img, $base['border'], $base['border'] + $base['title_height'], $base['img_width'] - $base['border'], $base['img_height'] - $base['border'], $border_coler);//画矩形
        //再填充一个小两个像素的 背景色区域,形成一个两个像素的外边框
        imagefilledrectangle($img, $base['border'] + 2, $base['border'] + $base['title_height'] + 2, $base['img_width'] - $base['border'] - 2, $base['img_height'] - $base['border'] - 2, $bg_color);//画矩形
        //画表格纵线 及 写入表头文字
        foreach ($base['column_x_arr'] as $key => $x) {
            imageline($img, $x, $border_top, $x, $border_bottom, $border_coler);//画纵线
            $x =  $x - $base['column_text_offset_arr'][$key] + 1;
            echo  $x;echo  "<br>";
            $y = $border_top + $base['row_hight'] - 8;
            $text = $base['table_header'][$key];
            imagettftext($img, $base['text_size'], 0, $x,$y , $text_coler, $base['font_ulr'],$text );//写入表头文字
        }
        //画表格横线
        foreach ($params['data'] as $key => $item) {
            $border_top += $base['row_hight'];
            imageline($img, $base['border'], $border_top, $base['img_width'] - $base['border'], $border_top, $border_coler);
            $x =   $base['column_x_arr'][0] - $base['row_text_offset_arr'][0];
            $y = $border_top + $base['row_hight'] - 10;
            $text =   $key + 1;
            imagettftext($img, $base['text_size'], 0, $x, $y, $text_coler, $base['font_ulr'], $text);//写入序号
            $sub = 0;
            foreach ($item as $value) {
                $sub++;
                imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][$sub] - $base['row_text_offset_arr'][$sub], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $value);//写入data数据
            }
        }
        //计算标题写入起始位置
        $title_fout_box = imagettfbbox($base['title_font_size'], 0, $base['font_ulr'], $params['title']);//imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:

        $title_fout_width = $title_fout_box[2] - $title_fout_box[0];//右下角 X 位置 - 左下角 X 位置 为文字宽度
        $title_fout_height = $title_fout_box[1] - $title_fout_box[7];//左下角 Y 位置- 左上角 Y 位置 为文字高度
        //居中写入标题
        imagettftext($img, $base['title_font_size'], 0, ($base['img_width'] - $title_fout_width) / 2, $base['title_height'], $text_coler, $base['font_ulr'], $params['title']);
        //写入制表时间
        imagettftext($img, $base['text_size'], 0, $base['border'], $base['title_height'], $text_coler, $base['font_ulr'], '时间:' . $params['table_time']);
        $save_path = $base['file_path'] . $params['file_name'];
        if (!is_dir($base['file_path']))//判断存储路径是否存在,不存在则创建
        {
            mkdir($base['file_path'], 0777, true);//可创建多级目录
        }
        imagepng($img, $save_path);//输出图片,输出png使用imagepng方法,输出gif使用imagegif方法

        echo '<img src="/' . $save_path . '"/>';
    }//	$params
    public function create_table()
    {
        $data = array();//生成演示数据
        for ($i = 0; $i < 30; $i++) {
            $data[] = [
                'nice_name' => '测试名' . $i,
                'zhuang' => 100,
                'xian' => 50,
                'he' => 50,
                'zhuang_dui' => 500,
                'xian_dui' => 5000,
            ];
        }

        $params = [
            'row' => count($data)+1,//数据的行数
            'file_name' => 'xuju.png',
            'title' => '数据核对表',
            'table_time' => '2018-4-29 22:50:43',
            'data' => $data
        ];
        $base = [
            'border' => 10,//图片外边框
            'file_path' => 'public/create_image/check/12/',//图片保存路径
            'title_height' => 30,//报表名称高度
            'title_font_size' =>16,//报表名称字体大小
            'font_ulr' => realpath('./default/conmon/font/simkai.ttf'),//字体文件路径
            'text_size' => 12,//正文字体大小
            'row_hight' => 30,//每行数据行高
            'filed_id_width' => 60,//序号列的宽度
            'filed_name_width' => 120,//玩家名称的宽度
            'filed_data_width' => 100,//数据列的宽度
            'table_header' => ['序号', '昵称', '数据1', '数据2', '数据3', '数据4', '数据5'],//表头文字
            'column_text_offset_arr' => [45, 70, 70, 55, 55, 65, 65],//表头文字左偏移量
            'row_text_offset_arr' => [50, 110, 90, 90, 90, 90, 90],//数据列文字左偏移量
        ];
        $base['img_width'] = $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5 + $base['border'] * 2;//图片宽度
        $base['img_height'] = $params['row'] * $base['row_hight'] + $base['border'] * 2 + $base['title_height'];//图片高度
        $border_top = $base['border'] + $base['title_height'];//表格顶部高度
        $border_bottom = $base['img_height'] - $base['border'];//表格底部高度
        $base['column_x_arr'] = [
            $base['border'] + $base['filed_id_width'],//第一列边框线x轴像素  70/2+10
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'],//第二列边框线x轴像素  190
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 1,//第三列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 2,//第四列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 3,//第五列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 4,//第五列边框线x轴像素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5,//第五列边框线x轴像素
        ];
        echo  "<pre>";
        var_dump( $base['column_x_arr']);
        $img = imagecreatetruecolor($base['img_width'], $base['img_height']);//创建指定尺寸图片
        $bg_color = imagecolorallocate($img, 255, 255, 255);//设定图片背景色
        $text_coler = imagecolorallocate($img, 0, 0, 0);//设定文字颜色
        $border_coler = imagecolorallocate($img, 0, 0, 0);//设定边框颜色
//        $white_coler = imagecolorallocate($img, 255, 255, 255);//设定边框颜色
        imagefill($img, 0, 0, $bg_color);//填充图片背景色
        //先填充一个黑色的大块背景
        imagefilledrectangle($img, $base['border'], $base['border'] + $base['title_height'], $base['img_width'] - $base['border'], $base['img_height'] - $base['border'], $border_coler);//画矩形
        //再填充一个小两个像素的 背景色区域,形成一个两个像素的外边框
        imagefilledrectangle($img, $base['border'] + 2, $base['border'] + $base['title_height'] + 2, $base['img_width'] - $base['border'] - 2, $base['img_height'] - $base['border'] - 2, $bg_color);//画矩形
        //画表格纵线 及 写入表头文字
        foreach ($base['column_x_arr'] as $key => $x) {
            imageline($img, $x, $border_top, $x, $border_bottom, $border_coler);//画纵线
            $x =  $x - $base['column_text_offset_arr'][$key] + 1;
            echo  $x;echo  "<br>";
            $y = $border_top + $base['row_hight'] - 8;
            $text = $base['table_header'][$key];
            imagettftext($img, $base['text_size'], 0, $x,$y , $text_coler, $base['font_ulr'],$text );//写入表头文字
        }
        //画表格横线
        foreach ($params['data'] as $key => $item) {
            $border_top += $base['row_hight'];
            imageline($img, $base['border'], $border_top, $base['img_width'] - $base['border'], $border_top, $border_coler);
            $x =   $base['column_x_arr'][0] - $base['row_text_offset_arr'][0];
            $y = $border_top + $base['row_hight'] - 10;
            $text =   $key + 1;
            imagettftext($img, $base['text_size'], 0, $x, $y, $text_coler, $base['font_ulr'], $text);//写入序号
            $sub = 0;
            foreach ($item as $value) {
                $sub++;
                imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][$sub] - $base['row_text_offset_arr'][$sub], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $value);//写入data数据
            }
        }
        //计算标题写入起始位置
        $title_fout_box = imagettfbbox($base['title_font_size'], 0, $base['font_ulr'], $params['title']);//imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:

        $title_fout_width = $title_fout_box[2] - $title_fout_box[0];//右下角 X 位置 - 左下角 X 位置 为文字宽度
        $title_fout_height = $title_fout_box[1] - $title_fout_box[7];//左下角 Y 位置- 左上角 Y 位置 为文字高度
        //居中写入标题
        imagettftext($img, $base['title_font_size'], 0, ($base['img_width'] - $title_fout_width) / 2, $base['title_height'], $text_coler, $base['font_ulr'], $params['title']);
        //写入制表时间
        imagettftext($img, $base['text_size'], 0, $base['border'], $base['title_height'], $text_coler, $base['font_ulr'], '时间:' . $params['table_time']);
        $save_path = $base['file_path'] . $params['file_name'];
        if (!is_dir($base['file_path']))//判断存储路径是否存在,不存在则创建
        {
            mkdir($base['file_path'], 0777, true);//可创建多级目录
        }
        imagepng($img, $save_path);//输出图片,输出png使用imagepng方法,输出gif使用imagegif方法

        echo '<img src="/' . $save_path . '"/>';
    }
    ```
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP 可以使用函数来导出 Excel 文件。你可以使用 PHPExcel 库来实现这一点。 你也可以使用 PHP 的 fputcsv 函数来手动创建 CSV 文件,然后将 CSV 文件导入到 Excel 中。 这是一个使用 PHPExcel 库导出 Excel 文件的例子: ``` <?php // 引入 PHPExcel 库文件 include 'PHPExcel.php'; // 创建 Excel 文件 $excel = new PHPExcel(); // 设置 Excel 文件属性 $excel->getProperties() ->setCreator("John Doe") ->setLastModifiedBy("John Doe") ->setTitle("Sample Excel File") ->setSubject("Sample Excel File") ->setDescription("Sample Excel File") ->setKeywords("office 2007 openxml") ->setCategory("Sample File"); // 在第一行添加标题 $excel->setActiveSheetIndex(0) ->setCellValue('A1', 'ID') ->setCellValue('B1', 'Name') ->setCellValue('C1', 'Email'); // 在下面的行中添加数据 $excel->setActiveSheetIndex(0) ->setCellValue('A2', 1) ->setCellValue('B2', 'John Doe') ->setCellValue('C2', 'john@example.com'); // 设置工作表名称 $excel->getActiveSheet()->setTitle('Sample Sheet'); // 设置第一个工作表为活动工作表 $excel->setActiveSheetIndex(0); // 将 Excel 文件保存到本地 $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save('excel.xlsx'); ?> ``` 这是使用 fputcsv 函数导出 CSV 文件的例子: ``` <?php // 设置标题行 $header = array('ID', 'Name', 'Email'); // 打开文件句柄 $fp = fopen('csv.csv', 'w'); // 写入标题行 fputcsv($fp, $header); // 写入数据行 $data = array(

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值