选择 .xls / .xlsx 文件并读取其中的数据

3

我正在开发一个应用程序,我想只选择 .xls/.xlsx 文件并从中读取数据。我该如何实现?

我写了下面的代码..但它允许我选择所有类型的数据。

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

    btnpurchase=(Button)findViewById(R.id.btnpurchase);
    btnsales=(Button)findViewById(R.id.btnsales);


    btnpurchase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("application/xlsx");
            startActivityForResult(intent, 7);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    switch(requestCode){

        case 7:

            if(resultCode==RESULT_OK){

                String PathHolder = data.getData().getPath();

                Toast.makeText(MainActivity.this, PathHolder , Toast.LENGTH_LONG).show();

            }
            break;

    }
}
1个回答

3
你可以从这里参考Microsoft Office MIME类型列表 https://dev59.com/bm855IYBdhLWcg3wq2TL#4212908 .xls文件

application/vnd.ms-excel

.xlsx文件

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

我认为这些是你想要的。

现在我使用了application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,我只能选择xlsx格式。那么我该如何从中读取数据呢? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接