Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExcelTest
- {
- public FileInputStream fis = null;
- public FileOutputStream fos = null;
- public XSSFWorkbook workbook = null;
- public XSSFSheet sheet = null;
- public XSSFRow row = null;
- public XSSFCell cell = null;
- String xlFilePath;
- boolean isEmptyStringCell;
- public ExcelTest(String xlFilePath) throws Exception
- {
- this.xlFilePath = xlFilePath;
- fis = new FileInputStream(xlFilePath);
- workbook = new XSSFWorkbook(fis);
- fis.close();
- }
- public void setCellData(String sheetName, String colName, int rowNum, String value)
- {
- try
- {
- int col_Num = -1;
- sheet = workbook.getSheet(sheetName);
- row = sheet.getRow(0);
- for (int i = 0; i < row.getLastCellNum(); i++)
- {
- if(row.getCell(i).getStringCellValue().trim().equals(colName))
- {
- col_Num = i;
- }
- }
- sheet.autoSizeColumn(col_Num);
- for(int j=2; j<7; j++)
- {
- row = sheet.getRow(j - 1);
- if(row==null)
- row = sheet.createRow(j - 1);
- cell = row.getCell(col_Num);
- isEmptyStringCell=cell.getStringCellValue().trim().isEmpty();
- if (this.isEmptyStringCell)
- {
- cell = row.createCell(col_Num);
- cell.setCellValue(value);
- break;
- }
- else
- {
- j=j+1;
- }
- }
- /*row = sheet.getRow(rowNum - 1);
- if(row==null)
- row = sheet.createRow(rowNum - 1);
- cell = row.getCell(col_Num);
- if(cell == null)
- cell = row.createCell(col_Num);
- cell.setCellValue(value);*/
- System.out.println("The cell value is "+cell.getStringCellValue());
- fos = new FileOutputStream(xlFilePath);
- workbook.write(fos);
- fos.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- for(int j=2; j<7; j++)
- {
- row = sheet.getRow(j - 1);
- if(row==null)
- row = sheet.createRow(j - 1);
- cell = row.getCell(col_Num);
- isEmptyStringCell=cell.getStringCellValue().trim().isEmpty();
- if (this.isEmptyStringCell)
- {
- cell = row.createCell(col_Num);
- cell.setCellValue(value);
- break;
- }
- else
- {
- j=j+1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement