Hello with this code i just get the first line in my database
i need to gett all line please what a should to do
I'm using Apache POI to read an Excel document. and i need to stock all data in my database but with this code i just save the first line please what i should to do !
how to do read all line in my excel thanks
#PostMapping("/upload")
public ResponseEntity<?> addRfp (#RequestParam("file") MultipartFile file) throws IOException, InvalidFormatException {
ZipSecureFile.setMinInflateRatio(0);
FileInputStream fis = (FileInputStream) file.getInputStream();
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
final DataFormatter df = new DataFormatter();
Rfp rfp = new Rfp();
for(int i =0 ; i < mySheet.getPhysicalNumberOfRows() ; i++)
{
Row nextRow = mySheet.getRow(row.getRowNum() + 1);
if (nextRow != null)
{
System.out.println(df.formatCellValue(row.getCell(3)));
rfp.setIde(df.formatCellValue(row.getCell(0)));
rfp.setUnit(df.formatCellValue(row.getCell(1)));
rfp.setRFPID(df.formatCellValue(row.getCell(2)));
rfp.setREQUESTID(df.formatCellValue(row.getCell(3)));
rfp.setINCOMING(df.formatCellValue(row.getCell(4)));
rfp.setPROJECTTITLE(df.formatCellValue(row.getCell(5)));
rfp.setSERVICELINE(df.formatCellValue(row.getCell(6)));
rfp.setBUSINESSUNIT(df.formatCellValue(row.getCell(7)));
rfp.setREQUSITIONBUSINESSUNIT(df.formatCellValue(row.getCell(8)));
rfp.setCOUNTRY(df.formatCellValue(row.getCell(9)));
rfp.setTARGETPROPOSE(df.formatCellValue(row.getCell(10)));
rfp.setRFX(df.formatCellValue(row.getCell(11)));
rfp.setWORKFLOWTRACKING(df.formatCellValue(row.getCell(12)));
rfp.setPROTOCOL(df.formatCellValue(row.getCell(13)));
rfp.setJOURFIXKUNDE(df.formatCellValue(row.getCell(14)));
rfp.setREJECTEDSTATUS(df.formatCellValue(row.getCell(15)));
rfp.setCLOSEDREASON(df.formatCellValue(row.getCell(16)));
rfp.setPRACTICE(df.formatCellValue(row.getCell(17)));
rfp.setSTARTDATE(df.formatCellValue(row.getCell(18)));
rfp.setENDDATE(df.formatCellValue(row.getCell(19)));
rfp.setVERTRAGSART(df.formatCellValue(row.getCell(20)));
rfp.setRFPCOMMENT(df.formatCellValue(row.getCell(21)));
rfp.setPERSONDAYS(df.formatCellValue(row.getCell(22)));
rfp.setACCOUNTEXECUTIVE(df.formatCellValue(row.getCell(23)));
rfp.setACCOUNTSALESMANAGER(df.formatCellValue(row.getCell(24)));
rfp.setEXPERTSALES(df.formatCellValue(row.getCell(25)));
rfp.setDELIVERYUNIT(df.formatCellValue(row.getCell(26)));
rfp.setDELIVERYRESPONSIBLE(df.formatCellValue(row.getCell(27)));
rfp.setREQUISITIONER(df.formatCellValue(row.getCell(28)));
rfp.setPROCUREMENT(df.formatCellValue(row.getCell(29)));
rfp.setREFERENCENUMBER(df.formatCellValue(row.getCell(30)));
rfp.setATOSCONTRACTNO(df.formatCellValue(row.getCell(31)));
rfp.setCANDIDATES(df.formatCellValue(row.getCell(32)));
rfp.setITEC(df.formatCellValue(row.getCell(33)));
rfp.setTCV(df.formatCellValue(row.getCell(34)));
rfp.setUPDATED(df.formatCellValue(row.getCell(35)));
rfp.setUPDATEDBY(df.formatCellValue(row.getCell(36)));
rfp.setPORTFOLIO(df.formatCellValue(row.getCell(37)));
rfp.setPATH(df.formatCellValue(row.getCell(38)));
rfp.setRMPROTOCOL(df.formatCellValue(row.getCell(39)));
rfp.setRM(df.formatCellValue(row.getCell(40)));
rfp.setWFM_ID(df.formatCellValue(row.getCell(41)));
rfp.setNEEDCONFOPART(df.formatCellValue(row.getCell(42)));
rfp.setCONFIRMATIONOFPARTICIPATION(df.formatCellValue(row.getCell(43)));
rfp.setPROPOSE(df.formatCellValue(row.getCell(44)));
rfp.setLASTCALL(df.formatCellValue(row.getCell(45)));
rfp.setLASTCALLDATE(df.formatCellValue(row.getCell(46)));
rfp.setSKILLS(df.formatCellValue(row.getCell(47)));
rfp.setLEISTUNGSERBRINGUNG(df.formatCellValue(row.getCell(48)));
rfp.setROLLEFUNKTION(df.formatCellValue(row.getCell(49)));
rfp.setINHALTSCOPE(df.formatCellValue(row.getCell(50)));
rfp.setLAUFZEITPERSPEKTIVE(df.formatCellValue(row.getCell(51)));
rfp.setLAUFZEITANGEFRAGT(df.formatCellValue(row.getCell(52)));
rfp.setPHASE(df.formatCellValue(row.getCell(53)));
rfp.setFRISTVERLAENGERUNG(df.formatCellValue(row.getCell(54)));
rfp.setCLOSEDSTATUS(df.formatCellValue(row.getCell(55)));
rfp.setDEFERRED_TILL(df.formatCellValue(row.getCell(56)));
rfp.setSIPA_YN(df.formatCellValue(row.getCell(57)));
rfp.setPO_NO(df.formatCellValue(row.getCell(58)));
rfp.setNESSIE_NO(df.formatCellValue(row.getCell(59)));
rfp.setNUMBER_OF_PERSONS(df.formatCellValue(row.getCell(60)));
rfp.setATT_JOB_SITE(df.formatCellValue(row.getCell(61)));
rfp.setATT_LANGUAGE(df.formatCellValue(row.getCell(62)));
rfp.setATT_MAIN_TOPICS(df.formatCellValue(row.getCell(63)));
rfp.setREF_RFPID(df.formatCellValue(row.getCell(64)));
rfp.setREF_HISTORY(df.formatCellValue(row.getCell(65)));
rfp.setREF_REJECTIONS(df.formatCellValue(row.getCell(66)));
rfp.setRV_REJ_STATUS_DETAILS(df.formatCellValue(row.getCell(67)));
rfp.setRV_ALLIANCE_MANAGER(df.formatCellValue(row.getCell(68)));
rfp.setRV_FINAL_STATUS(df.formatCellValue(row.getCell(69)));
rfp.setRV_CUST_LIST_DATE(df.formatCellValue(row.getCell(70)));
rfp.setRV_FINAL_NOTE(df.formatCellValue(row.getCell(71)));
rfp.setRV_ATOS_ROOT_CAUSES(df.formatCellValue(row.getCell(72)));
rfp.setCURRENCY(df.formatCellValue(row.getCell(73)));
rfp.setEXTERNALID(df.formatCellValue(row.getCell(74)));
rfp.setRESULTSTATUS(df.formatCellValue(row.getCell(75)));
rfp.setREASONCODE(df.formatCellValue(row.getCell(76)));
rfp.setSYSID(df.formatCellValue(row.getCell(77)));
rfp.setNCRM_COMMENT(df.formatCellValue(row.getCell(78)));
rfp.setIDEMAND_COMMENT(df.formatCellValue(row.getCell(79)));
rfp.setOFFSHORE_COMMENT(df.formatCellValue(row.getCell(80)));
rfp.setRV_RC_UPDBY(df.formatCellValue(row.getCell(81)));
rfp.setRV_RC_DATE(df.formatCellValue(row.getCell(82)));
rfp.setRV_FINALCOMMENT(df.formatCellValue(row.getCell(83)));
rfp.setRV_FINALDATE(df.formatCellValue(row.getCell(84)));
rfp.setTCV_EURO(df.formatCellValue(row.getCell(85)));
rfp.setREM_ACTIVE(df.formatCellValue(row.getCell(86)));
rfp.setREM_SEND(df.formatCellValue(row.getCell(87)));
rfp.setREMCOP_ACTIVE(df.formatCellValue(row.getCell(88)));
rfp.setREMCOP_SEND(df.formatCellValue(row.getCell(89)));
rfp.setIDEMAND(df.formatCellValue(row.getCell(90)));
rfp.setIDEMAND_URL(df.formatCellValue(row.getCell(91)));
rfp.setLABOUR_LEASING_YN(df.formatCellValue(row.getCell(92)));
rfp.setCAS_NO(df.formatCellValue(row.getCell(93)));
rfp.setPROLONGATION(df.formatCellValue(row.getCell(94)));
rfp.setRV_SKILLS_MISS_1(df.formatCellValue(row.getCell(95)));
rfp.setRV_SKILLS_MISS_2(df.formatCellValue(row.getCell(96)));
rfp.setRV_SKILLS_MISS_3(df.formatCellValue(row.getCell(97)));
rfp.setREF_RENEWAL_DATE(df.formatCellValue(row.getCell(98)));
rfprepository.save(rfp);
}
}}
}
return null;
}}
I use to get all rows this way.
FileInputStream fis = (FileInputStream) file.getInputStream();
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
for(Row row : mySheet){
final DataFormatter df = new DataFormatter();
System.out.println(df.formatCellValue(row.getCell(0)));
}
Related
I'm new to Apache POI and HttpClient, and I'm confused. My program is supposed to get the value from an excel cell, and use that value in the HttpGet request. Here is the method that isn't working:
public String censusIdValue(int rowNumber) throws IOException {
InputStream ExcelFileToRead = new FileInputStream("/Users/john/Documents/Censusplaces (1).xlsx");
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFWorkbook test = new XSSFWorkbook();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
String value = wb.getSheetAt(0).getRow(rowNumber).getCell(3);
return value;
}
wb.getSheetAt(0).getRow(rowNumber).getCell(3); is type XSSFCell, and I need to convert it to a string. Is this possible or did I go about pulling this value incorrectly?
public void getvalFromEcelSheet(){
File file= new File("pass here path of directry");
FileInput input=new FileInput(file);
XSSFWorkBook book=new XSSFWorkBook(input);
XSSFSheet sheet=book.getSheet("Sheet1");
XSSFRow row1=sheet.getRow(1);
XSSFCell cell=row1.getCell(1);
String value=cell.toString();
//we got String Value from Excelsheet
}
Simple fix.
String value =
wb.getSheetAt(0).getRow(rowNumber).getCell(3).getStringCellValue();
I need to parse over an excel sheet and retrieve values from each row to store it in database. Currently I am doing it based on the type of values that each cell holds. This is ok in the current case as I have to deal with only 2 columns. But I have a new requirement to parse an excel sheet that holds more than 12 columns. How can it be done in this case? Is there a way I could iterate each row based on column if I am using a structured table with table headers?
My current code is as follows.
File file = new File(UPLOAD_LOCATION + fileUpload.getFile().getOriginalFilename());
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
// getCellTypeEnum shown as deprecated for version 3.15
// getCellTypeEnum ill be renamed to getCellType starting
// from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + "--");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + "--");
}
}
I am using the following external apache API imports:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Is there a way I can do the same passing in the name of column headers?
Please help.
Thanks in advance.
based on the comments
InputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
ArrayList colsList=new ArrayList();
colsList.add("Col1");
colsList.add("Col2");
colsList.add("Col3");
colsList.add("Col4");
Sheet datatypeSheet = workbook.getSheetAt(0);
int numOfRows=datatypeSheet.getLastRowNum();
for(int rowNum=0;rowNum<numOfRows;rowNum++){
Row row=datatypeSheet.getRow(rowNum);
int numOfCellPerRow=row.getLastCellNum();
for(int cellNum=0;cellNum<numOfCellPerRow;cellNum++){
if(colsList.contains(row.getCell(rowNum).getStringCellValue())){
Cell cell=row.getCell(cellNum)
System.out.println("Cell No:"+cellNum+" value is:
"+cell.getStringCellValue())
}
}
System.out.println("This is a new Row");
}
I am trying to read column values from excel file which looks like this
This is my code for reading excel file
Xls_Reader d = new Xls_Reader("C:\\TestData.xlsx");
System.out.println(d.getRowCount("sheetname"));
String s1 = String.valueOf(d.getCellData("TC03", "Contract_ID",3));
s1 =(int)Double.parseDouble(s1) + "";
System.out.println(s1);
in this code i am able to get record of one row. But i need to print all the Row value.
Please suggest.
What Jars you are using for this approach?
I have following code to read data from excel using apache poi -
public static void main(String args[]) throws IOException
{
FileInputStream fis = new FileInputStream(new File("your_file.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis); // XSSFWorkbook for .xlsx file
XSSFSheet sheet = workbook.getSheetAt(0); // open sheet 1
Iterator<Row> rowIterator = sheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
if(row.getRowNum()!=0) // skip title row
{
Iterator cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = (Cell) cellIterator.next();
System.out.print(cell.getStringCellValue() + "\t");
}
}
}
}
Note :- In my case cell in excel sheet formatted as text you can change cell.getStringCellValue() method as per your data.
This method will be print all sheet of your excel document:
public static void GetEmail() throws IOException{
InputStream in = new FileInputStream("C:/path to your doc");
HSSFWorkbook wb = new HSSFWorkbook(in);
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(false); // Read formulas
extractor.setIncludeSheetNames(false);
String text = extractor.getText();
System.out.println(text);
}
I am very new to java. I am creating pList from excel. My excel file contains multiple sheets. I want to iterate through all the sheets of the excel file. How to to this? please help.
public static void main( String [] args ) {
try {
InputStream input = POIExample.class.getResourceAsStream( "qa.xls" );
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
HSSFSheet sheet = wb.getSheetAt(i);
// Do your stuff
}
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
From the apache poi documentation we see there is also an iterator available, which is in my opinion a cleaner solution:
Iterator<Sheet> sheetIterator = workbook.iterator();
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
}
Depending on the type of workbook you use (HSSF or XSSF) you might need to execute an additional cast operation:
HSSF: POI Project's pure Java implementation of the Excel '97(-2007) file format.
HSSFSheet sheet = (HSSFSheet) sheetIterator.next();
XSSF: POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
XSSFSheet sheet = (XSSFSheet) sheetIterator.next();
Java:
Workbook workbook = WorkbookFactory.create(file);
Iterator<Sheet> sheetIterator;
sheetIterator = workbook.sheetIterator();
while(sheetIterator.hasNext()){
Sheet sheet = sheetIterator.next();
out.println(sheet.getSheetName());
}
Scala:
var iterator = workbook.sheetIterator();
while(iterator.hasNext){
var sheet = iterator.next
println(sheet)
}
InputStream inp = new FileInputStream("workbook.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);
String text="";
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
// Do something here
ExcelExtractor extractor = new ExcelExtractor(wb);
text=extractor.getText();
System.out.print("text="+text);
}
}
sheet1.shiftRows(0,sheet1.getLastRowNum(),1);
I am not getting the result in my excel sheet after the shiftrows is called. the data stays the same. Where am I doing it wrong?
This is because you're workbook is only open for reading since it came from an inputstream
InputStream inp = new FileInputStream("workbook.xls");
To get the result you would have to write the new workbook object out to a new xsl spreadsheet. Something like...
File outWB = new File("workbook2.xls");
OutputStream out = new FileOutputStream(outWB);
wb.write(out);
out.flush();
out.close();
You could do this to the same file but you would need to inp.close() first.