how to write on specific column? - java

i have an excel file with 3 columns. i already store the "B" columns to array list and check it if the value is duplicate or not. now i have problem to write the "Duplicate" value to "C" columns. how to write on specific columns?
here is my code
FileInputStream file = new FileInputStream(new File(
"file name"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
ArrayList<String> col = new ArrayList<String>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
System.out.println(row.getRowNum());
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getColumnIndex()==1) {
col.add(cell.getStringCellValue());
System.out.print(cell.toString());
}
}
System.out.println();
}
for(int a = 0; a < 14; a++) {
if(col.get(a).equals("Order ID")) {
if(col.get(a).equals(col.get(a+1))) {
System.out.println("ROW no "+a+"Double Order");
}
} else {
if(col.get(a).equals(col.get(a+1)) || col.get(a).equals(col.get(a-1))) {
if(col.get(a).trim().length()>0) {
System.out.println("ROW no "+a+"Double Order");
col.add("Double");
}
}
}
}
FileOutputStream fileOut = new FileOutputStream("file name");
workbook.write(fileOut);
fileOut.close();

You do not need to iterate twice to identify and write against duplicate rows separately. You can do it like following:
private static void identifyDuplicateOrders() throws IOException {
FileInputStream file = new FileInputStream(new File("D:\\home\\test_in.xlsx"));
final FileOutputStream fileOut = new FileOutputStream("D:\\home\\test_out.xlsx");
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(file);
final XSSFSheet sheet = workbook.getSheetAt(0);
final Iterator<Row> rowIterator = sheet.iterator();
final Set<String> orderIds = new HashSet<String>();
while (rowIterator.hasNext()) {
final Row row = rowIterator.next();
final int rowNumber = row.getRowNum();
// SKIP HEADER
if (rowNumber == 0) {
continue;
}
System.out.print("Row " + rowNumber);
// GET ORDER ID CELL
final Cell cell = row.getCell(1);
if (!orderIds.add(cell.getStringCellValue())) {
// CREATE DOUBLE ORDER CELL
row.createCell(2).setCellValue("Duplicate");
System.out.println(" " + cell.toString() + " is Duplicate.");
} else {
System.out.println(" Order is Unique");
}
}
workbook.write(fileOut);
} finally {
workbook.close();
file.close();
fileOut.close();
}
}

