在Android应用程序中显示资产文件夹中的Excel文件
任何人都可以帮助我
从android应用程序中的assets文件夹中显示一个excel文件
我无法显示文件。 我用POI的jar文件也显示该文件…请给我的代码
我尝试从SD卡,但我不能从资产
public class MainActivity extends Activity { String dbStr = Environment.getExternalStorageDirectory() + "/dropbox/xls/stock1.xls"; String strHyouji=""; String[][] arrays = read(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(arrays == null) { strHyouji="no such file"; } else { for (String[] array : arrays) { for (String v : array) { strHyouji = strHyouji + v + ","; } strHyouji = strHyouji + "\n"; } } TextView textSetting = (TextView) findViewById(R.id.textView1); textSetting.setText(strHyouji); } public String[][] read() { Workbook workbook = null; try { WorkbookSettings ws = new WorkbookSettings(); ws.setGCDisabled(true); workbook = Workbook.getWorkbook(new File(dbStr), ws); Sheet sheet = workbook.getSheet(0); int rowCount = sheet.getRows(); String[][] result = new String[rowCount][]; for (int i = 0; i < rowCount; i++) { Cell[] row = sheet.getRow(i); result[i] = new String[row.length]; for (int j = 0; j < row.length; j++) { result[i][j] = row[j].getContents(); } } return result; } catch (BiffException e) { strHyouji=strHyouji+ e.toString(); } catch (IOException e) { strHyouji=strHyouji+ e.toString(); } catch (Exception e) { strHyouji=strHyouji+ e.toString(); } finally { if (workbook != null) { workbook.close(); } } return null; } }
邮件:ravitejabrt@gmail.com
迟到的反应,但它会被用于某人…
尝试这个…
不要忘记在AssetFolder中有Excel文件。
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; } }