Created
August 1, 2017 15:46
-
-
Save erajanraja24/35fef4946c8fd95f4beb77e1cfe89a9d to your computer and use it in GitHub Desktop.
Excel Write to write to an Excel sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package come.ExcelReadWrite; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import org.apache.poi.ss.usermodel.Cell; | |
import org.apache.poi.xssf.usermodel.XSSFRow; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
public class ExcelWrite { | |
public static void setvalue(int colno,int rowno,String sheetName,String path,String textValue) throws IOException { | |
try { | |
FileInputStream file = new FileInputStream(path); | |
XSSFWorkbook workbook = new XSSFWorkbook(file); | |
XSSFSheet sheet = workbook.getSheet(sheetName); | |
Cell cell = null; | |
//Retrieve the row and check for null | |
XSSFRow sheetrow = sheet.getRow(rowno); | |
if(sheetrow == null){ | |
sheetrow = sheet.createRow(rowno); | |
} | |
//Update the value of cell | |
cell = sheetrow.getCell(colno); | |
if(cell == null){ | |
cell = sheetrow.createCell(colno); | |
} | |
cell.setCellValue(textValue); | |
file.close(); | |
FileOutputStream outFile =new FileOutputStream(new File("Upload_Sequence.xlsx")); | |
workbook.write(outFile); | |
outFile.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment