对于那些需要使用全功能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文件的界面:

https://rogerkeays.com/simple-android-file-chooser

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);

}

excel函数怎么用android,在Android中阅读Excel相关推荐

  1. excel函数中if android,在Android中阅读Excel

    目前我正在开发android开发.根据要求,应用程序应该能够读取Excel文件以进行数据输入. 正如其他人从这个主题开始,我已经完成了Java Excel Api和Apache POI,但两者都需要进 ...

  2. excel函数:汉字转全拼_星期五的Excel函数:带过滤器的小计和总和

    excel函数:汉字转全拼 Last week, we used the Excel SUBTOTAL function to sum items in a filtered list, while ...

  3. 15个常用excel函数公式_项目上最常用的Excel函数公式大全,现在看还不晚

    做工程的免不了要做资料算量,Excel是工作中最常用的工具之一.只要搞清楚它的一些使用小技巧,工作效率那是嗖嗖的往上蹭啊.下面这些,你就绝对不能错过! 一.数字处理 1.取绝对值 =ABS(数字) 2 ...

  4. 如何用matlab做表格,matlab用excel数据做表格-如何向matlab中导入excel表格 利用表格数据做出图......

    matlab如何从excel表格中读取数据? 工具:matlab 2018b 1.打开matlab击主页下入数据,可以导入excel数据此将自己命名huitushuju文件导入: 2.点击打开按钮,即 ...

  5. 15个常用excel函数公式_excel表格时间和日期应用 – Excel常用函数公式及技巧

    excel表格时间和日期应用 – Excel常用函数公式及技巧 自动显示当前日期公式 =YEAR(NOW()) 当前年 =MONTH(NOW()) 当前月 =DAY((NOW())) 当前日 如何在单 ...

  6. java获取excel文件第一行_java - 在Java中读取Excel文件,但第一行除外 - 堆栈内存溢出...

    我正在尝试从使用xlsx文件的JasperReport模板自动创建报告,我用来从excel文件读取的方法是这样的: String[] columnNames = new String[]{" ...

  7. 计算机二级考试中Excel函数如何应用,计算机二级考试excle常用函数【计算机二级ms office中excel中必考函数有哪些?】...

    计算机二级excel主要函数的用法 AVERAGE 求出所有参数的术平均值. 数据计算 COLUMN 显示所引用单元格的列标号值. 显示位置 CONCATENATE 将多个字符文本或单元格中的数据连接 ...

  8. Excel函数应用之查询与引用函数

    编者语:Excel是办公室自动化中非常重要的一款软件,很多巨型国际企业都是依靠Excel进行数据管理.它不仅仅能够方便的处理表格和进行图形分析,其更强大的功能体现在对数据的自动处理和计算,然而很多缺少 ...

  9. 用函数统计各分数段人数c语言,excel统计各分数段人数的函数

    Q2:如何使用Excel函数统计各分数段的人数 "使用Excel函数统计各分数段的人数"的操作步骤是: 1.打开Excel工作表: 2.由已知条件可知,需要将B2:B34单元格的分 ...

最新文章

  1. C++网络包截取开发
  2. [原创] CSS自定义IOS苹果,Android安卓的CheckBox 效果,可以根据文字大小变化而变化,内框显示文字,另外可自定大小,自定颜色...
  3. 具有Azure功能的无服务器API
  4. 华为路由器与CISCO路由器在配置上的差别
  5. mysql新增表字段回滚_MySql学习笔记四
  6. 自动化审批决策树助你面试更上一层楼
  7. 虚拟机中CentOS系统安装流程
  8. 如何解决AttributeError: ‘DataFrame‘ object has no attribute ‘sort‘
  9. winform根据字符串生成HTML静态页
  10. Style transfer系列论文之——Arbitrary Style Transfer in Real-time with Adaptive Instance Normali,ICCV, 2017
  11. 利用计算机模拟,科学家试图利用计算机模拟整个宇宙的演化
  12. 阿里笔试算法题2021
  13. 借助C++类结构计算矩形面积(矩形类)
  14. js原生往父元素中添加子元素
  15. MacBook Air如何清理缓存
  16. python爬取bilibili弹幕_python 爬取bilibili 视频弹幕
  17. 什么是开环控制,闭环控制?它们的根本区别是什么
  18. IT人系列一 王珅:程序员转型做旧书书商年交易额逾千万
  19. 深度篇——人脸识别(一)  ArcFace 论文 翻译
  20. Android 第三方应用跳转到qq进行聊天(qq咨询)

热门文章

  1. 前端学习(2479):接口文档使用
  2. 前端学习(1945)vue之电商管理系统电商系统之调用api获取数据
  3. 前端学习(1602):create-react-app基本创建
  4. 前端学习(1485):restful接口规则
  5. 前端学习(1355) 子模板
  6. 前端学习(668):分支导读
  7. 前端学习(574):margin无效情形之绝对定位下的非定义的方向“无效”
  8. 前端学习(554):node实现登录和注册第二部分代码
  9. 第五十六期:IPv6只是增加了地址数量?其实真相并没有那么简单!
  10. linux(1):Linux经典面试题