mardi 17 mars 2015

reading excel sheet from assets folder in android


How to read data in excel-sheet in assets folder and retrieve database? 'I have an excel sheet where my different tables of database is written and i want to read the excel sheet so to store it in database and then want to retrieve database



public class ReadFileAssetsActivity extends ActionBarActivity {



private Button btnReadExcel1;
AssetManager assetManager;

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

btnReadExcel1 = (Button) findViewById(R.id.btnReadExcel1);

btnReadExcel1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnReadExcel1)

readExcelFileFromAssets();
}
}); {


assetManager = getAssets();

}


}

public void readExcelFileFromAssets() {

try {
// Creating Input Stream

File file = new File( "file:\\assets\\winthrop-mobile-data-test-records.xls");




FileInputStream myInput = new
FileInputStream(file)//





// InputStream myInput;



// myInput = assetManager.open("main table.xlsx");



// 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<Row> rowIter = mySheet.rowIterator();


while (rowIter.hasNext()) {

HSSFRow myRow = (HSSFRow) rowIter.next();


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


while (cellIter.hasNext()) {

HSSFCell myCell = (HSSFCell) cellIter.next();

Log.e("FileUtils", "Cell Value: " + myCell.toString()
+ " Index :" + myCell.getColumnIndex());

// Toast.makeText(getApplicationContext(), "cell Value:

" + // myCell.toString(), Toast.LENGTH_SHORT).show();


}
}
} catch (Exception e) {
e.printStackTrace();
}

return;
}'

Aucun commentaire:

Enregistrer un commentaire