我需要下载服务器中提供的excel(Microsoft Excel工作表(.xlsx))文件

时间:2015-12-23 07:10:16

标签: java angularjs excel

 $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {'Content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                  'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'},

    }).
    success(function(data, status, headers, config) {
        $scope.alerts = [];
        if (data.success) {
            $scope.alerts.push({type:'success', msg : data.message});
            var blob = new Blob([data.binary], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
            console.log(blob.type);      
            var objectUrl = URL.createObjectURL(blob);
            window.open(objectUrl);
        }
        else {
            $scope.alerts.push({type:'danger', msg : data.message});
        }

    }).
    error(function(data, status, headers, config) {
        $scope.alerts.push({type:'danger', msg : 'Report failed. ' + data});
    });

在java方面:

byte[] b =downloadReport();
        result.put("binary", b);
  

调用返回字节数组的方法。

public byte[] downloadReport()
{
    File file = new File("D:/app/ce/excel/report/Report20151223_1133.xlsx");

    byte[] b = new byte[(int) file.length()];
    try {
          FileInputStream fileInputStream = new FileInputStream(file);
          fileInputStream.read(b);
          for (int i = 0; i < b.length; i++) {
                      System.out.print((char)b[i]);
           }
     } catch (FileNotFoundException e) {
                 System.out.println("File Not Found.");
                 e.printStackTrace();
     }
     catch (IOException e1) {
              System.out.println("Error Reading The File.");
               e1.printStackTrace();
     }
    return b;

}
  

为什么下载的excel正在打开并显示消息&#34;无法打开bcoz文件格式或扩展名无效。&#34;请检查。

     

要求就像它应该下载适当的数据。

0 个答案:

没有答案