SHARE
TWEET

Untitled

a guest Jun 18th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ExcelTest
  2.  {
  3. public FileInputStream fis = null;
  4. public FileOutputStream fos = null;
  5. public XSSFWorkbook workbook = null;
  6. public XSSFSheet sheet = null;
  7. public XSSFRow row = null;
  8. public XSSFCell cell = null;
  9. String xlFilePath;
  10. boolean isEmptyStringCell;
  11.  
  12. public ExcelTest(String xlFilePath) throws Exception
  13. {
  14.     this.xlFilePath = xlFilePath;
  15.     fis = new FileInputStream(xlFilePath);
  16.     workbook = new XSSFWorkbook(fis);
  17.     fis.close();
  18. }
  19.  
  20. public void setCellData(String sheetName, String colName, int rowNum, String value)
  21. {
  22.     try
  23.     {
  24.         int col_Num = -1;
  25.         sheet = workbook.getSheet(sheetName);
  26.  
  27.         row = sheet.getRow(0);
  28.         for (int i = 0; i < row.getLastCellNum(); i++)
  29.         {
  30.             if(row.getCell(i).getStringCellValue().trim().equals(colName))
  31.             {
  32.                 col_Num = i;
  33.             }
  34.         }
  35.  
  36.         sheet.autoSizeColumn(col_Num);
  37.  
  38.         for(int j=2; j<7; j++)
  39.             {
  40.             row = sheet.getRow(j - 1);
  41.             if(row==null)
  42.                 row = sheet.createRow(j - 1);
  43.  
  44.             cell = row.getCell(col_Num);
  45.  
  46.             isEmptyStringCell=cell.getStringCellValue().trim().isEmpty();
  47.  
  48.             if (this.isEmptyStringCell)
  49.             {
  50.                 cell = row.createCell(col_Num);
  51.                 cell.setCellValue(value);
  52.                 break;
  53.             }
  54.             else
  55.             {
  56.                 j=j+1;
  57.             }
  58.  
  59.             }
  60.  
  61.         /*row = sheet.getRow(rowNum - 1);
  62.         if(row==null)
  63.             row = sheet.createRow(rowNum - 1);
  64.  
  65.         cell = row.getCell(col_Num);
  66.         if(cell == null)
  67.             cell = row.createCell(col_Num);
  68.  
  69.         cell.setCellValue(value);*/
  70.  
  71.         System.out.println("The cell value is "+cell.getStringCellValue());
  72.  
  73.         fos = new FileOutputStream(xlFilePath);
  74.         workbook.write(fos);
  75.         fos.close();
  76.     }
  77.     catch (Exception ex)
  78.     {
  79.         ex.printStackTrace();
  80.  
  81.     }
  82.    }
  83.      
  84. for(int j=2; j<7; j++)
  85.             {
  86.             row = sheet.getRow(j - 1);
  87.             if(row==null)
  88.                 row = sheet.createRow(j - 1);
  89.  
  90.             cell = row.getCell(col_Num);
  91.  
  92.             isEmptyStringCell=cell.getStringCellValue().trim().isEmpty();
  93.  
  94.             if (this.isEmptyStringCell)
  95.             {
  96.                 cell = row.createCell(col_Num);
  97.                 cell.setCellValue(value);
  98.                 break;
  99.             }
  100.             else
  101.             {
  102.                 j=j+1;
  103.             }
  104.  
  105.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top