i managed to write the data to a single column. My req is to write the data to the next adjacent column without overriding the data in the fist column. Any help?
InputStream inp;
try {
inp = new FileInputStream("D:\\test.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = null;
Cell c = null;
int rowNum=0;
Set<String> keySet = new HashSet<String>();
keySet = tushMap.keySet();
for (Entry<String, String> entry : tushMap.entrySet()) {
row=sheet.createRow((short) (sheet.getLastRowNum() + 1));
System.out.println((short) (sheet.getLastRowNum() + 1));
c = row.createCell(0);
c.setCellValue("fff");
System.out.println("fff");
System.out.println(entry.getKey() + "/" + entry.getValue());
rowNum++;
}
FileOutputStream fileOut = new FileOutputStream("D:\\test.xls");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This is the code written to populate the 1st column. Populating the second column overrides the fist 1.. Pls suggest some solution.. Thanks in advance.
To get the data side by side get the last row outside the loop and save it as the begening row. Keep a counter for which row you are editing inside the loop. If the row is null create the row.
rowPos = sheet.getLastRowNum();
for (Entry<String, String> entry : tushMap.entrySet()) {
rowPos++;
Row currentRow = sheet.getRow(rowPos);
if(null == currentRow)
currentRow = createRow(rowPos);
EDIT:
I am getting the desierd output using this
int beginingRow = sheet.getLastRowNum();
for( int col=0; col<2; col++){
int currentRow = beginingRow;
for (int i=0; i<10; i++) {
currentRow++;
row = sheet.getRow(currentRow);
if(null == row)
row=sheet.createRow(currentRow);
c = row.createCell(col);
c.setCellValue("fff");
System.out.println("fff");
//System.out.println(entry.getKey() + "/" + entry.getValue());
rowNum++;
}
}
Output
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.