Not able to highlight text in excel in java - java

My scenario is to get text from "Status" column and based on text comparison ,I want to highlight that particular cell.Below is my code-
private static void colorSheet(String xlsxFileAddress)
{
try
{
FileInputStream file = new FileInputStream(new File(xlsxFileAddress));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFFormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
XSSFSheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type after eveluating formulae
//If it is formula cell, it will be evaluated otherwise no change will happen
switch (evaluator.evaluateInCell(cell).getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "tt");
break;
case Cell.CELL_TYPE_STRING:
{
System.out.print(cell.getStringCellValue() + "tt");
ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL,"Extra Rows in new table");
org.apache.poi.ss.usermodel.PatternFormatting fill2 = rule2.createPatternFormatting();
fill2.setFillBackgroundColor(IndexedColors.BLUE.index);
fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] regions = {
CellRangeAddress.valueOf("A2:A7")
};
sheetCF.addConditionalFormatting(regions, rule2);
break;
}
}
}
System.out.println("");
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
I want that whichever cell has a text "Extra Rows in new table" should be highlighted in blue but my code is not working.please suggest needful.

Okay, the problem is that you're reading the file and changing its content in the process memory, but you never write the changes back into the file.
Try replacing:
file.close();
with:
file.close();
FileOutputStream writeFile = new FileOutputStream(new File(xlsxFileAddress));
workbook.write(writeFile);
writeFile.close();

It can be because of
ConditionalFormattingRule "Equal" will work only when it is given like ComparisonOperator.EQUAL, "\"" + "Extra Rows in new table" + "\"" instead of (ComparisonOperator.EQUAL,"Extra Rows in new table");
Add this after your file.close() in order to write back to the file.
FileOutputStream writeFile = new FileOutputStream(new
File(xlsxFileAddress));
workbook.write(writeFile);
writeFile.close();

Related

How can i get updated row count from Excel using Apache poi

Initially i have used 4 rows of data and then i deleted the last two rows . The problem is, i am still getting 4 as the total no of rows . What should i do to get updated count .
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = (Sheet) workbook.getSheetAt(0);
System.out.println("No of rows:"+firstSheet.getPhysicalNumberOfRows());
Iterator<Row> iterator = firstSheet.iterator();
try{
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
if(!cell.getStringCellValue().isEmpty()){
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
writer.append(cell.getStringCellValue());
break;
}
}
}
}
}finally{
inputStream.close();
workbook.close();
writer.flush();
writer.close();
}
The issue has been cleared by deleting Entire row in the Excel .if you want to delete some data in the excel , please don't just clear contents in the cell instead delete the entire row .

Read Column values from excel file using selenium java

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

Not able to write to excel at run time using POI

