Android 使用poi生成Excel ,word并保存在指定路径内

news/2024/7/21 5:55:37 标签: android, excel, word

一添加依赖

minSdk= 26   //注意最小支持SDK26
dependencies {
    implementation 'org.apache.poi:poi:4.1.2'
    implementation 'org.apache.poi:poi-ooxml:4.1.2'
    implementation 'javax.xml.stream:stax-api:1.0-2'
}

二创建方法

word">private word">void createExcelFile(String Path) {
    // 创建工作簿
    Workbook workbook = word">new XSSFWorkbook();
    // 创建工作表
    Sheet sheet = workbook.createSheet("姓名");
    // 创建行
 /*   Row row = sheet.createRow(0);
    // 创建单元格
    for (int i = 0; i <10 ; i++) {
        Cell cell = row.createCell(i);
        // 设置单元格的值
        cell.setCellValue("Fengfeng");
    }*/

    ArrayList<Map<Integer,Object>> arrayList = word">new ArrayList<>();
    Map<Integer,Object> m = word">new HashMap<>();
    m.put(0,"物料ID");
    m.put(1,"物料编码");
    m.put(2,"名称");
    m.put(3,"编号");
    m.put(4,"规格");
    m.put(5,"单位");
    m.put(6,"单价");
    m.put(7,"数量");
    m.put(8,"厂家");
    m.put(9,"类别");
    arrayList.add(m);
    word">for (word">int i = 0; i <10 ; i++) {
        Map<Integer,Object> map = word">new HashMap<>();
        map.put(0,"materialID");
        map.put(1,"materialEncoding");
        map.put(2,"materialName");
        map.put(3,"materialModel");
        map.put(4,"materialSize");
        map.put(5,"unit");
        map.put(6,"price");
        map.put(7,"count");
        map.put(8,"manufacturers");
        map.put(9,"type");
        arrayList.add(map);
    }
    Cell cell;
    word">int size = arrayList.get(0).size();
    word">for (word">int i = 0;i < arrayList.size();i++){
        Row row = sheet.createRow(i);
        Map<Integer, Object> map1 = arrayList.get(i);
        word">for (word">int j = 0;j < size;j++){
            cell = row.createCell(j);
            cell.setCellValue((String) map1.get(j));
        }
    }

    // 保存Excel文件
    word">try {
        File file = word">new File(Path, "example.xlsx");
        FileOutputStream outputStream = word">new FileOutputStream(file);
        workbook.write(outputStream);
        outputStream.close();
        Toast.makeText(word">this, "Excel文件已创建", Toast.LENGTH_SHORT).show();
    } word">catch (IOException e) {
        e.printStackTrace();
    }
}


http://www.niftyadmin.cn/n/5165834.html

相关文章

python- os模块

一、文件与目录相关的操作 1、删除文件/文件夹 2、os.stat("path/filename"): 获取文件/目录信息的结构说明 3、os.listdir 和 os.path.join 在Python中&#xff0c;os.listdir()和os.path.join()是操作系统模块&#xff08;os模块&#xff09;的两个函数&#x…

python中circle函数的用法

python中circle函数的用法 circle函数简介语法说明代码说明 circle函数简介 Python中的circle函数用于绘制圆形&#xff0c;是Python中非常基础 和重要的函数之一 语法 turtle.circle(radius,extentNone,stepsNone)说明 第一个参数- 半径是 radius - radius 可以是负数 - r…

【经验模态分解】2.EMD的3个基本概念

/*** poject 经验模态分解及其衍生算法的研究及其在语音信号处理中的应用* file EMD的3个基本概念* author jUicE_g2R(qq:3406291309)* * language MATLAB/Python/C/C* EDA Base on matlabR2022b* editor Obsidian&#xff08;黑曜石笔记软件…

lvgl 转换和使用新字体

一、背景 如果lvgl 提供的默认字体不符合我们的显示要求&#xff0c;我们可以在网上下载开源字体&#xff0c;或者利用系统自带&#xff08;注意版权问题&#xff09;的字体文件转换lvgl 能识别和调用的字体。 或者为了压缩存储空间&#xff0c;某些字体我们只需要个别字符&…

php加密解密的用法(对称加密,非对称加密)

加密和摘要的区别 ***摘要&#xff1a;是从已知的数据中&#xff0c;通过摘要计算出一个值&#xff0c;一个数据对应一个或多个摘要的值 *** 比如&#xff1a;md5 和 sha1 sha256 hash 就是得到一个特定的值 &#xff0c;同一个数据得到的md5 是一样的&#xff0c;不会改变的 比…

构建自己的插件框架:第 4 部分

文章目录 一、为插件开发者提供的C++外观模式1、C++对象模型包装类2、性能到底有多糟?3、ActorBaseTemplate4、PluginHelper二、RPG游戏1、概念2、设计接口3、实现对象模型本系列文章来自 Building Your Own Plugin Framework, 主要内容是讨论使用C/C++ 语言开发跨平台的插…

oauth2的知识点

OAuth 2.0是一种授权框架&#xff0c;允许第三方应用程序获取访问资源的权限。它允许用户授权第三方应用程序访问其受保护的资源&#xff0c;而不必共享其凭据&#xff0c;例如用户名和密码。 OAuth 2.0有四种角色&#xff1a; 1. 资源所有者&#xff1a;拥有受保护的资源&am…

秒表计时器

/* 1、程序目的&#xff1a;使用定时器学习秒表计时&#xff0c;中断0控制走表&#xff0c;中断1控制清零 2、硬件要求&#xff1a;数码管、晶振12M */ #include <reg52.h> code unsigned char tab[] {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}…