Hey there! any question in your mind? Ask It Now!.

Popular Categories





how to write data in excel from java code?

+3 votes
355 views
asked in Programming by Eathen (1,930 points)

I want to store my results in excel sheet ,My results are in the form of string. actually there are some condition if they are true i am printing results in logger but want to store them in excel so that any one can see results. 

like : 

if(String1.equals(String2)){

System.out.println(String1 and String2 are same);

// here i want to store results in Excel.

}

I hope you got my question ...Thanks for help!

1 Answer

+2 votes
answered by jatin Expert (3,823 points)
selected by Eathen
 
Best answer

You can use this code its for writing excel from java and you can modify according to your need.

package test;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
public class finalExcel {
 
    public static void main(String[] args) throws HPSFException {
        ArrayList data = new ArrayList();
        ArrayList headers = new ArrayList();
        java.util.Date date= new java.util.Date();
        File file123 = new File("D:\\sample"+date.getTime()+".xls");
 
        headers.add("Srno.");
        headers.add("Email");
        headers.add("EN0");
 
        for (int i = 0; i <= 5; i++) {
            ArrayList cells = new ArrayList();
            randomstring str = new randomstring();
            randomstring str1 = new randomstring();
            String t = str.genString(5);
            String t1 = str1.genString(5);
            cells.add(t);
            cells.add(t1);
            cells.add("ENO" + i);
            data.add(cells);
        }
 
        exportToExcel("Test", headers, data, file123);
    }
 
    public static void exportToExcel(String sheetName, ArrayList headers,
            ArrayList data, File outputFile) throws HPSFException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(sheetName);
        
        int rowIdx = 0;
        short cellIdx = 0;
 
        // Header
        HSSFRow hssfHeader = sheet.createRow(rowIdx);
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (Iterator cells = headers.iterator(); cells.hasNext();) {
            HSSFCell hssfCell = hssfHeader.createCell(cellIdx++);
            hssfCell.setCellStyle(cellStyle);
            hssfCell.setCellValue((String) cells.next());
        }
        // Data
        rowIdx = 1;
        for (Iterator rows = data.iterator(); rows.hasNext();) {
            ArrayList row = (ArrayList) rows.next();
            HSSFRow hssfRow = sheet.createRow(rowIdx++);
            cellIdx = 0;
            for (Iterator cells = row.iterator(); cells.hasNext();) {
                HSSFCell hssfCell = hssfRow.createCell(cellIdx++);
                hssfCell.setCellValue((String) cells.next());
            }
        }
 
        
        
      //  wb.setSheetName(0, sheetName, HSSFWorkbook);
        try {
            FileOutputStream outs = new FileOutputStream(outputFile);
            wb.write(outs);
            outs.close();
        } catch (IOException e) {
            throw new HPSFException(e.getMessage());
        }
 
    }
}
 
Enjoy... :)
commented by Eathen (1,930 points)
Nice its really works, thanks finally I got the solution.
commented by krish Expert (6,310 points)
Thanks @jatin this code helped me also.

Related Questions

+1 vote
2 answers 4,992 views
–1 vote
0 answers 109 views
+3 votes
1 answer 626 views
+2 votes
2 answers 788 views
0 votes
0 answers 71 views
+1 vote
1 answer 81 views
0 votes
1 answer 114 views
+1 vote
1 answer 765 views
+8 votes
2 answers 106 views
0 votes
1 answer 241 views

Not a Member yet?

Ask to Folks Login

My Account
701 Folks are online
49 members and 652 guest online
Your feedback is highly appreciated