【问题标题】:Display excel file from assets folders in an android application在 android 应用程序中显示资产文件夹中的 excel 文件
【发布时间】:2015-08-21 09:34:15
【问题描述】:

谁能帮帮我

在安卓应用程序中显示一个取自资产文件夹的 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

【问题讨论】:

    标签: android excel android-activity android-assets


    【解决方案1】:

    迟来的反应。但它会被某人使用......

    试试这个……

    别忘了在 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;
     }
    }
    

    【讨论】:

    • 记得关闭文件,完成后输入流资源。我的意思是你可以添加一个 finally 子句,例如: finally{ if (pOIFSFileSystem != null) { try { pOIFSFileSystem.close(); } catch (IOException e) { e.printStackTrace(out);扔 e; } } } 这也可以应用于输入流
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多