You can write the word Duplicate in a loop iterating rows, you don't need to hold array of values. To check duplicates you can use Set and check if add returns false.
e.g.
public static void main(String... args) throws IOException {
try (FileInputStream file = new FileInputStream(new File(args[0]));
XSSFWorkbook workbook = new XSSFWorkbook(file)) {
Set<String> orders = new HashSet<>(20);
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
// skip header if needed
if (row.getRowNum() == 0) {
continue;
}
// 1 for OrderId as in example
Cell orderCell = row.getCell(1);
if (!orders.add(getValue(orderCell))) {
Cell infoCell = row.getCell(2);
if (infoCell == null) {
infoCell = row.createCell(2);
}
infoCell.setCellValue("Duplicate");
}
}
// write result in new file
workbook.write(new FileOutputStream(new File(args[0] + ".result.xlsx")));
}
}
private static String getValue(Cell orderCell) {
switch (orderCell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
return Double.toString(orderCell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return orderCell.getStringCellValue();
}
return null;
}

Related

Use Java to iterate between given placeholders in Excel

Here is an example of what i could like to do:
Is there a way to output all the cells between startprint and end_print, i've got like 3 sheet in excel with same place holder, and i want to view only the data between startprint and endprint, for now all i can do is print out all the 3 sheet with all the content, here is my code:
public class ReadInvoices {
private static final String NAME = "C:\\Users\\........\\excelfile.xlsx";
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File(NAME));
Workbook workbook = new XSSFWorkbook(file);
DataFormatter dataFormatter = new DataFormatter();
Iterator<Sheet> sheets = workbook.sheetIterator();
while(sheets.hasNext()) {
Sheet sh = sheets.next();
System.out.println("Sheet name is "+sh.getSheetName());
System.out.println("---------");
Iterator<Row> iterator = sh.iterator();
while(iterator.hasNext()) {
Row row = iterator.next();
Iterator<Cell> cellIterator = row.iterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue+"\t");
}
System.out.println();
}
}
workbook.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
I don't have a spreadsheet to test this code.
The processLines method tests for the start print row and the end print row. When we're in between the start print and end print rows, we print the rows.
I'm not sure if this line is correct. I'm trying to get the cell from column A.
String text = row.getColumnObject(0);
Edit: Maybe this will work.
Iterator<Cell> cellIterator = row.iterator();
Cell cell = cellIterator.next();
String text = dataFormatter.formatCellValue(cell);
Here's the example code.
public class ReadInvoices {
private static final String NAME = "C:\\Users\\.......\excelfilename.xlsx";
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File(NAME));
Workbook workbook = new XSSFWorkbook(file);
DataFormatter dataFormatter = new DataFormatter();
Iterator<Sheet> sheets = workbook.sheetIterator();
while (sheets.hasNext()) {
Sheet sh = sheets.next();
System.out.println("Sheet name is " + sh.getSheetName());
System.out.println("---------");
processLines(dataFormatter, sh);
}
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void processLines(DataFormatter dataFormatter, Sheet sh) {
boolean printLine = false;
Iterator<Row> iterator = sh.iterator();
while (iterator.hasNext()) {
Row row = iterator.next();
Iterator<Cell> cellIterator = row.iterator();
Cell cell = cellIterator.next();
String text = dataFormatter.formatCellValue(cell);
if (printLine && text.equals("End_print")) {
printLine = false;
}
if (printLine) {
printRow(dataFormatter, row);
}
if (!printLine && text.equals("StartPrint")) {
printLine = true;
}
}
}
private static void printRow(DataFormatter dataFormatter, Row row) {
Iterator<Cell> cellIterator = row.iterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
}

Excel to String xls/xlsx different result

I want to compare Excel files with each other just to see if they are the same or not. I can choose my Excel Files and Read them. I have 2 Excel Sheets with the same Content but one in .xls and on in .xlsx format.
I use the following Code to read my files (for xls with HSSFWorkbook and so on)
private String xlsx(File inputFile) {
String outputString = "";
// For storing data into String
StringBuffer data = new StringBuffer();
try {
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
data.append(cell + ";");
}
data.append("\n");
}
System.out.println(data.toString());
outputString = data.toString();
wBook.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
return outputString;
}
In my Excel I have blank cells - when i read them with xls I get DATA;;;;;DATA which is correct but When i Do the same in xlsx I get DATA;DATA
Somehow the Code skips empty cells?! How can I fix this Problem?
Thanks in Advance
After some more Google research and trying different things i found a solution to my Problem. The Iterator skips empty Cells because they have no value - they are null - however in a xls File it seems like they are not null - Whatever
My Code:
private String xlsx(File inputFile) {
String outputString = "";
System.out.println("start");
// For storing data into String
StringBuffer data = new StringBuffer();
try {
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
// Decide which rows to process
int rowStart = 0;
int rowEnd = sheet.getLastRowNum()+1;
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
int lastColumn = r.getLastCellNum();
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn);
if (c == null) {
data.append("" + ";");
} else {
data.append(c + ";");
}
}
data.append("\n");
}
System.out.println(data.toString());
outputString = data.toString();
wBook.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
System.out.println("end");
return outputString;
}

How do i write list data to existing xlsx file in particular column without overwriting using java

