I am facing issues in print preview with xls generated using apache POI. I have used HSSFRichTextString to apply styling to cell content.For text I`m applying Arial font & for numbers within cell content i am using Terminal font.
E.g "SS 123 RR" SS/RR will be in Arial, 123 will be in Terminal.
Below is code snippet for the same.
package com.adp.wfn.payroll.exportToExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ReadAndWriteRichText {
public static void main(String[] args) {
try {
InputStream inp = new FileInputStream("/home/devel/Desktop/hi/CheckRegister_Template.xls");
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
Font font1 = workbook.getFontAt(cell.getCellStyle().getFontIndex());
System.out.println(font1);
Font font2 = workbook.createFont();
font2.setFontName("Terminal");
font2.setFontHeightInPoints(font1.getFontHeightInPoints());
System.out.println(font2);
String originalStr="ss 123 ss";
HSSFRichTextString richString = new HSSFRichTextString( "SS 123 SS 44" );
cell.setCellValue( formatNumbersOfCellText(originalStr,font2,font1) );
FileOutputStream out = new FileOutputStream(
new File("/home/devel/Desktop/createworkbookRaj.xlsx"));
workbook.write(out);
out.close();
System.out.println("createworkbook.xlsx written successfully");
} catch (Exception ex) {
}
}
private static HSSFRichTextString formatNumbersOfCellText(String outputString,Font numberFont, Font strFont){
HSSFRichTextString formattedString = null;
try{//"ss 123 nn 67"
if(null != outputString && !outputString.trim().equalsIgnoreCase("")){
int lstIndexCalculated=0;
int startIndex ;
int endIndex;
String[] splittedArr = outputString.split("\\s");
if(null != splittedArr && splittedArr.length > 0){
formattedString = new HSSFRichTextString(outputString);
for (int i = 0; i < splittedArr.length; i++) {
if(lstIndexCalculated == 0){
startIndex = outputString.indexOf(splittedArr[i]);
}else{
startIndex = outputString.indexOf(splittedArr[i],lstIndexCalculated);
if(lstIndexCalculated < startIndex){
formattedString.applyFont(lstIndexCalculated,startIndex ,numberFont);
}
}
endIndex = startIndex + (splittedArr[i].length());
lstIndexCalculated = endIndex;
if(isNumericField(splittedArr[i])){
formattedString.applyFont(startIndex,endIndex ,numberFont);
}else{
formattedString.applyFont(startIndex,endIndex ,strFont);
}
startIndex = 0;
endIndex = 0;
}
if(lstIndexCalculated != 0){
return formattedString;
}
}
}
}catch(Exception e){
return null;
}
return null;
}
private static boolean isNumericField(String inputStr){
if(null != inputStr && !inputStr.trim().equalsIgnoreCase("")){
try{
inputStr = inputStr.replaceAll("[(,)]", "");
Double value = Double.valueOf(inputStr);
return true;
}catch(NumberFormatException numFormatExp){
return false;
}catch (Exception e) {
return false;
}
}
return false;
}
}
Now when i open generated xls I see formatting applied to contents.
Please refer xls view for the same.
But now when i do print preview I see issue with font. Terminal font which applied for numbers is no longer visible in print preview. Please refer enter image description here for print preview view.
Note the difference between number '1' in both images.
Any idea/suggestion how do i overcome this ?
Thanks In advance.
Related
I have used apache.poi library to parse the .xlsx file but not getting right cell value.
I have Used below Libraries:
commons-collections4-4.1.jar
poi-3.17.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.17.jar
xmlbeans-2.6.0.jar
Here is my code:
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XLSXParser {
private static File clientFile = new File("C:\\demo\\data.xlsx");
private static HashMap<String, String> data = new HashMap<>();
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream(clientFile);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
int count = 0;
while (itr.hasNext() && count <= 3) {
String mpn = "";
String desc = "";
Row row = itr.next();
// Get MPN
mpn = getCellValue(row, 0);
System.out.println("MPN: "+mpn);
// Get Description
desc = getCellValue(row, 1);
if (!mpn.equals("MPN")) {
data.put(mpn, desc);
}
count++;
}
System.out.println(data);
// System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCellValue(Row row, int cellIndex) {
Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = "";
if (cell == null || cell.getCellType() == null) {
System.out.println("Hold..!!");
}
switch (cell.getCellType()) {
case STRING:
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case BLANK:
cellValue = "NA";
break;
default:
}
return cellValue;
}
}
Program Output:
MPN: MPN
MPN: 00112124
MPN: 8.2703124E7
MPN: 8.2703135E7
{8.2703124E7=BS 703 F-7024, 8.2703135E7=BS 703 F-7035, 00112124=BA 100806-7024}
Desire Output:
MPN: MPN
MPN: 00112124
MPN: 82703124
MPN: 82703135
{82703124=BS 703 F-7024, 82703135=BS 703 F-7035, 00112124=BA 100806-7024}
you can download my .xlsx file from here.
Here when I am trying to retrieve the value of a cell for MPN using Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); I am not getting the right value of a cell for the third(82703124) and fourth(82703135) row. Don't know why? or I am creating a mistake while retrieving it. can anyone help me to resolve this issue will be good help for me. Thank You.
I have read all previous ask question but there is solution I found.
How to Edit existing large excel file with SXSSF Streaming api
How to write to an existing file using SXSSF?
I have a existing file which I required to update the content change cell color for some content.
I need to modify excel file with large data over 40,000 rows.
Steps done in below code.
Create new file for Result, Copy file 2 for result to highlight not
equal cell
Create Workbook for 2 compare excel files Temp
XSSFWorkbook XSSF cellStyleRed as SXSSFWorkbook cannot have cellstyle color
keep 100 rows in memory, exceeding rows will be flushed to disk
compareTwoRows from both excel file not equal will change cellstyle color to red on Result file.
Problem is with below code Result file is blank there is no content. I understand my code
is incorrect as SXSSFWorkbook is creating new sheet.
HOW CAN I update result file by change cell color?
package pageobjects;
import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.Reporter;
import property.IHomePage;
import utility.SeleniumUtils;
public class Excelcom2try extends SeleniumUtils implements IHomePage {
public static FileOutputStream opstr = null;
XSSFCellStyle cellStyleRed = null;
SXSSFWorkbook sxssfWorkbook = null;
SXSSFSheet sheet = null;
SXSSFRow row3edit = null;
SXSSFCell Cell = null;
#SuppressWarnings("resource")
public void compare() {
try {
// Create new file for Result
XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fos = new FileOutputStream(new File("\\\\sd\\comparisonfile\\ResultFile.xlsx"));
workbook.write(fos);
workbook.close();
Thread.sleep(2000);
// get input for 2 compare excel files
FileInputStream excellFile1 = new FileInputStream(new File("new File("\\\\sd\\comparisonfile\\UAT_Relationship.xlsx"));
FileInputStream excellFile2 = new FileInputStream(new File(""\\\\sd\\comparisonfile\\Prod_Relationship.xlsx"));
// Copy file 2 for result to highlight not equal cell
FileSystem system = FileSystems.getDefault();
Path original = system.getPath(""\\\\sd\\comparisonfile\\Prod_Relationship.xlsx");
Path target = system.getPath(""\\\\sd\\comparisonfile\\ResultFile.xlsx");
try {
// Throws an exception if the original file is not found.
Files.copy(original, target, StandardCopyOption.REPLACE_EXISTING);
Reporter.log("Successfully Copy File 2 for result to highlight not equal cell");
Add_Log.info("Successfully Copy File 2 for result to highlight not equal cell");
} catch (IOException ex) {
Reporter.log("Unable to Copy File 2 ");
Add_Log.info("Unable to Copy File 2 ");
}
Thread.sleep(2000);
FileInputStream excelledit3 = new FileInputStream(new File("\\\\sd\\comparisonfile\\ResultFile.xlsx"));
// Create Workbook for 2 compare excel files
XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);
// Temp workbook
XSSFWorkbook workbook3new = new XSSFWorkbook();
//XSSF cellStyleRed as SXSSFWorkbook cannot have cellstyle color
cellStyleRed = workbook3new.createCellStyle();
cellStyleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
cellStyleRed.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// Get first/desired sheet from the workbook to compare both excel sheets
XSSFSheet sheet1 = workbook1.getSheetAt(0);
XSSFSheet sheet2 = workbook2.getSheetAt(0);
//XSSFWorkbook workbook3new temp convert to SXSSFWorkbook
// keep 100 rows in memory, exceeding rows will be flushed to disk
sxssfWorkbook = new SXSSFWorkbook(100);
sxssfWorkbook.setCompressTempFiles(true);
sheet = sxssfWorkbook.createSheet();
// Compare sheets
if (compareTwoSheets(sheet1, sheet2, sheet)) {
Reporter.log("\\n\\nThe two excel sheets are Equal");
Add_Log.info("\\n\\nThe two excel sheets are Equal");
} else {
Reporter.log("\\n\\nThe two excel sheets are Not Equal");
Add_Log.info("\\n\\nThe two excel sheets are Not Equal");
}
// close files
excellFile1.close();
excellFile2.close();
// excelledit3.close();
opstr.close();
// dispose of temporary files backing this workbook on disk
}catch (Exception e) {
e.printStackTrace();
}
Reporter.log("Successfully Close All files");
Add_Log.info("Successfully Close All files");
}
// Compare Two Sheets
public boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2, SXSSFSheet sheet) throws IOException {
int firstRow1 = sheet1.getFirstRowNum();
int lastRow1 = sheet1.getLastRowNum();
boolean equalSheets = true;
for (int i = firstRow1; i <= lastRow1; i++) {
Reporter.log("\n\nComparing Row " + i);
Add_Log.info("\n\nComparing Row " + i);
XSSFRow row1 = sheet1.getRow(i);
XSSFRow row2 = sheet2.getRow(i);
//row3edit = sheet.getRow(i);
for(int rownum = 0; rownum < 100; rownum++){
row3edit= sheet.createRow(rownum);
}
if (!compareTwoRows(row1, row2, row3edit)) {
equalSheets = false;
// Write if not equal
// Get error here java.lang.NullPointerException for row3edit.setRowStyle(cellStyleRed);
//if disable test is completed Successfully without writing result file
row3edit.setRowStyle(cellStyleRed);
Reporter.log("Row " + i + " - Not Equal");
Add_Log.info("Row " + i + " - Not Equal");
// break;
} else {
Reporter.log("Row " + i + " - Equal");
Add_Log.info("Row " + i + " - Equal");
}
}
// Write if not equal
opstr = new FileOutputStream(""\\\\sd\\comparisonfile\\ResultFile.xlsx");
sxssfWorkbook.write(opstr);
opstr.close();
return equalSheets;
}
// Compare Two Rows
public boolean compareTwoRows(XSSFRow row1, XSSFRow row2, SXSSFRow row3edit) throws IOException {
if ((row1 == null) && (row2 == null)) {
return true;
} else if ((row1 == null) || (row2 == null)) {
return false;
}
int firstCell1 = row1.getFirstCellNum();
int lastCell1 = row1.getLastCellNum();
boolean equalRows = true;
// Compare all cells in a row
for (int i = firstCell1; i <= lastCell1; i++) {
XSSFCell cell1 = row1.getCell(i);
XSSFCell cell2 = row2.getCell(i);
for(int cellnum = 0; cellnum < 10; cellnum++){
Cell = row3edit.createCell(cellnum);
String address = new CellReference(Cell).formatAsString();
Cell.setCellValue(address);
}
if (!compareTwoCells(cell1, cell2)) {
equalRows = false;
Reporter.log(" Cell " + i + " - NOt Equal " + cell1 + " === " + cell2);
Add_Log.info(" Cell " + i + " - NOt Equal " + cell1 + " === " + cell2);
break;
} else {
Reporter.log(" Cell " + i + " - Equal " + cell1 + " === " + cell2);
Add_Log.info(" Cell " + i + " - Equal " + cell1 + " === " + cell2);
}
}
return equalRows;
}
// Compare Two Cells
#SuppressWarnings("deprecation")
public static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) {
if ((cell1 == null) && (cell2 == null)) {
return true;
} else if ((cell1 == null) || (cell2 == null)) {
return false;
}
boolean equalCells = false;
int type1 = cell1.getCellType();
int type2 = cell2.getCellType();
if (type2 == type1) {
if (cell1.getCellStyle().equals(cell2.getCellStyle())) {
// Compare cells based on its type
switch (cell1.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
if (cell1.getCellFormula().equals(cell2.getCellFormula())) {
equalCells = true;
} else {
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (cell1.getNumericCellValue() == cell2.getNumericCellValue()) {
equalCells = true;
} else {
}
break;
case HSSFCell.CELL_TYPE_STRING:
if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
equalCells = true;
} else {
}
break;
case HSSFCell.CELL_TYPE_BLANK:
if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
equalCells = true;
} else {
}
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
if (cell1.getBooleanCellValue() == cell2.getBooleanCellValue()) {
equalCells = true;
} else {
}
break;
case HSSFCell.CELL_TYPE_ERROR:
if (cell1.getErrorCellValue() == cell2.getErrorCellValue()) {
equalCells = true;
} else {
}
break;
default:
if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
equalCells = true;
} else {
}
break;
}
} else {
return false;
}
} else {
return false;
}
return equalCells;
}
}
in general 40'000 rows is a very small volume of data and I would not
like to suggest employing Streaming in that case.
Instead, you can hold 40'000 rows in a XSSF workbook easily if you just
provide enough memory. Using a XSSF workbook, you can modify the
content directly as you have tried.
However, if you work with really large data of 1 Mill. rows and many
columns, then the following approach will help:
1) engange the Excel Streaming Reader
2) read both files F1 and F2 simultaneously and compare row by row and
cell by cell
3) based on the found difference, create a new row with new cells and
write that to your result file F3
You can not modify existing cells in a SXSSFWorkbook.
I am struggling through this IBAN Checker JAVA project where I am supposed to take IBANs from an excel sheet -> validate them -> print valid/invalid results back into Excel. I have it almost all set up but now I got stuck on looping function that should put already validated IBANS back to the sheet. Here I am getting only last IBAN number of my IBAN Array which gets printed into all the rows, other IBANs are not showing.
However, when I use "System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");" function all the ibans are validated correctly and printed into console one by one.
Is there please some way to get the results of above "System.out.printf" and iterate them through the cells in the Excel sheet? Or would you have some suggestion for modifications of the for-loop please? I think something in the loop is causing the issue because when I put "System.out.prinf" function inside of the loop, it starts validating only the last IBAN number which means something with the loop is not right.
Thank you very much for any help you can give!
IBANChecker03.java
package ibanchecker03;
import java.math.BigInteger;
import java.util.*;
//EXCEL
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//EXCEL
public class IBANChecker03 {
private static final String DEFSTRS = ""
+ "AL28 AD24 AT20 AZ28 BE16 BH22 BA20 BR29 BG22 "
+ "HR21 CY28 CZ24 DK18 DO28 EE20 FO18 FI18 FR27 GE22 DE22 GI23 "
+ "GL18 GT28 HU28 IS26 IE22 IL23 IT27 KZ20 KW30 LV21 LB28 LI21 "
+ "LT20 LU20 MK19 MT31 MR27 MU30 MC27 MD24 ME22 NL18 NO15 PK24 "
+ "PS29 PL28 PT25 RO24 SM27 SA24 RS22 SK24 SI19 ES24 SE24 CH21 "
+ "TN24 TR26 AE23 GB22 VG24 GR27 CR21";
private static final Map<String, Integer> DEFINITIONS = new HashMap<>();
static {
for (String definition : DEFSTRS.split(" "))
DEFINITIONS.put(definition.substring(0, 2), Integer.parseInt(definition.substring(2)));
}
public static void printValid(String iban) throws FileNotFoundException, IOException, InvalidFormatException {
File file = new File("G:\\AR\\INVprint (GD Thomas)\\EKG.xls");
FileInputStream ExcelFile = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(ExcelFile);
HSSFSheet sheet = wb.getSheet("SheetF");
System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");
try {
for (int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
HSSFRow row1 = sheet.getRow(rowNumber);
HSSFCell cell = row1.createCell(1);
cell.setCellValue(iban + " is " + validateIBAN(iban));
//System.out.println(cell);
//for(int columnNumber = 1; columnNumber < row1.getLastCellNum();) {
// HSSFCell cell = row1.createCell(columnNumber);
// if(cell != null) {
//cell.setCellValue("Darkness");
//System.out.println(cell);
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("G:\\AR\\INVprint (GD Thomas)\\EKG.xls");
wb.write(fileOut);
wb.close();
} catch (FileNotFoundException e) {
throw new FileNotFoundException("File not faaund");
}
}
public static void main(String[] args) throws IOException, FileNotFoundException {
String[] ibans = {"GB33BUKB20201555555555"};
for (String iban : ibans) {
try {
printValid(iban);
} catch (InvalidFormatException ex) {
Logger.getLogger(IBANChecker03.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
static boolean validateIBAN(String iban) {
iban = iban.replaceAll("\\s", "").toUpperCase(Locale.ROOT);
int len = iban.length();
if (len < 4 || !iban.matches("[0-9A-Z]+") || DEFINITIONS.getOrDefault(iban.substring(0, 2), 0) != len)
return false;
iban = iban.substring(4) + iban.substring(0, 4);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
sb.append(Character.digit(iban.charAt(i), 36));
BigInteger bigInt = new BigInteger(sb.toString());
return bigInt.mod(BigInteger.valueOf(97)).intValue() == 1;
}
}
HomeOffice.java
package ibanchecker03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class HomeOffice {
public static void main(String[] args) throws Exception {
File excelFile = new File("G:\\AR\\INVprint (GD Thomas)\\TEST2 IBAN.xlsx");
FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
//Here we start iterating through raws and cells
Iterator<Row> rowIt = sheet.iterator();
while (rowIt.hasNext()) {
Row row = rowIt.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getColumnIndex() == 7) { // Choose number of column
//System.out.println(cell.toString() + ","); // Print cells
String cellvalue = cell.toString();
IBANChecker03.printValid(cellvalue);
}
}
workbook.close();
fis.close();
}
}
}
I am new to apache poi, I wanted to split a excel file into multiple files based on row count.
E.g data.xlsx has 15k rows, new files should be like data_1.xlsx with 5k rows,data_2.xlsx should be 5-10k and data_3.xlsx should be 10-15k.
I've got you.
package com.industries.seanimus;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReportSplitter {
private final String fileName;
private final int maxRows;
public ReportSplitter(String fileName, final int maxRows) {
ZipSecureFile.setMinInflateRatio(0);
this.fileName = fileName;
this.maxRows = maxRows;
try {
/* Read in the original Excel file. */
OPCPackage pkg = OPCPackage.open(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
XSSFSheet sheet = workbook.getSheetAt(0);
/* Only split if there are more rows than the desired amount. */
if (sheet.getPhysicalNumberOfRows() >= maxRows) {
List<SXSSFWorkbook> wbs = splitWorkbook(workbook);
writeWorkBooks(wbs);
}
pkg.close();
}
catch (EncryptedDocumentException | IOException | InvalidFormatException e) {
e.printStackTrace();
}
}
private List<SXSSFWorkbook> splitWorkbook(XSSFWorkbook workbook) {
List<SXSSFWorkbook> workbooks = new ArrayList<SXSSFWorkbook>();
SXSSFWorkbook wb = new SXSSFWorkbook();
SXSSFSheet sh = wb.createSheet();
SXSSFRow newRow;
SXSSFCell newCell;
int rowCount = 0;
int colCount = 0;
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
newRow = sh.createRow(rowCount++);
/* Time to create a new workbook? */
if (rowCount == maxRows) {
workbooks.add(wb);
wb = new SXSSFWorkbook();
sh = wb.createSheet();
rowCount = 0;
}
for (Cell cell : row) {
newCell = newRow.createCell(colCount++);
newCell = setValue(newCell, cell);
CellStyle newStyle = wb.createCellStyle();
newStyle.cloneStyleFrom(cell.getCellStyle());
newCell.setCellStyle(newStyle);
}
colCount = 0;
}
/* Only add the last workbook if it has content */
if (wb.getSheetAt(0).getPhysicalNumberOfRows() > 0) {
workbooks.add(wb);
}
return workbooks;
}
/*
* Grabbing cell contents can be tricky. We first need to determine what
* type of cell it is.
*/
private SXSSFCell setValue(SXSSFCell newCell, Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
newCell.setCellValue(cell.getDateCellValue());
} else {
newCell.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
default:
System.out.println("Could not determine cell type");
}
return newCell;
}
/* Write all the workbooks to disk. */
private void writeWorkBooks(List<SXSSFWorkbook> wbs) {
FileOutputStream out;
try {
for (int i = 0; i < wbs.size(); i++) {
String newFileName = fileName.substring(0, fileName.length() - 5);
out = new FileOutputStream(new File(newFileName + "_" + (i + 1) + ".xlsx"));
wbs.get(i).write(out);
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
/* This will create a new workbook every 1000 rows. */
new ReportSplitter("Data.xlsx", 1000);
}
}
A few notes:
For writing the workbooks, I use
SXSSFWorkbook. It's a lot
faster than HSSF or XSSF, as it doesn't hold everything in memory
before writing (which causes a horrible gc mess).
The Busy Developer's Guide is your friend for learning Apache POI ;)
ENJOY!
EDIT: I've updated the code to copy cell styles as well. Two things to note about this:
Copying styles will SLOW things down considerably.
POI creates a template file that may become too big to be uncompressed, throwing a Zip bomb detected error. You can fix this by changing the minimum inflation ratio via ZipSecureFile.setMinInflateRatio(0).
Thanks for your code. Just two cent from my side
The code above does not copy the time Hence I modified it for having Time Columns which is a small modification in setValue Code.
Basically I'm checking using format part if it is a time column for which the year would be 1899
Hope it helps :)
private static SXSSFCell setValue(SXSSFCell newCell, Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
//System.out.println("The Cell Type is numeric ");
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
Date c = cell.getDateCellValue();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat year = new SimpleDateFormat("yyyy");
String strTime = simpleDateFormat.format(c);
String strYear=year.format(c);
if(strYear.equals("1899"))
{
System.out.println(strTime);
newCell.setCellValue(DateUtil.convertTime(strTime));
}
else
{
newCell.setCellValue(c);
}
} else {
newCell.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
default:
System.out.println("Could not determine cell type");
}
return newCell;
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SplitFile {
private final String fileName;
private final int maxRows;
private final String path;
private final String userfilename="";
public static int filecount;
public static String taskname;
public static int rowcounter;
public SplitFile(String fileName, final int maxRows, String filepath, String userfilename) throws FileNotFoundException {
path = filepath;
taskname = userfilename;
this.fileName = fileName;
this.maxRows = maxRows;
System.out.println("In Constructor");
File file = new File(fileName);
FileInputStream inputStream = new FileInputStream(file);
try {
/* Read in the original Excel file. */
//OPCPackage pkg = OPCPackage.open(new File(fileName));
Workbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
System.out.println("Got Sheet");
/* Only split if there are more rows than the desired amount. */
if (sheet.getPhysicalNumberOfRows() >= maxRows) {
List<SXSSFWorkbook> wbs = splitWorkbook(workbook);
writeWorkBooks(wbs);
}
}
catch (EncryptedDocumentException | IOException e) {
e.printStackTrace();
}
}
private List<SXSSFWorkbook> splitWorkbook(Workbook workbook) {
List<SXSSFWorkbook> workbooks = new ArrayList<SXSSFWorkbook>();
SXSSFWorkbook wb = new SXSSFWorkbook();
SXSSFSheet sh = (SXSSFSheet) wb.createSheet();
SXSSFRow newRow,headRow = null;
SXSSFCell newCell;
String headCellarr[] = new String[50];
int rowCount = 0;
int colCount = 0;
int headflag = 0;
int rcountflag = 0;
int cols = 0;
XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
//sheet.createFreezePane(0, 1);
int i = 0;
rowcounter++;
for (Row row : sheet) {
if(i==0)
{
//headRow = sh.createRow(rowCount++);
/* Time to create a new workbook? */
int j = 0;
for (Cell cell : row) {
//newCell = headRow.createCell(colCount++);
headCellarr[j] = cell.toString();
j++;
}
cols = j;
colCount = 0;
i++;
}
else
{
break;
}
}
for (Row row : sheet) {
//newRow = sh.createRow(rowCount++);
/* Time to create a new workbook? */
if (rowCount == maxRows) {
headflag = 1;
workbooks.add(wb);
wb = new SXSSFWorkbook();
sh = (SXSSFSheet) wb.createSheet();
rowCount = 0;
}
if(headflag == 1)
{
newRow = (SXSSFRow) sh.createRow(rowCount++);
headflag = 0;
for(int k=0;k<cols;k++)
{
newCell = (SXSSFCell) newRow.createCell(colCount++);
newCell.setCellValue(headCellarr[k]);
}
colCount = 0;
newRow = (SXSSFRow) sh.createRow(rowCount++);
for (Cell cell : row) {
newCell = (SXSSFCell) newRow.createCell(colCount++);
if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
newCell.setCellValue("-");
}
else
{
newCell = setValue(newCell, cell);
}
}
colCount = 0;
}
else
{
rowcounter++;
newRow = (SXSSFRow) sh.createRow(rowCount++);
for(int cn=0; cn<row.getLastCellNum(); cn++) {
// If the cell is missing from the file, generate a blank one
// (Works by specifying a MissingCellPolicy)
Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
// Print the cell for debugging
//System.out.println("CELL: " + cn + " --> " + cell.toString());
newCell = (SXSSFCell) newRow.createCell(cn);
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
newCell.setCellValue(cell.getNumericCellValue());
}
else
{
newCell.setCellValue(cell.toString());
}
}
}
}
/* Only add the last workbook if it has content */
if (wb.getSheetAt(0).getPhysicalNumberOfRows() > 0) {
workbooks.add(wb);
}
return workbooks;
}
/*
* Grabbing cell contents can be tricky. We first need to determine what
* type of cell it is.
*/
private SXSSFCell setValue(SXSSFCell newCell, Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
newCell.setCellValue(cell.getDateCellValue());
} else {
//newCell.setCellValue(cell.getNumericCellValue());
newCell.setCellValue(cell.toString());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
newCell.setCellValue("-");
break;
default:
System.out.println("Could not determine cell type");
newCell.setCellValue(cell.toString());
}
return newCell;
}
/* Write all the workbooks to disk. */
private void writeWorkBooks(List<SXSSFWorkbook> wbs) {
FileOutputStream out;
boolean mdir = new File(path + "/split").mkdir();
try {
for (int i = 0; i < wbs.size(); i++) {
String newFileName = fileName.substring(0, fileName.length() - 5);
//out = new FileOutputStream(new File(newFileName + "_" + (i + 1) + ".xlsx"));
out = new FileOutputStream(new File(path + "/split/" + taskname + "_" + (i + 1) + ".xlsx"));
wbs.get(i).write(out);
out.close();
System.out.println("Written" + i);
filecount++;
}
System.out.println(userfilename);
} catch (IOException e) {
e.printStackTrace();
}
}
public int sendtotalrows()
{
return rowcounter;
}
public static void main(String[] args) throws FileNotFoundException{
// This will create a new workbook every 1000 rows.
// new Splitter(filename.xlsx, No of split rows, filepath, newfilename);
new SplitFile("filepath/filename.xlsx", 10000, "filepath", "newfilename"); //No of rows to split: 10 K
}
}
Finding last column cell values. if cell value is "Not Valid" or "Not Applicable", delete the entire row.
The code I have written so far is as follows:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
public class Column
{
public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, IOException
{
try
{
Workbook wb = WorkbookFactory.create(new FileInputStream("C:/Users/Excel1.xlsx"));
Sheet sheet = wb.getSheet("Retail-All");
Workbook wb2 = new HSSFWorkbook();
wb2 = wb;
Row row;
row = sheet.getRow(0);
int getLastCell=row.getLastCellNum()-1;
int lastIndex = sheet.getLastRowNum();
for (int i=0; i<=lastIndex; i++)
{
row=sheet.getRow(i);
if(row.getCell(getLastCell)!=null && (row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid) || row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable")))
{
sheet.removeRow(row);
//sheet.shiftRows(i, lastIndex, -1);
}
}
FileOutputStream fileOut = new FileOutputStream("C:/Users/shiftRows.xlsx");
wb2.write(fileOut);
fileOut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
With the above code, using row.setZeroHeight(true); method I can able to hide the rows.
With the above code, using sheet.removeRow(row); method I can able to empty all the cells in that particular row.
I found some code in net, but none of them are deleting the rows permanently.My requirement is to delete the rows permanently. How to code to meet my requirement?
Perhaps using a VBA macro would be more helpful in this situation? VBA macros can be embedded into your spreadsheet, so running them could be a lot easier.
Here is a good place to get started with VBA:
http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx
Try this one...
SVTableModel model = new SVTableModel(sheet);
int lastIndex = model.getRowCount();
for (int i=1; i<=lastIndex; i++)
{
row=sheet.getRow(i);
if((row.getCell(getLastCell)!=null) && (row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid") || row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable")))
{
row.setZeroHeight(true);
sheet.removeRow(row);
}
Instead of sheet.removeRow(row); you can use the shiftRows(int startRow, int endRow, int n) instruction, that seems you have tried, that shifts rows between startRow and endRow n number of rows.
In your case it will be
// ... code before ...
for (int i=0; i<=lastIndex; i++)
{
row=sheet.getRow(i);
if(row.getCell(getLastCell)!=null && (row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid") || row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable")))
{
// This should shift up by 1 row all the rows below
sheet.shiftRows(i+1, lastIndex, -1);
i--; // Since you move row at i+1 to i you have to recompute the i-th row
}
}
// ... code after ...
Check org.apache.poi.hssf.usermodel.HSSFSheet Documentation for more information
Please try the below code. My last cell value is Mismatched ot
Matched. Set as per ur excel.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class DeleteAllRecord {
public static void main(String[] args) {
try{
File src= new File("testdata\\OSBC.xlsx");
FileInputStream fis= new FileInputStream(src);
XSSFWorkbook workbook= new XSSFWorkbook(fis);
XSSFSheet data= workbook.getSheet("TestData");
int length=data.getLastRowNum();
XSSFSheet data1;
Row row1;
for(int i=1;i<=length;i++){
String testCaseNo=data.getRow(i).getCell(4).toString();
System.out.println("The Testcase no "+testCaseNo);
data1= workbook.getSheet(testCaseNo);
row1 = data1.getRow(2);
int getLastCell=row1.getLastCellNum()-1;
System.out.println("The last cell is "+getLastCell);
int lastIndex = data1.getLastRowNum();
System.out.println("The last row is "+lastIndex);
String str1= row1.getCell(getLastCell).toString();
System.out.println("The valu1 is "+ str1);
for (int j=0; j<lastIndex-1; j++)
{
String str=data1.getRow(j+2).getCell(getLastCell).toString();
row1=data1.getRow(j+2);
System.out.println("print " + (j+2) +str);
if(row1.getCell(getLastCell)!=null &&
(row1.getCell(getLastCell).toString().equalsIgnoreCase("MATCHED")||
row1.getCell(getLastCell).toString().equalsIgnoreCase("MISMATCHED")||
row1.getCell(getLastCell).toString().equalsIgnoreCase(""))){
data1.removeRow(row1);
System.out.println("print " + (j+2)+ " " +str);
}
else{
System.out.println("Null");
}
}
FileOutputStream fileOut = new FileOutputStream("testdata\\delete.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("File deleted");
}
}
catch(Exception e){
System.out.println("No Data Exists to delete");
}
}
}