Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
private static Workbook wb;
private static FileOutputStream fos;
private static FileInputStream fis;
private static org.apache.poi.ss.usermodel.Sheet sh;
private static Row row;
private static Cell cell;
public static void main(String[] args) throws Exception {
fis = new FileInputStream("./javabook.xlsx");
wb = WorkbookFactory.create(fis);
sh = wb.getSheet("Sheet1");
row =sh.getRow(1);
cell = row.createCell(0);
cell.setCellValue("ibrahim1");
fos = new FileOutputStream("./javabook.xlsx");
wb.write(fos);
fos.close();
System.out.println("Done");
It worked for the first time and the text "ibrahim01" has appeared on the cell However when i ran it the second time i got this error:-
Exception in thread "main" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:177)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:149)
at package1.ClassA1.main(ClassA1.java:25)
picture of error when i open the file(javabook.xlsx)
enter image description here
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ClassA1 {
private static XSSFWorkbook wb;
private static FileOutputStream fos;
private static XSSFSheet sh;
private static Row row;
private static Cell cell;
public static void main(String[] args) throws Exception {
wb = new XSSFWorkbook();
sh = wb.createSheet("Sheet1");
row = sh.createRow(0);
cell = row.createCell(0);
cell.setCellValue("ibrahim1");
fos = new FileOutputStream("./javabook.xlsx");
wb.write(fos);
fos.close();
System.out.println("Done");
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.