Reputation: 21
I want to write to data which contains ArrayList of String to excel using java. When i am doing only the first data only writing to excel.remaining data is not.
public class ExcelGenerator {
public static void main(String args[]) throws FileNotFoundException, HPSFException, IOException, SQLException, NestableException
{
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("14");
arrlist.add("7");
arrlist.add("39");
arrlist.add("40");
/* For Loop for iterating ArrayList */
System.out.println("For Loop");
for (int counter = 0; counter < arrlist.size(); counter++) {
System.out.println(arrlist.get(counter));
}
HSSFWorkbook workbook=new HSSFWorkbook();
File f=new File("D:/Test/test.xls");
GeericExcelGenerator gl=new GeericExcelGenerator("Test",workbook);
FileOutputStream outputStream=new FileOutputStream(f);
gl.generate(outputStream,arrlist);
}
public void generate(OutputStream outputStream,ArrayList arrlist) throws SQLException, IOException, NestableException
{
try
{
int currentRow = 0;
for (int counter = 0; counter < arrlist.size(); counter++) {
HSSFRow row = sheet.createRow(currentRow);
System.out.println("Description is"+arrlist.get(counter));
String c=(String) arrlist.get(counter);
int i=0;
HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
cell.setCellValue(c);
i++;
workbook.write(outputStream);
currentRow++;
currentRow++;
}
}catch(IOException e)
{}
finally {
outputStream.close();
}
}
}
Upvotes: 1
Views: 7947
Reputation: 186
Try below solution may due to your initialization "i" variable
public void generate(OutputStream outputStream, ArrayList arrlist)
throws SQLException, IOException, NestableException {
try {
int currentRow = 0;
int i = 0;
for (int counter = 0; counter < arrlist.size(); counter++) {
HSSFRow row = sheet.createRow(currentRow);
System.out.println("Description is" + arrlist.get(counter));
String c = (String) arrlist.get(counter);
HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
cell.setCellValue(c);
i++;
workbook.write(outputStream);
currentRow++;
currentRow++;
}
} catch (IOException e) {
} finally {
outputStream.close();
}
}
Or you can check your implmentation with below example https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java
Upvotes: 3