upload excel file validation in java - java

i am new for upload Excel file POI API so i need to validate duplicate cell in particular column . example like
String dupcolumn = myRow.getcell(0);
Iterator iter = new Iterator();
while(iter.hesnext())
{
myRow = (Row) iter.next();
dupcolumn.contains(iter.toString());
}
Above code cannot support to read in particular column , but always continuous reading column .

This code may helpful to you.
InputStream xlsStream = excelFileUpload.getInputstream();
XSSFWorkbook wb = new XSSFWorkbook(xlsStream);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rows=sheet.iterator();
//need to keep retrieved values in a collection to check duplicates.
Set<String> values = new HashSet<String>();
//check all rows in excel sheet
while(rows.hasNext()){
//get next row
XSSFRow row =(XSSFRow)rows.next();
//pass '0' means first cell (column) in current row. if you need to get other cell value, you can pass relevant cell number instead of '0'.
XSSFCell cell=row.getCell(0);
if(values.contains(cell.getStringCellValue())){
//duplicated value
}else{
values.add(cell.getStringCellValue());
}
}

Related

How to add columns to an existing large excel file using SXSSF Apache POI?

I am working with a large excel file ( larger than 40 Mb , more than 100k rows and 50 columns ). I am successfully reading it using POI ( 3.10.1 version ) event stream and then doing some calculation and storing result into a List.
Now I have to append this List as a column in the same file. In this part I am facing issue.
I have tried to achieve this by using the below code
FileInputStream excelFile = new FileInputStream(new File(pathToFile));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0); // Get first sheet
Iterator<Row> iterator = datatypeSheet.iterator();
int i=0;
while (iterator.hasNext()) { // Loop over each row
Row currentRow = iterator.next();
Cell cell = currentRow.createCell(currentRow.getLastCellNum());
cell.setCellType(Cell.CELL_TYPE_STRING);
if(currentRow.getRowNum() == 0)
cell.setCellValue("OUTPUT-COLUMN"); // set column header for the new column
else {
cell.setCellValue(list.get(i)); // list contains the output to populate in new column
i++;
}
}
FileOutputStream fos = new FileOutputStream(new File(pathToOutput));
workbook.write(fos);
fos.close();
It is working fine with smaller files But the issue is that I am getting Out of memory for the larger files. Now I tried to modify this and use SXSSF in place of XSFF to get over the memory issue (See below code). But while testing even for smaller files I am getting output file same as the input file.
FileInputStream excelFile = new FileInputStream(new File(pathToFile));
XSSFWorkbook xwb = new XSSFWorkbook(inputStream);
inputStream.close();
SXSSFWorkbook wb = new SXSSFWorkbook(xwb,100);
wb.setCompressTempFiles(true);
SXSSFSheet sh = (SXSSFSheet) wb.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
int i=0;
while (iterator.hasNext()) { // Loop over each row
Row currentRow = iterator.next();
Cell cell = currentRow.createCell(currentRow.getLastCellNum());
cell.setCellType(Cell.CELL_TYPE_STRING);
if(currentRow.getRowNum() == 0)
cell.setCellValue("OUTPUT-COLUMN"); // set column header for the new column
else {
cell.setCellValue(list.get(i)); // list contains the output to populate in new column
i++;
}
}
FileOutputStream fos = new FileOutputStream(new File(pathToOutput));
wb.write(fos);
fos.close();
Using a db is not suitable in my use case and i want to avoid using a temporary data structure to hold data for writing due to memory constraint.
Is there a way to write in output workbook while streaming ? Here is the code that I am using to read using POI Streaming API
private class ExcelData implements SheetContentsHandler {
LinkedHashMap<Strin, String> rowMap;
public void startRow(int rowNum) {
}
public void endRow(int rowNum) {
// Process the row
// Handle write to output workbook ??
}
public void cell(String cellReference, String formattedValue,
XSSFComment comment) {
// Save current row in rowMap ( column name => cell value )
}
public void headerFooter(String text, boolean isHeader, String tagName)
{
}
}
It is not possible to add column to existing workbook using POI SXSSF. It only allows addition of new rows.
The only solution is to read the existing workbook and write to a new workbook with the added column.
To achieve this we can store the rows in a data structure or database in the endrow() method and then use the persisted data to write a new workbook.

