Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. public boolean setCellData(String sheetName,String colName,int rowNum, String data){
  2. try{
  3. fis = new FileInputStream(path);
  4. workbook = new XSSFWorkbook(fis);
  5.  
  6. if(rowNum<=0)
  7. return false;
  8.  
  9. int index = workbook.getSheetIndex(sheetName);
  10. int colNum=-1;
  11. if(index==-1)
  12. return false;
  13.  
  14.  
  15. sheet = workbook.getSheetAt(index);
  16.  
  17.  
  18. row=sheet.getRow(0);
  19. for(int i=0;i<row.getLastCellNum();i++){
  20. //System.out.println(row.getCell(i).getStringCellValue().trim());
  21. if(row.getCell(i).getStringCellValue().trim().equals(colName))
  22. colNum=i;
  23. }
  24. if(colNum==-1)
  25. return false;
  26.  
  27. sheet.autoSizeColumn(colNum);
  28. row = sheet.getRow(rowNum-1);
  29. if (row == null)
  30. row = sheet.createRow(rowNum-1);
  31.  
  32. cell = row.getCell(colNum);
  33. if (cell == null)
  34. cell = row.createCell(colNum);
  35.  
  36.  
  37. cell.setCellValue(data);
  38.  
  39. fileOut = new FileOutputStream(path);
  40.  
  41. workbook.write(fileOut);
  42.  
  43. fileOut.close();
  44.  
  45. }
  46. catch(Exception e){
  47. e.printStackTrace();
  48. return false;
  49. }
  50. return true;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement