|
代码如下,做了Flush之类的操作依然不行:
- public static void exportEquipBindToExcel(List<EquipBind> equipBindList, String filePath,Context context) {
- try (HSSFWorkbook workbook = new HSSFWorkbook ();
- FileOutputStream fileOut = new FileOutputStream(filePath)) {
- Sheet sheet = workbook.createSheet("设备数据");
- String yearMonth=getCurrentYearMonth();
- // 创建标题行
- Row headerRow = sheet.createRow(0);
- String[] headers = {"a", "b"};
- for (int i = 0; i < headers.length; i++) {
- Cell cell = headerRow.createCell(i);
- cell.setCellValue(headers[i]);
- }
- Map<Integer, String> equipTypeMap = new HashMap<>();
- EquipTypeDao equipTypeDao=new EquipTypeDao(context);
- equipTypeDao.open();
- List<EquipType> equipTypeList=equipTypeDao.getEquipTypes("",new String[]{},"");
- for (EquipType equipType : equipTypeList) {
- long id = equipType.getId();
- String equipTypeName = equipType.getEquipTypeName();
- // 将 Id 和 EquipTypeName 存储到 Map 中
- equipTypeMap.put((int)id, equipTypeName);
- }
- // 填充数据
- for (int i = 0; i < equipBindList.size(); i++) {
- EquipBind equipBind = equipBindList.get(i);
- Row row = sheet.createRow(i + 1); // 创建新行,注意标题行已经占用了第一行
- row.createCell(0).setCellValue(i+1);
- row.createCell(1).setCellValue(equipBind.getSecondaryNumber());
- }
- equipTypeDao.close();
- workbook.write(fileOut);
- fileOut.flush();
- fileOut.getFD().sync(); // 强制刷新文件系统缓存
- fileOut.close();
- workbook.close();
- MediaScannerConnection.scanFile(context, new String[] { filePath}, null, null);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
copycode
|
|