Android创建保存Excel文件

Android开发生成保存Excel文件,首先下载两个jar包。下载地址:Android读写Excel文件的两个jar包资源-CSDN文库

poi-3.12-android-a.jar

poi-ooxml-schemas-3.12-20150511-a.jar

把jar包放在app的libs文件夹下,引用jar我一般都在build.gradle的dependencies中添加

复制代码
implementation fileTree(dir: "libs", include: ["*.?ar"])

把.jar和.aar都引用上。

如果项目需要混淆,则加上混淆

复制代码
#生成Excel
-keep class com.bea.xml.stream.** { *;}
-keep class com.wutka.dtd.** { *;}
-keep class org.** { *;}
-keep class aavax.xml.** { *;}
-keep class com.microsoft.schemas.office.x2006.** { *; }
-keep class schemaorg_apache_xmlbeans.** { *; }
-keep class schemasMicrosoftComOfficeExcel.** { *; }
-keep class schemasMicrosoftComOfficeOffice.** { *; }
-keep class schemasMicrosoftComVml.** { *; }
-keep class repackage.** { *; }
-keep class schemaorg_apache_xmlbeans.** { *; }

然后就是生成Excel文件了

复制代码
/**
     * 创建excel分析结果文件
     */
    private void createExcel(){
        new Thread(){
            @Override
            public void run() {
                try {
                    Log.i("lllllll", "createExcel  ======");
                    XSSFWorkbook workbook = new XSSFWorkbook();
                    XSSFSheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName("sheet"));
                    //第一行
                    Row row0 = sheet.createRow(0);
                    String[] title = new String[]{"市公司", "道路名称", "路段长度(米)", "计算值", "起始经纬度", "结束经纬度"};
                    for (int i= 0;i<title.length;i++){
                        Cell cell = row0.createCell(i);
                        cell.setCellValue(title[i]);
                    }
                    //添加数据
                    for (int i= 0;i<excelBeans.size();i++){
                        ExcelBean eb = excelBeans.get(i);
                        Row row = sheet.createRow(i+1);
                        for (int j= 0;j<title.length;j++){
                            Cell cell = row.createCell(j);
                            if (j == 0){
                                cell.setCellValue(eb.getCity());
                            }else if (j == 1){
                                cell.setCellValue(eb.getAddress());
                            }else if (j == 2){
                                double distance = DistanceUtil.getDistance(eb.getStartLatLng(), eb.getStopLatLng());
                                cell.setCellValue(NumberUtils.makeNumDecimalPlaces(distance, 2));
                            }else if (j == 3){
                                cell.setCellValue(eb.getCoverage());
                            }else if (j == 4){
                                cell.setCellValue("["+eb.getStartLatLng().longitude + ","+eb.getStartLatLng().latitude+"]");
                            }else if (j == 5){
                                cell.setCellValue("["+eb.getStopLatLng().longitude + ","+eb.getStopLatLng().latitude+"]");
                            }
                        }
                    }

                    String path = parseFile.getAbsolutePath();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
                    String time = dateFormat.format(new Date());
                    analysisFilePath = path.replace(".csv", "_analysis_"+earfcn+"_"+time+".xlsx");
                    Log.i("lllllll", "fileName  ======" +analysisFilePath);
                    FileOutputStream fos = new FileOutputStream(analysisFilePath);
                    workbook.write(fos);
                    fos.flush();
                    fos.close();

                    Log.i("lllllll", "保存成功  ======");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

我这生成Excel之后,在手机里能打开,但是在电脑上只能用WPS打开,用offic打开就会报错。我查了半天,最终没有找到问题在哪。有可能是我电脑上的offic版本太低了,不过WPS能打开已经能满足需求了。

相关推荐
峥嵘life38 分钟前
Android EDLA项目导入mainline包后蓝牙签名报错分析解决
android·gitee
灵猫小西44 分钟前
Android耗时卡顿原因排查工具Profiler
android·profiler·耗时卡顿
办公解码器1 小时前
Excel怎么制作下拉菜单?
excel
gfdgd xi2 小时前
好消息:KMRE 安卓模拟器现可在Debian/GXDE使用
android·python·架构·bug·deepin·龙芯
La Pulga2 小时前
【STM32】FLASH闪存
android·c语言·javascript·stm32·单片机·嵌入式硬件·mcu
游戏开发爱好者83 小时前
iOS 崩溃日志分析工具全指南,多工具协同构建稳定性分析体系
android·macos·ios·小程序·uni-app·cocoa·iphone
左师佑图12 小时前
综合案例:Python 数据处理——从Excel文件到数据分析
开发语言·python·数据分析·excel·pandas
晨陌y13 小时前
深入剖析:仓颉语言的性能优化核心技术
android·性能优化·仓颉
xhbh66613 小时前
【实战总结】MySQL日期加减大全:日期计算、边界处理与性能优化详解
android
00后程序员张13 小时前
如何提高 IPA 安全性 多工具组合打造可复用的 iOS 加固与反编译防护体系(IPA 安全 iOS 加固 无源码混淆 Ipa Guard 实战)
android·安全·ios·小程序·uni-app·iphone·webview