You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio考勤系统:如何获取Firestore集合单文档全部数据?

问题分析

你的代码存在几个核心逻辑错误,导致只显示第一个文档数据且重复填充所有行:

  • 循环嵌套顺序错误:创建每行时遍历所有Firestore文档,但每次都把arrayExport的前8个元素(第一个文档的数据)赋值给当前行,覆盖了后续文档的处理
  • 固定创建4行,和实际文档数量不匹配
  • 多余的arrayExport和arrayRemoveAll变量,不仅没用还导致数据混乱
  • 无意义的for j循环,cellCount未明确作用,反而增加了不必要的重复执行
修正后的代码
export.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        firebaseFirestore.collection("QR").document("QRScanned")
                .collection(LoginProfessorTabFragment.superName)
                .document(TeacherDash.subjectName1)
                .collection("Record of Attendance")
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
                    HSSFSheet hssfSheet = hssfWorkbook.createSheet(TeacherDash.subjectName1);

                    // 创建表头行
                    HSSFRow headerRow = hssfSheet.createRow(0);
                    String[] headers = {"姓名", "日期", "时间", "学号", "课程", "科目", "教室", "日程"};
                    for (int i = 0; i < headers.length; i++) {
                        headerRow.createCell(i).setCellValue(headers[i]);
                    }

                    // 遍历Firestore文档,每个文档对应一行数据
                    int rowIndex = 1; // 从第2行开始存数据(第1行是表头)
                    for (DocumentSnapshot documentSnapshot : task.getResult()) {
                        HSSFRow row = hssfSheet.createRow(rowIndex);
                        
                        // 直接从文档中取值并设置到对应单元格
                        row.createCell(0).setCellValue(documentSnapshot.getString("Name"));
                        row.createCell(1).setCellValue(documentSnapshot.getString("Date"));
                        row.createCell(2).setCellValue(documentSnapshot.getString("Time"));
                        row.createCell(3).setCellValue(documentSnapshot.getString("StudentNumber"));
                        row.createCell(4).setCellValue(documentSnapshot.getString("Course"));
                        row.createCell(5).setCellValue(documentSnapshot.getString("Subject"));
                        row.createCell(6).setCellValue(documentSnapshot.getString("Room"));
                        row.createCell(7).setCellValue(documentSnapshot.getString("Schedule"));
                        
                        rowIndex++;
                    }

                    // 写入Excel文件
                    try {
                        if (!filePath.exists()) {
                            filePath.createNewFile();
                            Toast.makeText(TeacherDash.this, "下载成功", Toast.LENGTH_SHORT).show();
                        }

                        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                        hssfWorkbook.write(fileOutputStream);

                        if (fileOutputStream != null) {
                            fileOutputStream.flush();
                            fileOutputStream.close();
                        }
                    } catch (Exception exception) {
                        exception.printStackTrace();
                        Toast.makeText(TeacherDash.this, "导出失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // 处理Firestore查询失败的情况
                    Toast.makeText(TeacherDash.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
});
关键修正点
  • 调整循环逻辑:直接遍历Firestore返回的QuerySnapshot,每个文档对应创建一行,自动匹配实际数据数量
  • 移除冗余变量:删除arrayExport和arrayRemoveAll,直接从DocumentSnapshot取值赋值,避免数据混乱
  • 添加表头:增加表头行,让Excel文件结构更清晰
  • 完善错误处理:补充Firestore查询失败和文件写入失败的提示,提升用户体验
  • 移除无效循环:删除无意义的for i和for j循环,简化代码逻辑

内容的提问来源于stack exchange,提问作者Janz Harvey Sacdalan Ortega

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:03:31