Outputdata in Excel not iterating properly in SpringBoot - java

So i am trying to read an excel file in springboot, the excel file contains 10 sheets, the code iterated all the sheets successfully but the row headers and cell data are not correct except for the first sheet ie the 2nd to last sheets are taking the first sheet information
Also the output are not well arranged, is there a way to make it clean
Below is the code
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
#RestController
#RequestMapping("datafile")
public class DataController {
#RequestMapping(value = "getdata", method = RequestMethod.GET)
public void createBus() throws IOException {
final String SAMPLE_XLSX_FILE_PATH = "C:\\project\\transita\\src\\main\\resources\\transita.xlsx";
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook;
{
try {
workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// 1. You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
}
Below is the output
1st sheet
=> partners
Iterating over Rows and Columns using Iterator
partner_id partner_code partner_name partner_logo partner_address partner_telephone partner_email partner_website External Agency Oportunity Search sell partner_id
001 ABC ABC Transport Km 5 MCC Uratta Rd, Owerri, Imo State -1111 2348139862090, 0700222872678 info#abctransport.com https://www.abctransport.com TRUE TRUE FALSE FALSE A4
002 CHT Chisco Transport Ltd 104, Funsho Williams Avenue, Iponri, Surulere, . 0816517669, 08089273799, 08113798985 Customercare#chiscogroupng.com https://www.chiscotransport.com.ng TRUE TRUE FALSE FALSE A5
003 LIB Libra Motors NIgeria Ltd Cele Okota Road
Lagos Nigeria 09031565022 info#libmot.com www.libmot.com TRUE TRUE FALSE FALSE A6
004 GIG GIGM Ltd 20 Ikorodu Express Road, Jibowu, Lagos. 08139851110 contact#gigm.com. https://gigm.com/ TRUE FALSE FALSE FALSE A7
005 GUO GUO Jibowu street along ikorodu express, Jibowu, Lagos. 2348144988273 info#guotransport.com https://www.guotransport.com TRUE FALSE FALSE FALSE A8
2nd sheet
=> p_policies
Iterating over Rows and Columns using Iterator
partner_id partner_code partner_name partner_logo partner_address partner_telephone partner_email partner_website External Agency Oportunity Search sell partner_id
001 ABC ABC Transport Km 5 MCC Uratta Rd, Owerri, Imo State -1111 2348139862090, 0700222872678 info#abctransport.com https://www.abctransport.com TRUE TRUE FALSE FALSE A4
002 CHT Chisco Transport Ltd 104, Funsho Williams Avenue, Iponri, Surulere. 0816517669, 08089273799, 08113798985 Customercare#chiscogroupng.com https://www.chiscotransport.com.ng TRUE TRUE FALSE FALSE A5
003 LIB Libra Motors NIgeria Ltd Cele Okota Road
Lagos Nigeria 09031565022 info#libmot.com www.libmot.com TRUE TRUE FALSE FALSE A6
004 GIG GIGM Ltd 20 Ikorodu Express Road, Jibowu, Lagos. 08139851110 contact#gigm.com. https://gigm.com/ TRUE FALSE FALSE FALSE A7
005 GUO GUO Jibowu street along ikorodu express, Jibowu, Lagos. 2348144988273 info#guotransport.com https://www.guotransport.com TRUE FALSE FALSE FALSE A8
3RD SHEET
=> schedules
Iterating over Rows and Columns using Iterator
partner_id partner_code partner_name partner_logo partner_address partner_telephone partner_email partner_website External Agency Oportunity Search sell partner_id
001 ABC ABC Transport Km 5 MCC Uratta Rd, Owerri, Imo State -1111 2348139862090, 0700222872678 info#abctransport.com https://www.abctransport.com TRUE TRUE FALSE FALSE A4
002 CHT Chisco Transport Ltd 104, Funsho Williams Avenue, Iponri, Surulere, L. 0816517669, 08089273799, 08113798985 Customercare#chiscogroupng.com https://www.chiscotransport.com.ng TRUE TRUE FALSE FALSE A5
003 LIB Libra Motors NIgeria Ltd Cele Okota Road
Lagos Nigeria 09031565022 info#libmot.com www.libmot.com TRUE TRUE FALSE FALSE A6
004 GIG GIGM Ltd 20 Ikorodu Express Road, Jibowu, Lagos. 08139851110 contact#gigm.com. https://gigm.com/ TRUE FALSE FALSE FALSE A7
005 GUO GUO Jibowu street along ikorodu express, Jibowu, Lagos. 2348144988273 info#guotransport.com https://www.guotransport.com TRUE FALSE FALSE FALSE A8

The problem is in the first portion of code you've posted, which is:
// ...other stuff...
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// ...other stuff...
You're using the sheet iterator properly, retriving a new Sheet from the iterator at the beginning of your while using the following line:
Sheet sheet = sheetIterator.next();
But then, mysteriously, you're overriding the Sheet the iterator provides you with the Sheet at index zero (which is the first one of your Workbook) with the indicted line of code:
sheet = workbook.getSheetAt(0);
So of course your while-loop is actually iterating through always the first Sheet of your Workbook (the Sheet at index zero). Remove that bad bad line and the problem is solved:
// ...other stuff...
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
// sheet = workbook.getSheetAt(0); <-- BAD, BAD LINE!
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// ...other stuff...

Related

Playwright - How to export the output data to Excel sheet (Java)

I tried Exporting the output data to Excel sheet but all the values are storing as a single cell.
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Playwright playwright = Playwright.create();
// select which browser we need to launch and set headless mode
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
page.navigate("https://datatables.net/extensions/select/examples/initialisation/checkbox.html");
for (int i = 1; i <= 10; i++) {
Locator row = page.locator("table#example tr").nth(i); //represents entire row
List<String>text = row.allInnerTexts();
text.removeAll(Arrays.asList("", null));
System.out.println(text.toString());
}
}
}
Output:
[ Airi Satou Accountant Tokyo 33 $162,700]
[ Angelica Ramos Chief Executive Officer (CEO) London 47 $1,200,000]
[ Ashton Cox Junior Technical Author San Francisco 66 $86,000]
[ Bradley Greer Software Engineer London 41 $132,000]
[ Brenden Wagner Software Engineer San Francisco 28 $206,850]
[ Brielle Williamson Integration Specialist New York 61 $372,000]
[ Bruno Nash Software Engineer London 38 $163,500]
[ Caesar Vance Pre-Sales Support New York 21 $106,450]
[ Cara Stevens Sales Assistant New York 46 $145,600]
[ Cedric Kelly Senior Javascript Developer Edinburgh 22 $433,060]
Instead of storing as a full value in a cell (Airi Satou Accountant Tokyo 33 $162,700), I need the values to be assigned to each column.
Make a for loop for the rows and then another one for columns
for (int i = 1; i <= 10; i++) {
System.out.println("Values for row " + i);
for (int z = 0; z <= 6; z++) {
Locator cell=page.locator("(//tbody)[1]/tr[i]/td[z]");
String text=cell.innerText();
System.out.println(text.toString());
}

Retrive textcontent by matching start word and end word

I am getting text file with contents like below. I want to retrieve the data present between start_word=Tax% and end_word="ErrorMessage".
ParsedText:
Tax%
63 2 .90 0.00 D INTENS SH 80ML(48) 9.00% 9.00%
23 34013090 0.0 DS PURE WHIT 1 COG (24) 9.00% 9.00%
"ErrorMessage":"","ErrorDetails":""
After retreiving the output would be
63 2 .90 0.00 D INTENS SH 80ML(48) 9.00% 9.00%
23 34013090 0.0 DS PURE WHIT 1 COG (24) 9.00% 9.00%
Please help.
I am using camel to read the text then i want to retrive the data to process further as per my requiement.
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class DataExtractor implements Processor{
#Override
public void process(Exchange exchange) throws Exception {
String textContent=(String) exchange.getIn().getBody();
System.out.println("TextContents >>>>>>"+textContent);
}
}
In the text content I am getting the content that i have given above.I need help regarding retreiving the the data in java.
Below is the code snippet to extract the desired output:
String[] strArr = textContent.split("\\r?\\n");
StringBuilder stringBuilder = new StringBuilder();
boolean appendLines = false;
for(String strLines : strArr) {
if(strLines.contains("Tax%")) {
appendLines = true;
continue;
}
if(strLines.contains("\"ErrorMessage\"")) {
break;
}
if(appendLines){
stringBuilder.append(strLines);
stringBuilder.append(System.getProperty("line.separator"));
}
}
textContent = stringBuilder.toString();

how can i use TestNg assertion method in for loop

i have 2 excel sheets, i am comparing cell values using testNg Assertion.
following are the values in sheet1 and sheet2
Sheet1 Sheet2
sachin sachin
david david
nancy nancy
winter winter
int rowCount = xlib.getRowCount("Sheet1");
int rowCount1 = xlib.getRowCount("Sheet2");
for (int i = 34; i<=rowCount;i++)
{
String compair1= xlib.getExcelData("Sheet1", i, 5);
System.out.println(compair1);
for (int j = 34; j<=rowCount1;j++)
{
String compair2=xlib.getExcelData("Sheet2", j, 5);
System.out.println(compair2);
Assert.assertEquals(compair1, compair2);
System.out.println("compared successfully");
}
}
}
}
Result:-
sachin
sachin
compared successfully
david
FAILED: Totalcompare
java.lang.AssertionError: expected [david] but found [sachin]
Expected result
Result:-
sachin
sachin
compared successfully
david
david
compared successfully
nancy
nancy
compared successfully
winter
winter
compared successfully
passed: Totalcompare

