In case you are using JTable, refer to below code:
private int getColumnByName(JTable table, String name) {
for (int i = 0; i < table.getColumnCount(); ++i)
if (table.getColumnName(i).equals(name))
return i;
return -1;
}
Then you can use the following to set & get cell values :
table.setValueAt(value, rowIndex, getColumnByName(table, colName));
table.getValueAt(rowIndex, getColumnByName(table, colName));
If you are using POI, refer to below code:
InputStream inp = new FileInputStream("workbook.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
You can modify above code as per your requirement to get particular row and column.
Reference Links :
http://www.coderanch.com/t/537168/java/java/Writing-existing-excel-xls-file
How to get/set the cell value of a JTable by its column name
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…