Запись строки в файл.xlsm

wef: написать в xlsm (Excel 2007), используя apache poi

Когда я пишу простую строку в файл, я не могу открыть файл. Ошибка - "Excel не может открыть файл" Test1.xlsm ", потому что формат файла или расширение файла не является допустимым"

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();
}

1 ответ

Мне пришлось использовать FileInputStream для чтения и FileOutputStream для записи. Оно работало завораживающе!! Запись набора записей в 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();
    }
Другие вопросы по тегам