In Java ,how to use org.apache.poi.hssf and get the value of two columns alone with the column name from the excel sheet that is uploaded

Hi I am trying to get the values of two columns with the column name. I have uploaded an excel sheet and I have to fetch the values of this two columns and then save it in DB. Can someone help me with this
Include the latest POI jar
FileInputStream inputStream = ew FileInputStream(" Excel Path ");
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet workSheet = workbook.getSheet(0); // put your sheet number
Iterator<Row> rowIterator = workSheet.iterator();
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Cell cell = nextRow.getCell("col name");
String cellData = cell.getStringCellValue();}

How to iterate over current row in an excel sheet using column name?

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");
}

Reading excel files .xlsx via Java

So my excel file is relatively small in size. It contains 8 sheets. Each sheet has "records" of data which i need to read. Each sheet also has the first row reserved for headers which i skip; so my data will begin from the 2nd row (1st index) of each sheet and end on the last record.
So, below is my code to iterate through the sheets and read each row however it fails to read each sheet. And i can't seem to figure out why. Please have look and any suggestions will be appreciated.
Thanks!
FileInputStream fis = new FileInputStream(new File(filePath));
XSSFWorkbook wb = new XSSFWorkbook(fis);
DataFormatter formatter = new DataFormatter();
//iterate over sheets
for (int i=0; i<NUM_OF_SHEETS; i++) {
sheet = wb.getSheetAt(i);
sheetName = sheet.getSheetName();
//iterate over rows
for (int j=1; j<=lastRow; j++) { //1st row or 0-index of each sheet is reserved for the headings which i do not need.
row = sheet.getRow(j);
if (row!=null) {
data[j-1][0] = sheetName; //1st column or 0th-index of each record in my 2d array is reserved for the sheet's name.
//iterate over cells
for (int k=0; k<NUM_OF_COLUMNS; k++) {
cell = row.getCell(k, XSSFRow.RETURN_BLANK_AS_NULL);
cellValue = formatter.formatCellValue(cell); //convert cell to type String
data[j-1][k+1] = cellValue;
}//end of cell iteration
}
}//end of row iteration
}//end of sheet iteration
wb.close();
fis.close();
At least there is one big logical error. Since you are putting the data of all sheets in one array, this array must be dimensioned like:
String[][] data = new String[lastRow*NUM_OF_SHEETS][NUM_OF_COLUMNS+1];
And then the allocations must be like:
...
data[(j-1)+(i*lastRow)][0] = sheetName; //1st column or 0th-index of each record in my 2d array is reserved for the sheet's name.
...
and
...
data[(j-1)+(i*lastRow)][k+1] = cellValue;
...
With your code, the allocations from second sheet will overwrite the ones from the first sheet, since j starts with 1 for every sheet.

How to write on a null cell using java?

I am currently doing a program in Java that will run several tests and generate a report right after in excel. I was able to read and write through excel and the results of Passed or Failed are displayed in Results column. I was able to write these in excel but by supplying a default value on the cell (e.g. default) so the code will just overwrite it. I would like to write a comment on the Comment column, but I do not know how to write in a null cell. Here is a screenshot of the report I am generating (the link of the image available) and the code for reading and writing in excel as well.
FileInputStream fileInputStream = new FileInputStream("ReportExcel.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheetAt(0);
fileInput csvInputFile = new fileInput();
String[] sReturnValue = csvInputFile.arrayReturnSingleValue("fileInput.csv");
if (prodnameresult.equals(prodname) ){
Pass++;
totalResult++;
System.out.println ("Testcase1: Branding-Customised Product Name is PASSED");
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Passed");
}
else{
Fail++;
totalResult++;
System.out.println("Testcase1: Branding-Customised Product Name is FAILED");
//assertEquals(prodnameresult.equals(prodname), true);
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Failed");
}
//Harold's Input
FileOutputStream os = new FileOutputStream("ReportExcel.xls");
workbook.write(os);
os.close();
}
Report:
http://i47.tinypic.com/2s1lsfc.png
I don't have access to the report, but to answer to your question, if your cell is null you should create it and not get it as following:
HSSFCell cell = row.getCell((short) 4);
if(cell == null)
cell = row.createCell((short) 4);
cell.setCellValue("Passed");
You should maybe re-apply the Style to the Cell if it had a different one from the row.

Categories

Resources