Hi,
In one of my Project, I need to read an excel file data which is in assets folder.
1) Create Project in Android using Eclipse.
2) Download poi Java Library or After Downloading take this poi-3.10-FINAL-20140208.jar file and add to the libs folder and add to Build Path.
3) Place an excel sheet in assets folder in your project.
4) Copy the following code in your activity
MainActivity.java
5) Note : For Output
Please see the logcat in Eclipse for the data.
Import both project and AppCompat v7 Library.
6) Download the full source code
In one of my Project, I need to read an excel file data which is in assets folder.
1) Create Project in Android using Eclipse.
2) Download poi Java Library or After Downloading take this poi-3.10-FINAL-20140208.jar file and add to the libs folder and add to Build Path.
3) Place an excel sheet in assets folder in your project.
4) Copy the following code in your activity
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | public class MainActivity extends ActionBarActivity implements OnClickListener { private Button btnReadExcel1; AssetManager assetManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnReadExcel1 = (Button) findViewById(R.id.btnReadExcel1); btnReadExcel1.setOnClickListener(this); assetManager = getAssets(); } @Override public void onClick(View v) { if (v.getId() == R.id.btnReadExcel1) { readExcelFileFromAssets(); } } public void readExcelFileFromAssets() { try { // Creating Input Stream /* * File file = new File( filename); FileInputStream myInput = new * FileInputStream(file); */ InputStream myInput; // Don't forget to Change to your assets folder excel sheet myInput = assetManager.open("contacts.xls"); // 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; } } |
5) Note : For Output
Please see the logcat in Eclipse for the data.
Import both project and AppCompat v7 Library.
6) Download the full source code
No comments:
Post a Comment