0

I want to export data from MySQL to excel file. As soon as file created download file in android device. I create excel file by this code :

$result = mysql_query("SELECT * FROM `tbl_notes` WHERE `user_id` = '$user_id' ") or
    die(mysql_error());

include_once ("jdf.php");
$date = jdate('Y/n/j');

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$date.xls");
header("Pragma: no-cache");
header("Expires: 0");

$sep = "\t";

while ($row = mysql_fetch_row($result)) {
    $schema_insert = "";
    for ($j = 0; $j < mysql_numfields($result); $j++) {
        if (!isset($row)) {
            $schema_insert .= "NULL" . $sep;
        } else
            if ($row[$j] != "") {
                $schema_insert .= "$row[$j]" . $sep;
            } else {
                $schema_insert .= "" . $sep;
            }
    }
    $schema_insert = str_replace($sep . "$", "", $schema_insert);
    $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
    $schema_insert .= "\t";
    print (trim($schema_insert));
    print "\n";
}

I don't know how to force android device to download file in php code. thanks.

Hajitsu
  • 764
  • 17
  • 49

1 Answers1

2

In your question you have to separate between two things:

  • how to generate an excel file from a mysql database using php and
  • how to access the generated excel file from an android phone.

Android itself is not able to execute your php script.

To generate an excel file from a mysql database using php you have to put your php file somewhere where it can be read by a php interpreter and where it is able to access your mysql database. Usually, php is executed on some webserver. However it also possbile to execute it from the shell. If it is executed on some webserver, you are able to access your script and download the excel file with any browser.

To access the generated excel file from an android phone you can either access the php script on some webserver using an android browser. Are more sophisticated approach is to develop an android application and download the excel via some http rest application. For example, you could use the HttpConnection class.

joel
  • 480
  • 1
  • 5
  • 9
  • In android app i send the commands with http connection and recieved the data by json.my problem is how to download the created excel file. – Hajitsu Nov 27 '12 at 12:48
  • If you have a specific question about your code, then post your code. Json is a text based notation to structure data. However you want to download your excel file, which isn't a text file. Therefore you have to process binary data. [Here] (http://stackoverflow.com/questions/6660243/how-do-i-use-httpclient-in-java-to-retrieve-a-binary-file) you find an example to do this with the HttpClient. [Here] (http://stackoverflow.com/questions/3221979/reading-binary-file-from-urlconnection) you will find also an example to download the file using the URLConnection class. – joel Nov 28 '12 at 19:46