2013-03-08 43 views
1

我正在開發此應用程序,需要我提供一個選項以導出爲ex​​cel並根據用戶選擇在excel中生成圖形。現在,我已經使用了JExcel和JFreechart的組合來生成excel工作表和顯示圖形。 我無法做的是爲特定列生成圖形,因爲我需要提供單元格範圍。導出爲ex​​cel並顯示圖表

因此,有沒有其他的方法可以做到這一點? 我需要編寫通用代碼來顯示數據和圖形,以便它適用於所有類型的數據。

+1

圖表也可以在Excel中完成。 – 2013-03-08 05:48:18

+0

當excel文件被創建時,它也必須有圖形。我無法打開文件,然後創建圖形 – 2013-03-08 05:55:48

+0

*「..根據用戶選擇在excel中生成圖形。」*如果我是具有選擇的用戶,則選擇Open Office over Excel。 – 2013-03-08 06:16:54

回答

0
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>