I am trying to write my test results/data to excel at runtime for which I have written the below code. I am not getting any error while compiling this code however the results are not getting written in this. Can someone help me out with the problem?
public void WritetoExcel(String filepath, String OrderID) throws IOException
{
FileInputStream ExcelFile = new FileInputStream(filepath);
System.out.println(filepath);
ExcelWBook = new XSSFWorkbook(ExcelFile);
System.out.println("WorkBook Sucessfully");
ExcelWSheet = ExcelWBook.getSheetAt(0);
System.out.println("Sheet Sucessfully");
Iterator<Row> rowIterator= ExcelWSheet.iterator();
int RowNum =0;
while (rowIterator.hasNext())
{
Row row=rowIterator.next();
RowNum++;
}
try
{
Row = ExcelWSheet.createRow(RowNum);
Iterator<Cell> cellIterator=Row.iterator();
Cell = Row.getCell(0);
if (Cell==null)
{
Cell=Row.createCell(0);
Cell.setCellValue(OrderID);
}
else
{
Cell.setCellValue(OrderID);
}
FileOutputStream fileOut = new FileOutputStream(filepath);
ExcelWBook.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch (Exception e )
{
throw (e);
}
}
I was going to make this a comment, but it is too long.
There are a few comments I can make about your code.
First, it seems you are iterating through and counting rows that exist in the sheet. Then you are creating a new row at that index. Since a spreadsheet could have missing rows, this will only work for a very specific type of spreadsheet. That is, one which has no missing rows, and you always want to add the next row in the next empty spot. Instead of:
Iterator<Row> rowIterator= ExcelWSheet.iterator();
int RowNum =0;
while (rowIterator.hasNext())
{
Row row=rowIterator.next();
RowNum++;
}
try
{
Row = ExcelWSheet.createRow(RowNum);
You can just as easily use:
int rowNum = ExcelWSheet.getLastRowNum() + 1;
Row row = ExcelWSheet.createRow(rowNum);
Then you write orderId in the first column of that row. Instead of:
Iterator<Cell> cellIterator=Row.iterator();
Cell = Row.getCell(0);
if (Cell==null)
{
Cell = Row.createCell(0);
Cell.setCellValue(OrderID);
}
else
{
Cell.setCellValue(OrderID);
}
You could just use:
Cell cell = row.createCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellValue(OrderID);
In addition, for this you don't even need the iterators, but when you really need to iterate through the rows and cells of a spreadsheet it is better to use the for each syntax like this:
for (Row row : sheet) {
for (Cell cell : row) {
// do something with the cell
}
}

XLSX to CSV out of memory error

I found plenty of solutions how to convert XLSX to CSV file using Java, all the solutions use: XSSFWorkbook. Problem I am facing is that probably the stream is having too much data. I just don't get why, the file is just 4mb.
CODE:
// For storing data into CSV files
StringBuffer data = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
System.out.println("Getting input stream.");
// Get the workbook object for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile));
System.out.println(" - Done");
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
System.out.println(" - Reading xlsx rows.");
while (rowIterator.hasNext()) {
i++;
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ";");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ";");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ";");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ";");
break;
default:
data.append(cell + ";");
}
}
data.append('\n');
int limit = 10000;
if ((i % limit) == 0) {
System.out.println(" - Writing " + limit + " data.");
fos.write(data.toString().getBytes());
fos.flush();
data = null;
data = new StringBuffer();
System.out.println(" - Data written.");
}
}
fos.write(data.toString().getBytes());
fos.flush();
fos.close();
The error is pointing to line in switch statement where I am appending something to data (StringBuffer), but I am nulling it so it shouldn't be an issue.
Now you may not be able to use SXSSFWorkbook (as it's write-only), but you may be able to convert your program to streaming-style using the SAX-based API. Edit: Another thing you may want to try is to create the XSSFWorkbook from File instead of InputStream (I remember reading somewhere that the File-based code needs less memory).
(First try was:
Since you are reading data sequentially the SXSSFWorkbook class should be just the thing you need.)
The xlsx format is just a zip with content xml and shared-strings xml. Hence 4 MB compressed, may well be very large uncompressed.
Using a zip file system you could load the shared strings into memory, and then read content xml sequentially, immediately outputting.
As two inner files are concerned, you might use java's zip file system. Tedious but not difficult.
try this code this one is perfectly working for me i hope that also working for you.
package com.converting;
import java.io.FileInputStream;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.opencsv.CSVWriter;
import java.util.Iterator;
import java.io.FileWriter;
public class XlsxtoCSV {
public static void main(String[] args) throws Exception{
FileInputStream input_document = new FileInputStream(new File("/home/blackpearl/Downloads/aa.xlsx"));
XSSFWorkbook my_xls_workbook = new XSSFWorkbook(input_document);
XSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
Iterator<Row> rowIterator = my_worksheet.iterator();
FileWriter my_csv=new FileWriter("/home/blackpearl/Downloads/Newaa.csv");
CSVWriter my_csv_output=new CSVWriter(my_csv);
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
int i=0;//String array
String[] csvdata = new String[20];
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next(); //Fetch CELL
switch(cell.getCellType()) { //Identify CELL type
case Cell.CELL_TYPE_STRING:
csvdata[i]= cell.getStringCellValue();
break;
}
i=i+1;
}
my_csv_output.writeNext(csvdata);
}
System.out.println("file imported");
my_csv_output.close(); //close the CSV file
input_document.close(); //close xlsx file
}
}

How to update character set of a excel sheet through Java

I am extracting Russian characters from db and generating one excel sheet. But unfortunately in extracted excel sheet, I have to change the character set manually to "UNICODE(UTF-8)" , otherwise I can't able to see Russian character. Is anybody aware how to update this character set through java(I am not asking to convert the value) ?
Let me know if anything needs to be provided from my end.
Is Apache POI an option ?
Apache POI manages the encoding for you. All the text returned is in Java Unicode format (which I believe is UTF-16, and not UTF-8)
Here is how you read Excel sheet in POI:
try {
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
FileOutputStream out =
new FileOutputStream(new File("C:\\test.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Source: http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/

Categories

Resources