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();
Related
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)));
}
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);
}
import java.io.*;
import jxl.*;
import jxl.write.*;
class Exc
{
public static void main(String args[])
{
InputStream input = new FileInputStream("sample.xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
Workbook wb = WorkbookFactory.create(input);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(10);
if (cell == null)
cell = row.createCell(10);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("Data");
FileOutputStream fileOut = new FileOutputStream("sample.xls");
wb.write(fileOut);
fileOut.close();
}
}
This is simple code where I am just replacing the data in a cell. When i execute it I get this error:
Exc.java:13: error: cannot find symbol Workbook wb = WorkbookFactory.create(input);
What does the error mean? I have placed the jxl.jar file in the correct path and executed. Thanks in advance.
WorkbookFactory is not a JExcel API class. It belongs to Apache POI.
So, instead of this
InputStream input = new FileInputStream("sample.xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
Workbook wb = WorkbookFactory.create(input);
Use JExcel API's Workbook.getWorkbook() method as
Workbook wb = Workbook.getWorkbook(new File("sample.xls"));
We have next situation: our system has data export in xls format, this is huge file with many rows and columns.
And after user download and open document he see document scrolled to last column and last Spreadsheet tab. This is very annoying, better to set focus on first tab and first cell. I did simple test code to see how it works:
public class SelectionTest {
public static String file = "/usr/test/poi.test/src/main/resources/test";
#Test
public void test() throws FileNotFoundException, IOException {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet s = wb.getSheetAt(0);
s.setActive(true);
HSSFRow row = s.getRow(0);
HSSFCell cell = row.getCell(0);
cell.setAsActiveCell();
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
}
}
And this doesn't works.
Here is working solution, have found this here
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet s = wb.getSheetAt(0);
wb.setActiveSheet(0);
s.showInPane(0, 0);
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
This worked for me:
sheet.setActiveCell(new CellAddress(0, 0));
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.