Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create appendable excelsheet using java

I want to create an append-able excel sheet. Like i have four columns stream1 stream2 stream3 stream4

first time i am inserting data only first column(stream1) after that i want to full fill other columns one by one.

this is the code which i am using:

 public void createFile(Jqueue stream1, Jqueue stream2, Jqueue stream3, Jqueue stream4) {
    try {

        String filename = "path";
        boolean alreadyExists = (new File(filename)).exists();

        HSSFRow rowhead = sheet1.createRow((short) 0);
        rowhead.createCell((short) 0).setCellValue("Stream 1");
        rowhead.createCell((short) 1).setCellValue("Stream 2");
        rowhead.createCell((short) 2).setCellValue("Stream 3");
        rowhead.createCell((short) 3).setCellValue("Stream 4");


        int i = 1;
        while (!stream1.isEmpty()) {
            String urlstream1 = "";
            String urlstream2 = "";
            String urlstream3 = "";
            String urlstream4 = "";
            HSSFRow row = sheet1.createRow((short) i);
            try {
                if (stream1.size() > 0) {
                    urlstream1 = stream1.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            try {
                if (stream2.size() > 0) {
                    urlstream2 = stream2.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            try {
                if (stream3.size() > 0) {
                    urlstream3 = stream3.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            try {
                if (stream4.size() > 0) {
                    urlstream4 = stream4.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            row.createCell((short) 0).setCellValue(urlstream1);
            row.createCell((short) 1).setCellValue(urlstream2);
            row.createCell((short) 2).setCellValue(urlstream3);
            row.createCell((short) 3).setCellValue(urlstream4);
            i++;
        }

        FileOutputStream fileOut = new FileOutputStream(filename);
        hwb.write(fileOut);
        fileOut.close();

But this is not append-able code. its inserting data row by row.

thx in advanced.

like image 467
narendra Avatar asked Jun 30 '12 07:06

narendra


People also ask

Can you automate Excel with Java?

Especially when working with loads of data, Excel makes it pretty easy to order, filter, calculate, visualize and report. Expanding into loads^2 of data and adding automation on top is also relatively simple: just couple Excel with Java to do the hard work of mining databases and delivering clean sheets.


1 Answers

I suppose a slight change in your code should do it.

Try replacing

int i = 1;

with the following:

int i = sheet1.getLastRowNum() + 1;

You would also need to change your implementation for reading and writing files a bit.

like image 66
Abhishek Avatar answered Oct 15 '22 06:10

Abhishek