通过php创建excel文件并在android中下载

时间:2014-07-25 05:55:08

标签: php android android-asynctask phpexcel

使用phpExcel,在服务器中创建了一个excel文件。

$objPHPExcel->setActiveSheetIndex(0);
// Save Excel file in server
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('newrecord.xlsx');

使用AsyncTask的类已被用作

@Override
protected Void doInBackground(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.1.1.5/connect/createexcelreport.php");

    try {
        HttpResponse response = httpclient.execute(httppost);
        String responseString = EntityUtils.toString(response.getEntity());

        JSONObject jsonObj = new JSONObject(responseString);
        int success = jsonObj.getInt(TAG_SUCCESS);
        if (success==1){
            //file has been created. 
            //through browser, I can access it in http://10.1.1.5/connect/newreport.xlsx

请问anyobdy请分享我将如何在这个帖子的android中获取它。 我试过了

if(success==1){
InputStream data = response.getEntity().getContent();
        try {
            OutputStream output = new FileOutputStream("newrecord.xlsx");
            try {
                ByteStreams.copy(data, output);
            } finally {
                Closeables.close(output, true);
            }
        } finally {
         ..............
        }

但它给文件找不到异常。 NewBie,请你好。

我已经搜索过任何可能的解决方案。因此,请在标记重复之前比较问题

1 个答案:

答案 0 :(得分:0)

通过保存为newrecord.xlsx这样的文件名,您可以将文件保存到您正在运行PHP的网络服务器上。如果您想将其作为响应直接发送到调用客户端(您的Android设备),则需要将其保存到php://output(发送相应的标头)。

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;