Simple Microsoft Excel file generation via PHP and Mysql
config.php :-
<?php
$host_name="localhost";
$username="root";
$password="";
$db_name="test";
//Database Connection
$conn=mysql_connect($host_name,$username,$password);
//Database Select
mysql_select_db($db_name,$conn);
?>
BuyerAssistReport.php :-
<?php
error_reporting (E_ALL ^ E_NOTICE);
include("config.php");
$query = "SELECT * FROM fruit ";
$result = mysql_query($query) or die(mysql_error().'Error, query failed');
$tsv="";
$html="";
$xml = "";
while($row = mysql_fetch_array($result))
{
$xml .="<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$row['price']."</td></tr>";
}
$html .= "<table border=1 style='border-color:#CCCCCC'><tr><td><b>Fruit id</b></td><td><b>Fruit name</b></td><td><b>Fruitprice</b></td></tr>" . $xml . "</table>";
$fileName = 'BuyerAssistReport.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
echo $tsv;
echo $html;
?>
Database backup:-
-- phpMyAdmin SQL Dump
-- version 3.4.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 01, 2012 at 03:31 PM
-- Server version: 5.5.20
-- PHP Version: 5.3.10
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `fruit`
--
CREATE TABLE IF NOT EXISTS `fruit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `fruit`
--
INSERT INTO `fruit` (`id`, `name`, `price`) VALUES
(1, 'apple', 20),
(2, 'tomatto', 30),
(3, 'mango', 30),
(4, 'bannana', 40);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
$host_name="localhost";
$username="root";
$password="";
$db_name="test";
//Database Connection
$conn=mysql_connect($host_name,$username,$password);
//Database Select
mysql_select_db($db_name,$conn);
?>
BuyerAssistReport.php :-
<?php
error_reporting (E_ALL ^ E_NOTICE);
include("config.php");
$query = "SELECT * FROM fruit ";
$result = mysql_query($query) or die(mysql_error().'Error, query failed');
$tsv="";
$html="";
$xml = "";
while($row = mysql_fetch_array($result))
{
$xml .="<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$row['price']."</td></tr>";
}
$html .= "<table border=1 style='border-color:#CCCCCC'><tr><td><b>Fruit id</b></td><td><b>Fruit name</b></td><td><b>Fruitprice</b></td></tr>" . $xml . "</table>";
$fileName = 'BuyerAssistReport.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
echo $tsv;
echo $html;
?>
Database backup:-
-- phpMyAdmin SQL Dump
-- version 3.4.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 01, 2012 at 03:31 PM
-- Server version: 5.5.20
-- PHP Version: 5.3.10
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `fruit`
--
CREATE TABLE IF NOT EXISTS `fruit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `fruit`
--
INSERT INTO `fruit` (`id`, `name`, `price`) VALUES
(1, 'apple', 20),
(2, 'tomatto', 30),
(3, 'mango', 30),
(4, 'bannana', 40);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Comments
Post a Comment