如何在Android中导出excel?
我想在Android中将字符串或对象导出到excel。 我没有什么好主意。 我该怎么办?如何在Android中导出excel?,android,export-to-excel,Android,Export To Excel,我想在Android中将字符串或对象导出到excel。 我没有什么好主意。 我该怎么办? 请给我一些关于android导出excel的建议。试试这个简单excel: 或android5xlsx: 它们是在Android上包装和重新包装ApachePOI的试验。如果您已经熟悉ApachePOI,这将是一个很好的开始 我希望这会有帮助 添加渐变导入 编译组:“net.sourceforge.jexcelapi”,名称:“jxl”,版本: “2.6” 在AndroidManifest.xml文件中添
请给我一些关于android导出excel的建议。试试这个简单excel: 或android5xlsx: 它们是在Android上包装和重新包装ApachePOI的试验。如果您已经熟悉ApachePOI,这将是一个很好的开始 我希望这会有帮助
好的,我试试。谢谢。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
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();
}
}
}
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();
}
}
@Override
protected void onResume() {
super.onResume();
askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, READ_EXST);
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXST);
ExcelExporter.export();
}