excel函数中if android,在Android中阅读Excel
目前我正在開發android開發.根據要求,應用程序應該能夠讀取Excel文件以進行數據輸入.
正如其他人從這個主題開始,我已經完成了Java Excel Api和Apache POI,但兩者都需要進行一些修改以滿足我的要求:
JExcel API:
– 不能支持XLSX
Apache POI:
– 支持XLS文件
– 要在Dalvik中支持XLSX,您需要克服64K和javax庫,或使用端口版本(即從Andrew Kondratev開始)
– 文件大小將增加2.4MB
但是我們還有其他選擇在Android 4或更低版本中使用Excel文件嗎?
解決方法:
對于那些需要使用全功能excel文件(即繪圖,VBA等等)的應用程序,你應該使用Apache POI,它很簡單,但現在仍然是最好的解決方案.
但是,如果您只需要閱讀Excel,那么使用JavaScript解決方案可能會更好.使用js-xlsx庫,您可以將Excel文件傳輸到JSON.庫大小很小,只有395KB(僅包括xlsx.core.min.js)
我相信這不是最好的解決方案:
– WebView需要使用UI Thread,它可能會在讀取大型Excel文件時阻止UI.
– 性能問題
但您可以將其更改為其他JavaScript引擎(如Rhino或V8)以解決這些問題.
這是代碼
回調接口:
public interface ExcelReaderListener {
void onReadExcelCompleted(List stringList);
}
主要活動:
private ProgressDialog progressDialog;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AlertDialog.Builder(MainActivity.this)
.setMessage("message")
.setTitle("title")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
new FileChooser(MainActivity.this, new String[]{"xls", "xlsx"})
.setFileListener(new FileChooser.FileSelectedListener() {
@Override
public void fileSelected(File file) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("title");
progressDialog.setMessage("message");
progressDialog.setIndeterminate(true);
progressDialog.setCanceledOnTouchOutside(false);
Toast.makeText(MainActivity.this, file.getName(), Toast.LENGTH_SHORT).show();
String filePath = file.getAbsolutePath();
ExcelReaderListener excelReaderListener = MainActivity.this;
progressDialog.show();
try {
final WebView webView = new WebView(MainActivity.this);
new JSExcelReader(filePath, webView, excelReaderListener);
} catch (Exception ex) {
Log.e("Import excel error", ex.getMessage());
}
}
})
.showDialog();
}
})
.show();
}
@Override
public void onReadExcelCompleted(List stringList) {
Toast.makeText(MainActivity.this, "Parse Completed", Toast.LENGTH_SHORT).show();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
// Write into DB
...
}
用戶選擇excel文件的界面:
JSExcelReader :(讀取excel并將其轉換為ArrayList的核心部分)
public class JSExcelReader {
private ExcelReaderListener callback;
public JSExcelReader(String filePath, final WebView webView, ExcelReaderListener callback) {
this.callback = callback;
File file = new File(filePath);
try (InputStream is = new FileInputStream(file)) {
// convert file to Base64
if (file.length() > Integer.MAX_VALUE)
Log.e("File too big", "file too big");
byte[] bytes = new byte[(int) file.length()];
int offset = 0;
int numRead;
while (offset < bytes.length &&
(numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length)
throw new Exception("Could not completely read file");
final String b64 = Base64.encodeToString(bytes, Base64.NO_WRAP);
// feed the string into webview and get the result
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/AndroidParseExcel.html");
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webView.evaluateJavascript("convertFile('" + b64 + "');", new ValueCallback() {
@Override
public void onReceiveValue(String value) {
parseJSON(value);
}
});
}
});
} catch (Exception ex) {
Log.e("Convert Excel failure", ex.getMessage());
}
}
private void parseJSON(String jsonString) {
try {
// return value is something like "{\n\"Sheet1\":\n[\"title\"...
// you need to remove those escape character first
JSONObject jsonRoot = new JSONObject(jsonString.substring(1, jsonString.length() - 1)
.replaceAll("\\\\n", "")
.replaceAll("\\\\\"", "\"")
.replaceAll("\\\\\\\\\"", "'"));
JSONArray sheet1 = jsonRoot.optJSONArray("Sheet1");
List stringList = new ArrayList<>();
JSONObject jsonObject;
for (int i = 0; i < sheet1.length(); i++) {
jsonObject = sheet1.getJSONObject(i);
stringList.add(jsonObject.optString("title"));
}
callback.onReadExcelCompleted(stringList);
} catch (Exception ex) {
Log.e("Error in parse JSON", ex.getMessage());
}
}
}
AndroidParseExcel.html :(你應該把這個和JavaScript庫放到資產文件夾中)
"use strict";
var X = XLSX;
function convertFile(b64data) {
var wb = X.read(b64data, {type: 'base64',WTF: false});
var result = {};
wb.SheetNames.forEach(function(sheetName) {
var roa = X.utils.sheet_to_row_object_array(wb.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return JSON.stringify(result, 2, 2);
}
標簽:jexcelapi,android,excel,apache-poi
來源: https://codeday.me/bug/20190727/1553770.html
總結
以上是生活随笔為你收集整理的excel函数中if android,在Android中阅读Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android sdk中添加自定义api
- 下一篇: android 横向stepview,A