I have two 'xlsx' files 'sss.xlsx' and 'abc.xlsx'.
In 'sss.xlsx' file there are some Headers (columns only, doesn't contain any data) and in 'abc.xlsx' file there is data.
I wants to copy data from 'abc' file to 'sss' file column by column by checking column names. If column name matches then copy that column data to "sss.xlsx"
In same column (keep column as it is given in sss file).
I tried with below code. I am able to write but while writing header gets deleted. I am not getting where I went wrong.
Code:
public class Read {
private static int flag;
public static void main(String[] args) throws IOException{
//Create blank workbook
HSSFWorkbook workbook1 = new HSSFWorkbook();
//Create a blank sheet
HSSFSheet spreadsheet = workbook1.createSheet(
" Info ");
ArrayList<String> lst = new ArrayList<String>();
Read read = new Read();
File f2 = new File("sss.xlsx");
FileInputStream ios2 = new FileInputStream(f2);
XSSFWorkbook workbook2 = new XSSFWorkbook(ios2);
XSSFSheet sheet2 = workbook2.getSheetAt(0);
int noOfColumns = sheet2.getRow(0).getLastCellNum();
System.out.println(noOfColumns);
//String temp2=f.getSheet();
Iterator<Row> rowIterator = sheet2.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator <Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell1 = cellIterator.next();
String temp2=cell1.getStringCellValue().toString();
System.out.print(cell1.getStringCellValue() + "\n");
lst = read.extractExcelContentByColumnIndex(temp2);
File f3 = new File("sss.xlsx");
FileInputStream ios = new FileInputStream(f3);
XSSFWorkbook workbook3 = new XSSFWorkbook(ios);
String temp1=f3.getName();
//String colName="Mob No";
XSSFSheet sheet3 = workbook3.getSheetAt(0);
//String temp2=f.getSheet();
int columnIndex1=0;
Iterator<Row> rowIterator3 = sheet3.iterator();
//columndata = new ArrayList<String>();
while (rowIterator3.hasNext()) {
Row row3 = rowIterator3.next();
Iterator<Cell> cellIterator3 = row3.cellIterator();
while (cellIterator3.hasNext()) {
Cell cell3 = cellIterator3.next();
String temp5=cell3.getStringCellValue().toString();
//System.out.println(temp);
if(temp5.equals(temp2)){
columnIndex1 = cell1.getColumnIndex();
System.out.println(columnIndex1);
flag=1;
break;
}
}
if(flag==1)
{
flag = 0;
break;
}
}
while (rowIterator.hasNext()) {
Row row6 = rowIterator.next();
// System.out.println(row.getRowNum());
Iterator<Cell> cellIterator6 = row6.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > 0){ //To filter column headings
if(cell.getColumnIndex() == columnIndex1){// To match column index
// }
}
}
}
}
for(int RowNum=0; RowNum<lst.size();RowNum++){
HSSFRow row1 = spreadsheet.createRow(RowNum+1);
HSSFCell cell6 = row1.createCell(columnIndex1);
cell6.setCellValue(lst.get(RowNum).toString());
}
}
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("sss.xlsx"));
// System.out.println(lst);
workbook1.write(out);
out.close();
}
}
public ArrayList<String> extractExcelContentByColumnIndex( String colName)
{
ArrayList<String> columndata = null;
int columnIndex= 0;
int flag=0;
try {
File f = new File("abc.xlsx");
FileInputStream ios = new FileInputStream(f);
XSSFWorkbook workbook = new XSSFWorkbook(ios);
String temp1=f.getName();
XSSFSheet sheet = workbook.getSheetAt(0);
//String temp2=f.getSheet();
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<String>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell1 = cellIterator.next();
String temp=cell1.getStringCellValue().toString();
//System.out.println(temp);
if(temp.equals(colName)){
columnIndex=cell1.getColumnIndex();
flag=1;
break;
}
}
if(flag==1)
{
flag = 0;
break;
}
}
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// System.out.println(row.getRowNum());
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > -1){ //To filter column headings
if(cell.getColumnIndex() == columnIndex){// To match column index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columndata.add(cell.getNumericCellValue()+"");
break;
case Cell.CELL_TYPE_STRING:
columndata.add(cell.getStringCellValue());
break;
}
}
}
}
}
ios.close();
System.out.println(colName);
for(String ele : columndata)
{
System.out.println(ele);
}
} catch (Exception e) {
e.printStackTrace();
}
return columndata;
}
}
Thanks in advance!

Update excel file base on the output from textarea

