我正在开发这个应用程序,它需要我提供一个选项来导出到 excel 并根据用户的选择在 excel 中生成图形。现在我使用了 JExcel 和 JFreechart 的组合来生成 excel 表和显示图形。我无法做的是为特定列生成图形,因为我需要提供单元格范围。
因此,还有其他替代方法可以做到这一点吗?我需要编写通用代码来显示数据和图形,以便它适用于所有类型的数据。
我正在开发这个应用程序,它需要我提供一个选项来导出到 excel 并根据用户的选择在 excel 中生成图形。现在我使用了 JExcel 和 JFreechart 的组合来生成 excel 表和显示图形。我无法做的是为特定列生成图形,因为我需要提供单元格范围。
因此,还有其他替代方法可以做到这一点吗?我需要编写通用代码来显示数据和图形,以便它适用于所有类型的数据。
index.php
<html>
<head>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="upload" value="">
<input type="submit" value="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
require_once 'dbConfig.php';
require_once 'excel_reader/excel_reader.php';
$excel = new PhpExcelReader();
$file = $_FILES['upload']['tmp_name'];
$excel->read($file);
// echo '<pre>';print_r($excel);
// echo '<pre>';print_r($excel->sheets[0]['cells']);
//echo '<pre>'; print_r($excel->sheets[0]['numCols']);
//echo '<pre>'; print_r($excel->sheets[0]['numRows']);
//echo '<pre>'; print_r($excel->sheets[0]['cells'][2][1]);
$coloms=$excel->sheets[0]['numCols'];
$rows=$excel->sheets[0]['numRows'];
// $cell = $excel->sheets[0]['cells'][$j][$i] ;
for($i=3;$i<=$rows;)
{
for($j=1;$j<=$coloms;)
{
$cell = ($excel->sheets[0]['cells'][$i][$j]);
$j++;
}
$name = ($excel->sheets[0]['cells'][$i][1]) ;
$roll_no = ($excel->sheets[0]['cells'][$i][2]);
$hobbies = ($excel->sheets[0]['cells'][$i][3]) ;
$i++;
//echo $name;
//echo $roll_no;
//echo $hobbies;
echo $r = "INSERT INTO fileupload(name,roll_no,hobbies) VALUES('".$name."','".$roll_no."','".$hobbies."')";
$result = mysqli_query($con,$r);
}
echo "<script>alert('Inserted Successfully');window.location.href = 'upload.php'</script>";
}
?>
</body>
</html>
upload.php
<table width="80%" border="1">
<tr>
<td>Name</td>
<td>Roll no</td>
<td>Hobbies</td>
</tr>
<?php
require_once 'dbConfig.php';
$sql="SELECT name,roll_no,hobbies FROM fileupload";
$result_set=mysqli_query($con,$sql);?>
<?php
$hobbies='';
while($row=mysqli_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['roll_no']; ?></td>
<td><?php $hobbies.=$row['hobbies'].','; echo $row['hobbies'];?></td>
</tr>
<?php
}
$hobbies= explode(',',rtrim($hobbies,','));
$hobbies= array_count_values($hobbies);
foreach ($hobbies as $key => $value)
{
$key .'-'.$value.'<br>';
}
?>
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load Charts and the corechart and barchart packages.
google.charts.load('current', {'packages':['corechart']});
// Draw the pie chart and bar chart when Charts is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
<?php
foreach ($hobbies as $key => $value)
{?>
['<?php echo $key ;?>',<?php echo $value;?>],
<?php }?>
]);
var piechart_options = {title:'Activities',
width:400,
height:300};
var piechart = new google.visualization.PieChart(document.getElementById('piechart_div'));
piechart.draw(data, piechart_options);
var barchart_options = {title:'Activities',
width:400,
height:300,
legend: 'none'};
var barchart = new google.visualization.BarChart(document.getElementById('barchart_div'));
barchart.draw(data, barchart_options);
var chart = new google.visualization.ImageLineChart(document.getElementById('chart_div'));
chart.draw(data,linechart_options );
}
</script>
<body>
<!--Table and divs that hold the pie charts-->
<table class="columns">
<tr>
<td><div id="piechart_div" style="border: 1px solid #ccc"></div></td>
<td><div id="barchart_div" style="border: 1px solid #ccc"></div></td>
<td><div id="chart_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
</body>
</html>