2016-09-12 3 views
-1

WEF:ファイル

私は、ファイルへの単純な文字列を書きwrite to xlsm (Excel 2007) using apache poiが、私はファイルを開くことができないのです。エラー -

try { 

    Workbook workbook; 
    workbook = new XSSFWorkbook(OPCPackage.open("C:\\temp\\Test.xlsm")); 

    String content = "This is the text content"; 
    byte[] contentInBytes = content.getBytes(); 

    FileOutputStream out = new FileOutputStream("C:\\temp\\Test1.xlsm"); 
    out.write(contentInBytes); 

    workbook.write(out); 
    out.close(); 

    System.out.println("xlsm created successfully.."); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (InvalidFormatException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

あなたは 'out.write()'とは何と思います、なぜそう思いますか?つまり、Excel以外のバイトをファイルの先頭に書き込んでいて、そのファイルをExcelで読み込めると期待していますか?なぜそれを期待していますか? Excelの文書のどこにそのテキストがあると思いますか?セルA1?もしそうなら、あなたはそれがあなたがそれを望む場所に指定していないときに、それをなぜ期待しますか? – Andreas

答えて

0

私が読みとのFileOutputStreamを書くためのFileInputStreamを使用していた「ファイルフォーマットやファイルの拡張子が有効でないため、Excelがファイル 『Test1.xlsm』を開くことができません」。それは魅力のように働いた!! xlsmにレコードセットを書き込む

try { 
     File file = new File("C://temp//test.xslm"); 
     FileInputStream inputStream = new FileInputStream(file); 
     Workbook wb = new XSSFWorkbook(OPCPackage.open(inputStream)); 

     Sheet sheet = wb.getSheet("Sheet1"); 
     for (int x = 1; x < 5; x++) { 
      for (int y = 0; y < 4; y++) { 
       Cell c = sheet.getRow(x).getCell(y); 
       c.setCellValue(recs.getValueAt(x - 1, y).toString()); 
      } 
     } 

     FileOutputStream fileOut = new FileOutputStream("C://temp//test.xslm"); 
     wb.write(fileOut); 
     fileOut.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (InvalidFormatException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
関連する問題