My code imports the excel file then put a certain column in to a textarea
I want to know how would i modify the excel file if for example i edited something on the textarea that edit should be saved on the excel file
String excelFilePath = "sample.xlsx";
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(excelFilePath));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Iterator<Cell> scellIterator = nextRow.cellIterator();
cellIterator.next();
scellIterator.next();
scellIterator.next();
Cell topicsCell = cellIterator.next();
Cell topicSentimentCell =scellIterator.next();
String cellContents = topicsCell.getStringCellValue();
String scellContents = topicSentimentCell.getStringCellValue();
String[] topics = cellContents.split(";");
String[] topicSentiment = scellContents.split(";");
ArrayList<String> tpc = new ArrayList<>();
ArrayList<String> topicsents = new ArrayList<>();
for(int i = 0; i < topics.length; i++) {
topics[i] = topics[i].trim();
tpc.add(topics[i]);
for (int indx = 0; indx < tpc.size(); indx++) {
textArea.append(tpc.get(indx)+"\n");
}
}
for(int si = 0; si < topicSentiment.length; si++) {
topicSentiment[si] = topicSentiment[si].trim();
topicsents.add(topicSentiment[si]);
for (int index = 0; index < topicsents.size(); index++) {
// textArea.append(topicsents.get(index)+"\n");
System.out.print(topicsents+"\n");
}
}
}
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
Open a file as XSSFWorkbook
Open a XSSFSheet of the workbook
Make some changes in the sheet. For example change the value of a XSSFCell
Save the changes by workbook.write()
Example
// Open workbook and sheet
final XSSFWorkbook workbook = new XSSFWorkbook(new File("filename.xlsx"));
final XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate rows
final int firstRowNumber = sheet.getFirstRowNum();
final int lastRowNumber = sheet.getLastRowNum();
for (int rowNumber = firstRowNumber; rowNumber < lastRowNumber; rowNumber++ ) {
final XSSFRow row = sheet.getRow(rowNumber);
if (row == null) continue;
// Iterate columns
final int firstColumnNumber = row.getFirstCellNum();
final int lastColumnNumber = row.getLastCellNum();
for (int columnNumber = firstColumnNumber; columnNumber < lastColumnNumber; columnNumber++ ) {
final XSSFCell cell = row.getCell(firstColumnNumber);
if (cell == null) continue;
// Make some changes
cell.setCellValue("new Value");
}
}
// Save changes
workbook.write(new FileOutputStream("newFilename.xlsx"));

Unable to read all the cell values in a row when using Apache POI library

I am facing a problem while iterating all the cells in a row, I am able to read first 4 cells properly rest all coming as null. Any help appreciated
//Below is the code I am using
InputStream is = new FileInputStream(new File(fileName));
Workbook workbook = new XSSFWorkbook(is);
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheet = workbook.getSheetAt(i);
String name = sheet.getSheetName();
if (name.equals(sheetName)) {
Iterator<Row> rowItr = sheet.rowIterator();
while (rowItr.hasNext()) {
Row eachRow = rowItr.next();
QuestionObjBuilder builder = new QuestionObjBuilder();
Iterator<Cell> cellItr = eachRow.cellIterator();
int index = 1;
while (cellItr.hasNext()) {
Cell eachCell = cellItr.next();
int cellType = eachCell.getCellType();
String label = "";
if (cellType == Cell.CELL_TYPE_STRING) {
label = eachCell.getStringCellValue();
} else if (cellType == Cell.CELL_TYPE_NUMERIC) {
int value = (int) eachCell.getNumericCellValue();
label = String.valueOf(value);
}
builder = builder.set(index, label);
index++;
if (index > 10) {
break;
}
}
}
}
}
workbook.close();
If your goal is to read all cells from the excel you can use this code,
FileInputStream fileInputStream = new FileInputStream(fileName);
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheet(sheetName);
Iterator<Row> it = worksheet.rowIterator();
while(it.hasNext()){
HSSFRow r = (HSSFRow) it.next();
Iterator<Cell> it1=r.cellIterator();
while(it1.hasNext()){
HSSFCell cell = (HSSFCell)it1.next();
System.out.println("Row: "+cell.getRowIndex()+" ,Column: "+cell.getColumnIndex());
System.out.println(cell);
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources