Android Read Excel File from Website and display using a ListView

In this example we start the activity by making a HTTP request to read the Excel file hosted on a Web Server. We take help of the AsyncTask to fetch the data in background from the Excel file using the URL pointing to it. The input stream from the file is then parsed using the Apache POI classes and stored in an ArrayList. After the AsyncTask is completed, basically the complete Excel file is downloaded and parsed it is then displayed using a ListView.

Android receiving excel data over Web
Android display excel data in a ListView
Android excel data into ListView

Android Activity - ReadExcelFromUrlActivity.java

package com.as400samplecode;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import android.app.Activity;
import android.widget.ArrayAdapter;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.ListView;
import android.widget.Toast;

public class ReadExcelFromUrlActivity extends Activity {

 private String LOG_TAG = "ReadExcelFromUrl";
 ArrayList<ShoppingCart> shoppingCartList;    

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  //URL path for the Excel file
  String url = "http://demo.mysamplecode.com/Servlets_JSP/demoFiles/ExcelOrderFile.xls";
  excelURL(url); 

 }
 
 private void displayCart() {

  //Array list of countries
  List<String> myList = new ArrayList<String>();
  for(int i = 0, l = shoppingCartList.size(); i < l; i++){
   ShoppingCart shoppingCart = shoppingCartList.get(i);
   String myData = shoppingCart.getItemNumber() + ": " +
       shoppingCart.getDescription() + "\nPrice: $" +
       shoppingCart.getPrice() + "\nQuantity: " +
       shoppingCart.getQuantity();
   myList.add(myData);
  }
  
  //Display the Excel data in a ListView
  ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
    R.layout.data_list, myList);
  ListView listView = (ListView) findViewById(R.id.listView1);
  listView.setAdapter(dataAdapter);
  
  
 }

 public void excelURL(String url) {
  Log.v(LOG_TAG, url);
  new ExcelURL().execute(url);
 }

 private class ExcelURL extends AsyncTask<String, Void, String> {
  private static final int REGISTRATION_TIMEOUT = 3 * 1000;
  private static final int WAIT_TIMEOUT = 30 * 1000;
  private final HttpClient httpclient = new DefaultHttpClient();
  final HttpParams params = httpclient.getParams();
  HttpResponse response;
  private String content =  null;
  private ProgressDialog dialog = new ProgressDialog(ReadExcelFromUrlActivity.this);

  protected void onPreExecute() {
   dialog.setMessage("Getting your data... Please wait...");
   dialog.show();
  }

  protected String doInBackground(String... urls) {

   String URL = null;

   try {

    URL = urls[0];
    HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
    ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);

    HttpGet httpGet = new HttpGet(URL);
    response = httpclient.execute(httpGet);

    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
     parseExcel(response.getEntity().getContent());
    } else{
     //Closes the connection.
     Log.w("HTTP1:",statusLine.getReasonPhrase());
     response.getEntity().getContent().close();
     throw new IOException(statusLine.getReasonPhrase());
    }
   } catch (ClientProtocolException e) {
    Log.w("HTTP2:",e );
    content = e.getMessage();
    cancel(true);
   } catch (IOException e) {
    Log.w("HTTP3:",e );
    content = e.getMessage();
    cancel(true);
   }catch (Exception e) {
    Log.w("HTTP4:",e );
    content = e.getMessage();
    cancel(true);
   }

   return content;
  }

  protected void onCancelled() {
   dialog.dismiss();
   Toast toast = Toast.makeText(ReadExcelFromUrlActivity.this, 
     "Error connecting to Server", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.TOP, 25, 400);
   toast.show();

  }

  protected void onPostExecute(String content) {
   dialog.dismiss();
   displayCart();
  }
  
  

  private void parseExcel(InputStream fis){

   shoppingCartList = new ArrayList<ShoppingCart>();    

   try{

    // Create a workbook using the Input Stream 
    HSSFWorkbook myWorkBook = new HSSFWorkbook(fis);

    // Get the first sheet from workbook 
    HSSFSheet mySheet = myWorkBook.getSheetAt(0);

    // We now need something to iterate through the cells
    Iterator<Row> rowIter = mySheet.rowIterator();
    while(rowIter.hasNext()){

     HSSFRow myRow = (HSSFRow) rowIter.next();
     // Skip the first 2 rows
     if(myRow.getRowNum() < 2) {
      continue;
     }
     
     ShoppingCart shoppingCart = new ShoppingCart();

     Iterator<Cell> cellIter = myRow.cellIterator();
     while(cellIter.hasNext()){

      HSSFCell myCell = (HSSFCell) cellIter.next();
      String cellValue = "";
      
      // Check for cell Type
      if(myCell.getCellType() == HSSFCell.CELL_TYPE_STRING){
       cellValue = myCell.getStringCellValue();
      }
      else {
       cellValue = String.valueOf(myCell.getNumericCellValue());
      }
      
      // Just some log information
      Log.v(LOG_TAG, cellValue);
      
      // Push the parsed data in the Java Object
      // Check for cell index
      switch (myCell.getColumnIndex()) {
      case 0: 
       shoppingCart.setItemNumber(cellValue);
       break;
      case 1: 
       shoppingCart.setDescription(cellValue);
       break;
      case 2: 
       shoppingCart.setPrice(Double.valueOf(cellValue));
       break;
      case 3: 
       shoppingCart.setQuantity(Double.valueOf(cellValue));
       break;
      default: 
       break;
      }
      
     }

     // Add object to list
     shoppingCartList.add(shoppingCart);
    }
   }
   catch (Exception e){
    e.printStackTrace(); 
   }

  }

 }
}

Shopping cart Java object - ShoppingCart.java

package com.as400samplecode;

public class ShoppingCart {
 
 String itemNumber = null;
 String description = null;
 Double price = null;
 Double quantity = null;
 
 public String getItemNumber() {
  return itemNumber;
 }
 public void setItemNumber(String itemNumber) {
  this.itemNumber = itemNumber;
 }
 public String getDescription() {
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
 public Double getPrice() {
  return price;
 }
 public void setPrice(Double price) {
  this.price = price;
 }
 public Double getQuantity() {
  return quantity;
 }
 public void setQuantity(Double quantity) {
  this.quantity = quantity;
 }
 

}

Android Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.as400samplecode"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light">
        <activity
            android:name=".ReadExcelFromUrlActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Please Note: You need to download and add Apache POI jar files to your Android Project

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.