Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
M making an android application in which i have to display data from excel sheet to my android application.

so can anybody tell me how to do this..need some idea...:(
Posted

1 solution

Hi,

Have a look at Apache POI.

Apache POI will allow you to work with .xls files.

The last time I checked you could only work with .xls files (not .xlsx).

Here is a Sample for you to look at.

... hope it helps.
 
Share this answer
 
Comments
vn12 30-Oct-14 7:21am    
Have gone through this example and have Apache POI..i just want to display data of my excel sheet into android app.so i implemented only readExcelFile method..but uts not working


Here is my code:

SearchActivity.java

package com.medicine.meditracker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class SearchActivity extends ActionBarActivity implements OnClickListener {

Button readExcelButton;
static String TAG = "ExelLog";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
readExcelButton = (Button) findViewById(R.id.readExcel);
readExcelButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.readExcel:
readExcelFile(this,"medicine.xlsx");
break;
}
}
private static void readExcelFile(Context context, String filename) {

if (!isExternalStorageAvailable() || isExternalStorageReadOnly())
{
Log.e(TAG, "Storage not available or read only");
return;
}

try{
// Creating Input Stream
File file = new File(context.getExternalFilesDir(null), "medicine.xlsx");
FileInputStream myInput = new FileInputStream(file);

// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

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

/** We now need something to iterate through the cells.**/
Iterator rowIter = mySheet.rowIterator();

while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
Log.d(TAG, "Cell Value: " + myCell.toString());
Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){e.printStackTrace(); }

return;
}

public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}

public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOU
hypermellow 30-Oct-14 7:34am    
What do you mean it's not working ... how far do you get? ... what exception is raise?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900