2016-07-20 2 views
1

Я использую следующий код для записи данных в файл Excel:Невозможно записать Excel файл с помощью Apache POI

public class WriteExcellFile { 

    public boolean saveExcelFile(Context context, String fileName) { 

     // check if available and not read only 
     if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
      Log.d("FileUtils", "Storage not available or read only"); 
      return false; 
     } 

     boolean success = false; 

     //New Workbook 
     Workbook wb = new HSSFWorkbook(); 

     Cell c = null; 

     //Cell style for header row 
     CellStyle cs = wb.createCellStyle(); 
     cs.setFillForegroundColor(HSSFColor.LIME.index); 
     cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 

     //New Sheet 
     Sheet sheet1 = null; 
     sheet1 = wb.createSheet("myOrder"); 

     // Generate column headings 
     Row row = sheet1.createRow(0); 

     c = row.createCell(0); 
     c.setCellValue("Item Number"); 
     c.setCellStyle(cs); 

     c = row.createCell(1); 
     c.setCellValue("Quantity"); 
     c.setCellStyle(cs); 

     c = row.createCell(2); 
     c.setCellValue("Price"); 
     c.setCellStyle(cs); 

     sheet1.setColumnWidth(0, (15 * 500)); 
     sheet1.setColumnWidth(1, (15 * 500)); 
     sheet1.setColumnWidth(2, (15 * 500)); 

     // Create a path where we will place our List of objects on external storage 
     File file = new File(context.getExternalFilesDir(null), fileName); 
     FileOutputStream os = null; 

     try { 
      os = new FileOutputStream(file); 
      wb.write(os); 
      Log.d("FileUtils", "Writing file" + file); 
      success = true; 
     } catch (IOException e) { 
      Log.d("FileUtils", "Error writing " + file, e); 
     } catch (Exception e) { 
      Log.d("FileUtils", "Failed to save file", e); 
     } finally { 
      try { 
       if (null != os) 
        os.close(); 
      } catch (Exception ex) { 
      } 
     } 

     return success; 
    } 


    public static boolean isExternalStorageReadOnly() { 
     String extStorageState = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { 
      return true; 
     } 
     return false; 
    } 

    public static boolean isExternalStorageAvailable() { 
     String extStorageState = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { 
      return true; 
     } 
     return false; 
    } 
} 

код работает без каких-либо ошибок на LogCat, но когда я иду в папку, где это было предположительно, напишите файл excel, папка пуста. Я также добавил разрешение в файле манифеста Android.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

Каков путь к этой папке? И как вы к этому идете? – greenapps

+0

Возвращает ли функция true? – greenapps

ответ

0
File file = new File(context.getExternalFilesDir(null), fileName); 

В строке выше, если вы замените нуль с Environment.DIRECTORY_DOCUMENTS, то ваш выходной файл будет храниться там.

Смежные вопросы