I am reading from a file, upon certain condition I want to insert rows into a Excel file. This is to be done on a regular basis. The current will be replaced with old one.

You could write it to a .csv file? This is a file that excel can read where all values are delimited by comma's (or semicolons). You can write to such a file in the way you would write to a regular textfile.

Do you read data from database?

Do you read data from database?

I am reading data from url, and want to insert a single field into excel or csv.

This the code that I used in one application for reading data from db and write into excel sheet.

$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();

Just replace the database part code with your requirement.

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.