I have to find the example string in excel sheet as I have to change it using java. I did it easily. But the problem comes when I have to do it same for specific times and each time I have to copy the data into new sheet. So if I have to do it for 5 times then there should be 5 worksheet in my excel workbok.
Here is my code.
int a=1;
String match="Keval Dalsaniya";
String[] name={"Ankit","Keval","Varun","Dhaval","Nirav"};
int i;
try {
FileInputStream file = new FileInputStream(new File("D:\\Ankit\\Data\\data.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
for(i=0;a<=name.length;i++,a++)
{
Cell cell = null;
cell = sheet.getRow(0).getCell(4);
workbook.createSheet(name[i]);
if(cell.getStringCellValue().equals(match))
cell.setCellValue(name[i]);
else
JOptionPane.showMessageDialog(null, "No match found.");
System.out.println(name[i]);
}
file.close();
FileOutputStream outFile =new FileOutputStream(new File("D:\\Ankit\\Data\\update.xlsx"));
workbook.write(outFile);
outFile.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Use
XSSFSheet newSheet = workbook.createSheet();
and put edited String in the newly created sheet.
Hope this helps.
Edit:
You are creating a new sheet but not using it.
Try the following.
XSSFSheet sheet = workbook.getSheetAt(0);
for (i = 0; a <= name.length; i++,a++)
{
Cell cell = null;
cell = sheet.getRow(0).getCell(4);
XSSFSheet newSheet = workbook.createSheet(name[i]);
// createRow and createCell should come to rescue here.
Cell cellInNewSheet = newSheet.getRow(0).getCell(4);
if (cell.getStringCellValue().equals(match)) {
cellInNewSheet.setCellValue(name[i]);
}
else {
JOptionPane.showMessageDialog(null, "No match found.");
}
System.out.println(name[i]);
}
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.