doushu0591 2015-10-03 15:15
浏览 455
已采纳

PHP_XLSXWriter问题

I am using PHP_XLSXWriter.
I want to do the following expected output:

enter image description here

Here is my code:

<?php
include_once('./PHP_XLSXWriter-master/xlsxwriter.class.php');

$writer = new XLSXWriter();
$sheet_name = 'Sheet1';

$header = array("A", "B", "C");

$writer->writeSheetRow($sheet_name, $header);

$writer->markMergedCell($sheet_name, $start_row = ?, $start_col = ?, $end_row = ?, $end_col = ?);

$writer->writeToFile('test.xlsx');
?>

How should control ? in my code?

  • 写回答

1条回答 默认 最新

  • duandang6352 2015-10-03 16:51
    关注

    There can be better options to do this, but I could do only this kind of a workaround. It may be helpful for you.

    <?php
    include_once("xlsxwriter.class.php"); //include
    $writer = new XLSXWriter();     //new writer
    $sheet_name = 'sheets';   //sheetname
    $header = array("string","string","string","string","string","string");  // header-made for six columns
    $row1 = array("A"," ","B"," ","C"); //column B1, D1 made blank to match formatting
    $writer->writeSheetHeader($sheet_name, $header, $suppress_header_row = true);   //write header
    $writer->markMergedCell($sheet_name, $start_row = 0, $start_col = 0, $end_row = 1, $end_col = 1);  //merge cells for A
    $writer->markMergedCell($sheet_name, $start_row = 0, $start_col = 2, $end_row = 0, $end_col = 3);  //merge cells for B
    $writer->markMergedCell($sheet_name, $start_row = 0, $start_col = 4, $end_row = 0, $end_col = 5);  //merge cells for C
    $writer->writeSheetRow($sheet_name, $row1);   //write data
    $writer->writeToFile('test.xlsx');  //save file
    ?>
    

    Edit: If you need looping for repeating same style of merging, do like this

    <?php
    include_once("xlsxwriter.class.php"); //include
    $writer = new XLSXWriter();     //new writer
    $sheet_name = 'sheets';   //sheetname
    $header = array("string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string");  // header-made for 19 columns
    $row1 = array("A"," ","B"," ","C"," ","D"," ","E"," ","F"," ","G"," ","H"," ","I"," ","J"); //column B1, D1, F1, H1, J1, L1, N1, P1, R1 made blank to match formatting
    $writer->writeSheetHeader($sheet_name, $header, $suppress_header_row = true);   //write header
    $writer->markMergedCell($sheet_name, $start_row = 0, $start_col = 0, $end_row = 1, $end_col = 1);  //merge cells for A
    $j = 2; //start column
    for ($i=0; $i<9; $i++)
    {
    $writer->markMergedCell($sheet_name, $start_row = 0, $start_col = $j, $end_row = 0, $end_col = $j+1);  //merge cells
    $j+=2;
    }
    $writer->writeSheetRow($sheet_name, $row1);   //write data
    $writer->writeToFile('test.xlsx');  //save file
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?