Can somebody help me please?
i try to generate an xls(excel) file using this code, all format work fine except the
cell size(width).
can anyone help me how to format the cell width?

</php
.
.
.
$workbook = new Spreadsheet_Excel_Writer();

$format_und =& $workbook->addFormat();

$format_und->setBottom(1);//thick

$format_und->setAlign('center');

$format_und->setHAlign('equal_space');

$format_und->setBold();

$format_und->setVAlign('vcentre');

$format_und->setFgColor('silver');

$format_und->setColor('black');

$format_und->setFontFamily('Arial');

$format_und->setSize(8);



$format_cell =& $workbook->addFormat();

$format_cell->setBottom(1);//thick

$format_cell->setAlign('center');

$format_cell->setHAlign('equal_space');

$format_cell->setVAlign('vcentre');

$format_cell->setPattern(5);

$format_cell->setFgColor('blue');

$format_cell->setFontFamily('Arial');

$format_cell->setSize(8);



$format_reg =& $workbook->addFormat();

$format_reg->setColor('grey');

$format_reg->setFontFamily('Arial');

$format_reg->setSize(8);



$arr = array(

      'test'=>$sheet1,

      'Names'   =>$sheet2,

      );



foreach($arr as $wbname=>$rows)

{

    $rowcount = count($rows);

    $colcount = count($rows[0]);



    $worksheet =& $workbook->addWorksheet($wbname);



    $worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)

    $worksheet->setColumn(1,3,15.00);

    $worksheet->setColumn(4,4, 8.00);

    

    for( $j=0; $j<$rowcount; $j++ )

    {

        for($i=0; $i<$colcount;$i++)

        {

            $fmt  =& $format_reg;

            if ($j==0)

                $fmt =& $format_und;



            if (isset($rows[$j][$i]))

            {

				$col_arr = array(3,4,5,8,9);

				foreach($col_arr as $ca){

				if($i==$ca && $j==0)

					$fmt =& $format_cell;

				}

				

                $data=$rows[$j][$i];

                $worksheet->write($j, $i, $data, $fmt);

            }

        }

    }

}



$workbook->send('test.xls');

$workbook->close();

?>

thank you in advance.
Sorry for my bad english

Recommended Answers

All 2 Replies

I used the following code to generate xls file from php code. I just share the code here.

$result=mysql_query("select * from tbl_sms order by id asc");
		function xlsBOF() 
		{
			echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
			return;
		}
		function xlsEOF() 
		{
			echo pack("ss", 0x0A, 0x00);
			return;
		}
		function xlsWriteNumber($Row, $Col, $Value) 
		{
			echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
			echo pack("d", $Value);
			return;
		}
		function xlsWriteLabel($Row, $Col, $Value ) 
		{
			$L = strlen($Value);
			echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
			echo $Value;
			return;
		}
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: application/force-download");
		header("Content-Type: application/octet-stream");
		header("Content-Type: application/download");;
		header("Content-Disposition: attachment;filename="."smslist_".date('Y-m-d_H-i',time()).".xls");
		header("Content-Transfer-Encoding: binary ");
		xlsBOF();
		
		xlsWriteLabel(0,0,"No.");
		xlsWriteLabel(0,1,"Name");
		xlsWriteLabel(0,2,"Mobile Number");
		$xlsRow = 1;
		while($row=mysql_fetch_array($result))
		{
			xlsWriteNumber($xlsRow,0,$row['id']);
			xlsWriteLabel($xlsRow,1,$row['name']);
			xlsWriteLabel($xlsRow,2,$row['mobile']);
			$xlsRow++;
		}
		xlsEOF();
		exit();

thanks karthik. I already used that code but i dont how to format the cell use that code.

I solve the problem with setColumn(), Thank for replay..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.