Jump to content

codeigniter export to excel


homer.favenir

Recommended Posts

thanks

i already make it working,

but i have one problem left.

when my page uploads it export to excel directly without prompting.

i need a link or button like "export to excel".

 

how can i make an export to excel button?

 

<?php 
if ($excel == "excel"){header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=yourfilename.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0");}  // this is my export to excel 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title><?php echo $batch[8] ?></title>
</head>
<?php
$kebase = $batch[0];
$qcbase = $batch[1];
	$dir =	$batch[2];
$kedir = $batch[3];
$qcdir = $batch[4] ;
$kefiles = $batch[5];
$qcfiles = $batch[6];
$count_files = $batch[7] - 2;
?>
<?php
echo $count_files . " " . "files found" . " " . "in" . " " . $dir . " " . "directory" . "<br>";
?>

<form action=<?php echo $_SERVER['PHP_SELF']; ?> method="POST"> // this is my button export to excel
<input name="excel" type="hidden" value="excel" />
<input name="submit" type="submit" value="export to excel" />
</form>

<br>
<?php echo anchor('Front_cont/index', 'Home'); ?>
<pre>
<table border="1" width="0" cellpadding="0" cellspacing="5">
<th>Batch/Line</th>
<th>Field Image</th>
<th>Minor Error</th>
<th>Major Error</th>
<tr>
<?php
for($i = 2; $i <= $count_files; $i++)
{
	if(isset($qcfiles[$i]))
		{ ?>
       <?php echo "<tr><td>"; ?><h4><?php	echo $qcfiles[$i];?></h4><?php echo "</td></tr>"; ?>
	<?php	$file1 = file($kedir . "/" . $kefiles[$i]);
			$file2 = file($qcdir . "/" . $qcfiles[$i]);
			$file1_count = count($file1);
			$file2_count = count($file2);
			$count = 1;
			for($x = 0; $x <= $file1_count; $x++)
			{
			if(isset($file1[$x]) && isset($file2[$x]))
				{
					$vtag1 = substr($file1[$x],178,1);
					$vtag2 = substr($file2[$x],178,1);
					$page1 = substr($file1[$x],0,4);
					$page2 = substr($file2[$x],0,4);
					$type1 = substr($file1[$x],5,1);
					$type2 = substr($file2[$x],5,1);
					$name1 = substr($file1[$x],7,78);
					$name2 = substr($file2[$x],7,78);
					$ind1 = substr($file1[$x],0,1);
					$ind2 = substr($file2[$x],0,1);						
					$addr1 = substr($file1[$x], 1, 78);
					$addr2 = substr($file2[$x], 1, 78);
					$city1 = substr($file1[$x], 79, 30);
					$city2 = substr($file2[$x], 79, 30);
					$state1 = substr($file1[$x], 110, 2);
					$state2 = substr($file2[$x], 110, 2);
					$zc1 = substr($file1[$x], 112, 5);
					$zc2 = substr($file2[$x], 112, 5);
					$tf1 = substr($file1[$x], 123, 40);
					$tf2 = substr($file2[$x], 123, 40);
					$ac1 = substr($file1[$x], 164, 3);
					$ac2 = substr($file2[$x], 164, 3);
					$phone1  = substr($file1[$x], 168, ;
					$phone2  = substr($file2[$x], 168, ;
					if($vtag2 == 1)
			 		{ 

						$page_comp = strcmp($page1, $page2);
						$type_comp = strcmp($type1, $type2);
						$name_comp = strcmp($name1, $name2);
						if($page_comp != 0)
							{
								$err_stat = 1;
								$error = TRUE;
								$err_line = $count;
								$err_page1 = $page1;
								$err_page2 = $page2;?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE Page: " . " " . $err_page1 . "<br>" . "QC Page: " . " " . $err_page2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?> 
  						<?php	}
						if($type_comp != 0)
							{
								$err_stat = 1;
								$error = TRUE;
								$err_line = $count;
								$err_type1 = $type1;
								$err_type2 = $type2;?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE List Type: " . " " . $err_type1 . "<br>" . "QC List Type: " . " " . $err_type2; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?> 
					<?php	}
						if($name_comp != 0)
							{
								$err_stat = 1;
								$error = TRUE;
								$err_line = $count . "\t";
								$err_name1 = $name1 . "\n";
								$err_name2 = $name2; 
								?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE Name: " . " " . $err_name1 . "<br>" . "QC Name: " . " " . $err_name2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>
  							<?php
							if(isset($err_name1)){
							fwrite($fp, $err_line);
							fwrite($fp, $err_name1);
						?>	
  						<?php }

					}else{ 
							$ind_comp = strcmp($ind1, $ind2);
							$addr_comp = strcmp($addr1, $addr2);
							$city_comp = strcmp($city1, $city2);
							$state_comp = strcmp($state1, $state2);
							$zc_comp = strcmp($zc1, $zc2);
							$tf_comp = strcmp($tf1, $tf2);
							$ac_comp = strcmp($ac1, $ac2);
							$phone_comp = strcmp($phone1, $phone2); 
							 if($ind_comp != 0)
								{
									$error = TRUE;
									$err_line = $count . "\t \n";
									$err_ind1 = $ind1 . "\t \n";
									$err_ind2 = $ind2; 
									?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE Indent: " . " " . $err_ind1 . "<br>" . "QC Indent: " . " " . $err_ind2; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php } 
							if($addr_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_addr1 = $addr1;
									$err_addr2 = $addr2;?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE Address: " . " " . $err_addr1 . "<br>" . "QC Address: " . " " . $err_addr2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php } 
								if($city_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_city1 = $city1;
									$err_city2 = $city2; ?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE City: " . " " . $err_city1 . "<br>" . "QC City: " . " " . $err_city2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php	} 
								if($state_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_state1 = $state1;
									$err_state2 = $state2; ?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE State: " . " " . $err_state1 . "<br>" . "QC State: " . " " . $err_state2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php } 
								if($zc_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_zc1 = $zc1;
									$err_zc2 = $zc2; ?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE ZipCode: " . " " . $err_zc1 . "<br>" . "QC ZipCode: " . " " . $err_zc2; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php	} 
								if($tf_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_tf1 = $tf1;
									$err_tf2 = $tf2; ?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE TollFreeText: " . " " . $err_tf1 . "<br>" . "QC TollFreeText: " . " " . $err_tf2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php	} 
								if($ac_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_ac1 = $ac1;
									$err_ac2 = $ac2; ?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE AreaCode: " . " " . $err_ac1 . "<br>" . "QC AreaCode: " . " " . $err_ac2; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php	} 
								if($phone_comp != 0)
								{
									$error = TRUE;
									$err_line = $count;
									$err_phone1 = $phone1;
									$err_phone2 = $phone2; ?>
  <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php echo "KE PhoneNumber: " . " " . $err_phone1 . "<br>" . "QC PhoneNumber: " . " " . $err_phone2; ?>
  <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "<td>";?><?php if(isset($error)){echo 1;}?><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>									
						<?php
								}
							}
							}
						$count++; 
					}
				} ?>
<?php echo "<tr><td>"; ?><b><?php echo "Total";?></b><?php echo "</td>"; ?>
<?php echo "<td>";?><?php  ?>
  <?php echo "<td>";?><b><?php echo ""; ?></b><?php echo "</td>"; ?>
  <?php echo "<td>";?><b><?php echo ""; ?></b><?php echo "</td>"; ?>
  <?php echo "</td></tr>"; ?>
	<?php	}
}
?>
</tr>
</table>
</pre>
</head>	
</html>

 

pls advice.

 

thanks

 

 

Link to comment
Share on other sites

since you're using CI, just make a view with the header info in it.

 

say our view is 'excelview'

 

class Excel extends Controller
{

function __construct()
{
    Parent::Controller();
}

function export()
{
    $this->load->view('excelview');
}

}

Inside that view needs to go only the code to generate the excel file, nothing else.

 

On your page make this link:

 



<a href="excel/export">Generate excel file</a>

 

If you've set it up correctly, you will be prompted with a download window and you can then download your file.  This isn't the only way to do it, but its the easiest I know of.

Link to comment
Share on other sites

in the view page i place the

<?php 
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=yourfilename.xls');
header('Pragma: no-cache');
header('Expires: 0');
?>

 

the export to excel works ok, but no content were exported.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.