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

一添加依赖(一定要用新版依赖防止一些bug)

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

二,创建方法

java 复制代码
private void createExcelFile(String Path) {
    // 创建工作簿
    Workbook workbook = 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 = new ArrayList<>();
    Map<Integer,Object> m = 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);
    for (int i = 0; i <10 ; i++) {
        Map<Integer,Object> map = 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;
    int size = arrayList.get(0).size();
    for (int i = 0;i < arrayList.size();i++){
        Row row = sheet.createRow(i);
        Map<Integer, Object> map1 = arrayList.get(i);
        for (int j = 0;j < size;j++){
            cell = row.createCell(j);
            cell.setCellValue((String) map1.get(j));
        }
    }

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

三,如果有表追加的做法(这里使用的是room数据库导入的)

java 复制代码
public class DbConvertExcel {
    public static void appendToExcelFile(Context context, String filePath) {
        // 工作表名称
        String sheetName = "test";

        testDao testDao = testDatabase.getDatabaseInstance(context).getLaserMachDao();
        try {
            File file = new File(filePath, "test.xlsx");
            XSSFWorkbook workbook;
            Sheet sheet;
            if (file.exists()) {
      /*          FileInputStream inputStream = new FileInputStream(file);
                OPCPackage opc = OPCPackage.open(inputStream);
                workbook = new XSSFWorkbook(opc);
                sheet = workbook.getSheet(sheetName);
                opc.close();  // 关闭OPCPackage*/
                FileInputStream inputStream = new FileInputStream(file);
                workbook = new XSSFWorkbook(inputStream);
                sheet = workbook.getSheet(sheetName);
            } else {
                workbook = new XSSFWorkbook();
                sheet = workbook.createSheet(sheetName);
                // 添加标题行
                Row titleRow = sheet.createRow(0);
                titleRow.createCell(0).setCellValue("编号");
                titleRow.createCell(1).setCellValue("名称");
                titleRow.createCell(2).setCellValue("类型");
            }

            int lastRowNum = sheet.getLastRowNum();
            List<test> all = testDao.getAll();
            for (test test: all) {
                Row row = sheet.createRow(lastRowNum + 1); // 从最后一行的下一行开始写入
                row.createCell(0).setCellValue(String.valueOf(test.getId()));
                row.createCell(1).setCellValue(test.getName());
                row.createCell(2).setCellValue(String.valueOf(test.getHandType()));
                lastRowNum++;
            }

            FileOutputStream outputStream = new FileOutputStream(file);
            workbook.write(outputStream);
            outputStream.close();
            Toast.makeText(context, "数据已追加到Excel文件", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("===========", e.toString());
        }
    }
}
相关推荐
LAM LAB1 小时前
【VBA/word】批量替换字体大小
开发语言·c#·word
CYRUS STUDIO1 小时前
FART 主动调用组件设计和源码分析
android·逆向·源码分析·fart·脱壳
半青年3 小时前
Qt读取Excel文件的技术实现与最佳实践
c语言·c++·python·qt·c#·excel
DreamNotOver4 小时前
Word中英文引号替换为中文
word·替换·中英文引号
nuclear20116 小时前
使用Python将 Excel 中的图表、形状和其他元素导出为图片
python·excel·将excel图表转换为图片·将excel文本框转换为图片
zimoyin6 小时前
kotlin Android AccessibilityService 无障碍入门
android·开发语言·kotlin
三少的笔记6 小时前
Windows中PDF TXT Excel Word PPT等Office文件在预览窗格无法预览的终级解决方法大全
pdf·word·excel
pengone17 小时前
PHP8.0版本导出excel失败
php·excel
韩仔搭建17 小时前
第二章:安卓端启动流程详解与疑难杂症调试手册
android·ui·娱乐
A-花开堪折17 小时前
Android7 Input(七)App与input系统服务建立连接
android