How to set PivotTable Field Number Format Cell with Apache POI

I'd like to set number format cell of pivot table Value field Sum of Balance as # ##0.
Pivot table created with code based on Official POI Sample CreatePivotTable
Code below do create and get CTPivotField pivotField. But how to set its number format?
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2);
CTPivotField pivotField = pivotTable
.getCTPivotTableDefinition()
.getPivotFields()
.getPivotFieldArray(2);
In MS Excel this is doing by next steps (see screenshot):
right click on Sum of Balance pivot table Value
select Field Settings
click Number...
set Format Cells
Help please with decide, advice or any idea.
Format of pivot table fields is setting by CTDataField.setNumFmtId(long numFmtId) for values and CTPivotField.setNumFmtId(long numFmtId) for columns & rows.
numFmtId is id number of format code. Available format codes are represented in Format cells list - Custom category:
Predefined format codes, thanks to Ji Zhou - MSFT, is here:
1 0
2 0.00
3 #,##0
4 #,##0.00
5 $#,##0_);($#,##0)
6 $#,##0_);[Red]($#,##0)
7 $#,##0.00_);($#,##0.00)
8 $#,##0.00_);[Red]($#,##0.00)
9 0%
10 0.00%
11 0.00E+00
12 # ?/?
13 # ??/??
14 m/d/yyyy
15 d-mmm-yy
16 d-mmm
17 mmm-yy
18 h:mm AM/PM
19 h:mm:ss AM/PM
20 h:mm
21 h:mm:ss
22 m/d/yyyy h:mm
37 #,##0_);(#,##0)
38 #,##0_);[Red](#,##0)
39 #,##0.00_);(#,##0.00)
40 #,##0.00_);[Red](#,##0.00)
45 mm:ss
46 [h]:mm:ss
47 mm:ss.0
48 ##0.0E+0
49 #
Full list of predefined format codes in MSDN NumberingFormat Class
Here is an example of applying format pivot table fields:
package ru.inkontext.poi;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFPivotTable;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataFields;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
public class CreatePivotTableSimple {
private static void setFormatPivotField(XSSFPivotTable pivotTable,
long fieldIndex,
Integer numFmtId) {
Optional.ofNullable(pivotTable
.getCTPivotTableDefinition()
.getPivotFields())
.map(pivotFields -> pivotFields
.getPivotFieldArray((int) fieldIndex))
.ifPresent(pivotField -> pivotField
.setNumFmtId(numFmtId));
}
private static void setFormatDataField(XSSFPivotTable pivotTable,
long fieldIndex,
long numFmtId) {
Optional.ofNullable(pivotTable
.getCTPivotTableDefinition()
.getDataFields())
.map(CTDataFields::getDataFieldList)
.map(List::stream)
.ifPresent(stream -> stream
.filter(dataField -> dataField.getFld() == fieldIndex)
.findFirst()
.ifPresent(dataField -> dataField.setNumFmtId(numFmtId)));
}
public static void main(String[] args) throws IOException, InvalidFormatException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
//Create some data to build the pivot table on
setCellData(sheet);
XSSFPivotTable pivotTable = sheet.createPivotTable(
new AreaReference("A1:C6", SpreadsheetVersion.EXCEL2007),
new CellReference("E3"));
pivotTable.addRowLabel(1); // set second column as 1-th level of rows
setFormatPivotField(pivotTable, 1, 9); //set format of row field numFmtId=9 0%
pivotTable.addRowLabel(0); // set first column as 2-th level of rows
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2); // Sum up the second column
setFormatDataField(pivotTable, 2, 3); //set format of value field numFmtId=3 # ##0
FileOutputStream fileOut = new FileOutputStream("stackoverflow-pivottable.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
private static void setCellData(XSSFSheet sheet) {
String[] names = {"Jane", "Tarzan", "Terk", "Kate", "Dmitry"};
Double[] percents = {0.25, 0.5, 0.75, 0.25, 0.5};
Integer[] balances = {107634, 554234, 10234, 22350, 15234};
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("Name");
row.createCell(1).setCellValue("Percents");
row.createCell(2).setCellValue("Balance");
for (int i = 0; i < names.length; i++) {
row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(names[i]);
row.createCell(1).setCellValue(percents[i]);
row.createCell(2).setCellValue(balances[i]);
}
}
}
https://github.com/stolbovd/PoiSamples

