gpt4 book ai didi

java - 导出至excel并显示图表

转载 作者:行者123 更新时间:2023-12-01 04:49:08 24 4
gpt4 key购买 nike

我正在开发这个应用程序,需要我提供一个导出到 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>

关于java - 导出至excel并显示图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15287440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com