首页 > 解决方案 > 如何从 EditText 编写 Android 外部 Excel 文件?

问题描述

如何将编辑文本中的数据写入android中的excel文件?我想将数据从editText绕过到excel文件中,而不是从存储中打开它。我能知道怎么做吗?

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

    final Activity activity = this;

    scanBtn = (Button) findViewById(R.id.BtnScn);
    resultSC = (TextView) findViewById(R.id.SC_text);
    ExelWrite = (Button) findViewById(R.id.ExelWrite);
    SetTemp = (EditText) findViewById(R.id.SetTemp);

    scanBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick (View view){

            startActivity(new Intent(getApplicationContext(),ScanCodeActivity.class));
        }
    });

    ExelWrite.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.ExelWrite:
            saveExcelFile(this,"Staff_Temperature.xls");
            break;

    }
}

private static boolean saveExcelFile(MainActivity context, String fileName) {

    // check if available and not read only
    if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
        Log.e(TAG, "Storage not available or read only");
        return false;
    }

    boolean success = false;

这是我为 excel 创建工作簿的地方

    //New Workbook
    Workbook wb = new HSSFWorkbook();

    Cell c = null;

    //Cell style for header row'
    CellStyle cs = wb.createCellStyle();
    cs.setFillForegroundColor(HSSFColor.LIME.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    //New Sheet
    Sheet sheet1 = null;
    sheet1 = wb.createSheet("RiSE");

    // Generate column headings
    Row row = sheet1.createRow(0);

    c = row.createCell(0);
    c.setCellValue("Staff Credential");
    c.setCellStyle(cs);

    c = row.createCell(1);
    c.setCellValue("Staff Temperature");
    c.setCellStyle(cs);

    sheet1.setColumnWidth(0, (15 * 500));
    sheet1.setColumnWidth(1, (15 * 500));

这是我为文件创建路径的地方。我想它应该在这里,但我不知道如何称呼它

// Create a path where we will place our List of objects on external storage

    File file = new File(context.getExternalFilesDir(null), fileName);
    FileOutputStream os = null;

    try {
        os = new FileOutputStream(file);
        wb.write(os);
        Log.w("FileUtils", "Writing file" + file);
        success = true;
    } catch (IOException e) {
        Log.w("FileUtils", "Error writing " + file, e);
    } catch (Exception e) {
        Log.w("FileUtils", "Failed to save file", e);
    } finally {
        try {
            if (null != os)
                os.close();
        } catch (Exception ex) {
        }
    }
    return success;
}

public static boolean isExternalStorageReadOnly() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
        return true;
    }
    return false;
}

public static boolean isExternalStorageAvailable() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        return true;
    }
    return false;
}

标签: android-studio

解决方案


推荐阅读