gpt4 book ai didi

android - 如何在 Android 中导出 excel?

转载 作者:搜寻专家 更新时间:2023-11-01 09:39:03 33 4
gpt4 key购买 nike

我想将字符串或对象导出到 Android 中的 excel。我没有一个好主意。我应该怎么办?请给我一些关于 android export excel 的建议。

最佳答案

  1. 添加 gradle 导入

compile group: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6'

  1. 在AndroidManifest.xml文件中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  1. 创建新类 ExcelExporter:

    import android.os.Environment;

    import java.io.File;
    import java.util.Locale;

    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;

    public class ExcelExporter {

    public static void export() {
    File sd = Environment.getExternalStorageDirectory();
    String csvFile = "yourFile.xls";

    File directory = new File(sd.getAbsolutePath());

    //create directory if not exist
    if (!directory.isDirectory()) {
    directory.mkdirs();
    }
    try {

    //file path
    File file = new File(directory, csvFile);
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setLocale(new Locale(Locale.GERMAN.getLanguage(), Locale.GERMAN.getCountry()));
    WritableWorkbook workbook;
    workbook = Workbook.createWorkbook(file, wbSettings);

    //Excel sheetA first sheetA
    WritableSheet sheetA = workbook.createSheet("sheet A", 0);

    // column and row titles
    sheetA.addCell(new Label(0, 0, "sheet A 1"));
    sheetA.addCell(new Label(1, 0, "sheet A 2"));
    sheetA.addCell(new Label(0, 1, "sheet A 3"));
    sheetA.addCell(new Label(1, 1, "sheet A 4"));

    //Excel sheetB represents second sheet
    WritableSheet sheetB = workbook.createSheet("sheet B", 1);

    // column and row titles
    sheetB.addCell(new Label(0, 0, "sheet B 1"));
    sheetB.addCell(new Label(1, 0, "sheet B 2"));
    sheetB.addCell(new Label(0, 1, "sheet B 3"));
    sheetB.addCell(new Label(1, 1, "sheet B 4"));

    // close workbook
    workbook.write();
    workbook.close();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
  2. 在您的 MainActivity 中添加以下方法:

private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(StartMenu.this, permission)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(
StartMenu.this, permission)) {

//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(StartMenu.this,
new String[]{permission}, requestCode);

} else {
ActivityCompat.requestPermissions(StartMenu.this,
new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this, permission + " is already granted.",
Toast.LENGTH_SHORT).show();
}
}
  1. 找一个地方执行导出,例如:
@Override
protected void onResume() {
super.onResume();
askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, READ_EXST);
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXST);
ExcelExporter.export();
}

关于android - 如何在 Android 中导出 excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41017394/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com