Read Excel file into R with XLConnect package from URL

There are lots of good examples out there on how to read Microsoft Excel files into R with the XLConnect package, but I can't find any examples of how to read in an Excel file directly from a URL. The reproducible example below returns a "FileNotFoundException (Java)". But, I know the file exists because I can pull it up directly by pasting the URL into a browser.
fname <- "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
sheet <- c("Sheet1")
data <- readWorksheetFromFile(fname, sheet, header=TRUE, startRow=11, startCol=2, endCol=13)
Although, the URL is prefixed with "https:" it is a public file that does not require a username or password.
I have tried to download the file first using download.file(fname, destfile="test.xls") and got a message that says it was downloaded but when I try to open it in Excel to check to see if it was successful i get a Excel popup box that says "..found unreadable content in 'test.xls'.
Below are the specifics of my system:
Computer: 64-bit Dell running
Operating System: Windows 7 Professional
R version: R-3.1.0
Any assistance would be greatly appreciated.
You can use RCurl to download the file:
library(RCurl)
library(XLConnect)
appURL <- "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
f = CFILE("exfile.xls", mode="wb")
curlPerform(url = appURL, writedata = f#ref, ssl.verifypeer = FALSE)
close(f)
out <- readWorksheetFromFile(file = "exfile.xls", sheet = "Sheet1", header = TRUE
, startRow = 11, startCol = 2, endCol = 15, endRow = 35)
> head(out)
Col1 EEI Col3 IESO MHEB Col6 PJM SOCO SWPP TVA WAUE Col12 Other Total
1 Hour 1 272 NA 768 1671 NA 148 200 -52 198 280 NA 700 4185
2 Hour 2 272 NA 769 1743 NA 598 200 -29 190 267 NA 706 4716
3 Hour 3 272 NA 769 1752 NA 598 200 -28 194 267 NA 710 4734
4 Hour 4 272 NA 769 1740 NA 598 200 -26 189 266 NA 714 4722
5 Hour 5 272 NA 769 1753 NA 554 200 -27 189 270 NA 713 4693
6 Hour 6 602 NA 769 1682 NA 218 200 -32 223 286 NA 714 4662
Two things:
Try using a different package--I know the gdata package's read.xls function has support for URLs
Try loading in a publicly-available xls file to make sure it's not an issue with the particular website.
For instance, you can try:
library("gdata")
site <- "http://www.econ.yale.edu/~shiller/data/chapt26.xls"
data <- read.xls(site, header=FALSE, skip=8)
head(data)
XLConnect does not support importing directly from URLs. You have to use e.g. download.file first to download the file to your local machine:
require(XLConnect)
tmp = tempfile(fileext = ".xls")
download.file(url = "http://www.econ.yale.edu/~shiller/data/chapt26.xls", destfile = tmp)
readWorksheetFromFile(file = tmp, sheet = "Data", header = FALSE, startRow = 9, endRow = 151)
or with your originally proposed URL:
require(XLConnect)
tmp = tempfile(fileext = ".xls")
download.file(url = "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls", destfile = tmp, method = "curl")
readWorksheetFromFile(file = tmp, sheet = "Sheet1", header = TRUE, startRow = 11, startCol = 2, endCol = 13)
library(relenium)
library(XML)
library(RCurl)
firefox=firefoxClass$new()
url="https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
url=sprintf(url)
firefox$get(url)
This will open a Firefox instance within R and ask you to download the file, which you could then open in the next line of code. I don't know of any R utilities that will open an excel spreadsheet from HTTPS.
You could then set a delay while you're saving the file and then read the sheet from your downloads folder:
Sys.sleep(10)
sheet <- c("Sheet1")
data <- readWorksheetFromFile(path, sheet, header=TRUE, startRow=11, startCol=2, endCol=13)

Categories

Resources