如何读取资产文件夹中excel-sheet中的数据并检索数据库? '我有一张excel表,我写了不同的数据库表,我想阅读excel表,以便将其存储在数据库中,然后想要检索数据库
public class ReadFileAssetsActivity扩展了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;
}'