Мне нужно прочитать три столбца как Name
, Empid
и empcode
из Excel, вставить в текстовое поле и сохранить запись, после чего будут сгенерированы поля setid
& reqid
.Теперь сохраните setid
и reqid
после Name
, Empid
и empcode
в том же Excel.Я пробовал любой код из Интернета, но мало что работает.
Ниже код для написания в Excel
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static FileInputStream fs;
public static FileOutputStream out;
public void WriteExcelFile()
{
String str1 = ackgreqid1.getText();
File file = new File(System.getProperty("user.dir") + "/src/main/java/com/testdata/FreeTestData.xlsx");
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
wb = new XSSFWorkbook(fs);
sheet = wb.getSheet("TestData");
} catch (IOException e) {
System.out.println("unable to find workbook or worksheet");
e.printStackTrace();
}
Iterator<Row> sheetrow = sheet.rowIterator();
// Row firstRow = sheetrow.next();
// int rowCount = sheet.getLastRowNum();
// = firstRow.cellIterator();
// Iterator<Cell> cell = firstRow.cellIterator();
while (sheetrow.hasNext()) {
Row value = sheetrow.next();
// System.out.println(value);
Iterator<Cell> cell = value.cellIterator();
Cell cellValue = cell.next();
String cellData = cellValue.getStringCellValue();
while (cell.hasNext()) {
if (cellData.equalsIgnoreCase("Step-Up")) {
int lastCellNum = value.getLastCellNum();
// System.out.println(lastCellNum);
Cell createCell = value.createCell(lastCellNum);
createCell.setCellValue(str1);
try {
out = new FileOutputStream(file);
wb.write(out);
// cell.next();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
break;
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
break;
}
}
