Advertisement
Guest User

Untitled

a guest
May 21st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. <?php
  2. require_once("Java.inc");
  3. header("Content-type: application/vnd.ms-excel");
  4. $vFileName = 'MyExcelFile.xls';
  5. header("Content-Disposition: attachment; filename=$vFileName");
  6.  
  7. // create a 50x40 excel sheet and return it to the client
  8. $workbook = new java("org.apache.poi.hssf.usermodel.HSSFWorkbook");
  9. $sheet = $workbook->createSheet("datafrommysql");
  10.  
  11.  
  12. $host = "127.0.0.1"; // localhost
  13. $user = "root";
  14. $pass = "";
  15. $database = "dbupm";
  16. mysql_connect($host, $user, $pass) or die ("Cannot connect to Server!");
  17. mysql_select_db($database) or die ("Cannot Select Database!");
  18.  
  19. $vSQL = "Select * from usermaster";
  20.  
  21. $vExcelRowNo = 0;
  22. $rs = mysql_query($vSQL);
  23.  
  24. while($dbrow = mysql_fetch_array($rs)){
  25.  
  26. $row = $sheet->createRow($vExcelRowNo++);
  27. for($x=0; $x<=2; $x++) {
  28. $cell = $row->createCell($x);
  29. $cell->setCellValue($dbrow[$x]);
  30. }
  31.  
  32. }
  33.  
  34. // create and return the excel sheet to the client
  35. $memoryStream = new java("java.io.ByteArrayOutputStream");
  36. $workbook->write($memoryStream);
  37. $memoryStream->close();
  38. echo java_values($memoryStream->toByteArray());
  39. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement