extract full path for external references in a excel file - java

I am trying to get full path of external referenced files,
able to extract partial path not compete path for the exteranl referneced files.
With POI is it possible to achieve this. Or need to go with different library or approch?
try {
String filePath = "C:\\Users\\DATA\\2021 Plan Summary.xlsx";
Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath));
Map<String, String> dataMap = new HashMap<String, String>();
List<String> formulaList = new ArrayList<String>();
EvaluationWorkbook evalWorkbook = null;
if (workbook instanceof HSSFWorkbook) {
evalWorkbook = HSSFEvaluationWorkbook.create((HSSFWorkbook) workbook);
} else if (workbook instanceof XSSFWorkbook) {
evalWorkbook = XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
}
List<ExternalLinksTable> externalLinksTableList = getReferencedWorkbooksXssfV2((XSSFWorkbook)workbook);
Sheet sheet = workbook.getSheetAt(0);
EvaluationSheet evalSheet = evalWorkbook.getSheet(0);
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == CellType.FORMULA && cell.getCellFormula().contains("[")) {
// System.out.println(cell.getStringCellValue());
String cellFormula = cell.getCellFormula();
EvaluationCell evaluationCell = evalSheet.getCell(cell.getRowIndex(), cell.getColumnIndex());
Ptg[] formulaTokens = evalWorkbook.getFormulaTokens(evaluationCell);
for (Ptg formulaToken : formulaTokens) {
if (formulaToken instanceof Ref3DPxg) {
Ref3DPxg pxg = (Ref3DPxg) formulaToken;
int extWB = pxg.getExternalWorkbookNumber();
int extLink = extWB - 1;
ExternalLinksTable links = externalLinksTableList.get(extLink);
String filename = links.getLinkedFileName();
formulaList.add(cellFormula);
dataMap.put(StringUtils.substringBetween(cellFormula, "[", "]"), filename);
}
}
}
}
}
}
private static List<ExternalLinksTable> getReferencedWorkbooksXssfV2(XSSFWorkbook workbook) throws OpenXML4JException {
final List<ExternalLinksTable> externalLinksTable = workbook.getExternalLinksTable();
return externalLinksTable;
}

Related

filter a column in excel to specific word using java code

I have an Excel file which needs filtering on a specific column.
String fileName = "filepath";
String cellContent = "Automation";
int rownr = 0;
int colnr = 0; //column from which you need data to store in array list
InputStream input = new FileInputStream(fileName);
XSSFWorkbook wb = new XSSFWorkbook(input);
XSSFSheet sheet = wb.getSheetAt(0);
List filteredCol = new ArrayList();
filteredCol = findRow(sheet, cellContent);
if (filteredCol != null) {
for (Iterator iter = filteredCol.iterator(); iter.hasNext(); ) {
System.out.println(iter.next());
}
}
private static List findRow(HSSFSheet sheet, String cellContent) {
List filter=new ArrayList();
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
//System.out.println("Row numbers are"+row.getRowNum());
int rownumber=row.getRowNum();
//return row.getRowNum();
XSSFRow row1 = sheet.getRow(rownumber);
XSSFCell cell1 = row1.getCell(0);
filter.add(cell1);
}
}
}
}
return filter;
}
I am getting number format exception on this codeline:
"cell.getRichStringCellValue().getString().trim().equals(cellContent)"

How to write data into multiple sheets in same excel [duplicate]

This question already has an answer here:
Creating multiple sheets using Apache poi and servlets
(1 answer)
Closed 1 year ago.
I am trying to write the data into same excel file in different sheets, below is the code I tried here only one sheet is creating and data is updating in that sheet, new sheet name is overriding on old sheet. Here I am calling call method two times with 2 different sheet name, when we call from 1st time data need to update in sheet1 and 2nd time call data need to update in sheet2 but in this code only sheet2 is creating and data updating in that.
public static void call(Map<String, String[]> dataListLMS_IPS, String sheet)
{
try {
String filePath = "C:\\Users\\PATIV25\\Downloads\\APEX UPEX.xlsx";
File theDir = new File(filePath);
String filename = theDir.toString();
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.close();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet(sheet);
XSSFRow row;
Set<String> keyid = dataListLMS_IPS.keySet();
int rowid = 0;
// writing the data into the sheets...
CellStyle style = workbook.createCellStyle();
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
String[] i = dataListLMS_IPS.get(key);
int cellid = 0;
int a = 0;
for (String obj : i) {
Cell cell = row.createCell(cellid++);
cell.setCellValue(obj);
if (rowid != 1) {
if (i[2].equals(i[6]) && i[3].equals(i[7])) {
style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
cell.setCellValue(obj);
if (a == 2 || a == 3 || a == 6 || a == 7)
cell.setCellStyle(style);
a++;
}
}
}
}
// .xlsx is the format for Excel Sheets...
// writing the workbook into the file...
FileOutputStream out = new FileOutputStream(theDir);
workbook.write(out);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] arg) throws Exception {
Map<String, String[]> data = new LinkedHashMap<>();
data.put("A", new String[]{"ACC NO: ", "REPORT TYPE", "PAYMENTID", "AMOUNT", "REPORT TYPE", "PAYMENTID", "AMOUNT", "RESULT"});
data.put("v", new String[]{"ACC NO: ", "REPORT TYPE", "PAYMENTID", "AMOUNT", "REPORT TYPE", "PAYMENTID", "AMOUNT", "RESULT"});
call(data, "sheet1");
call(data, "sheet2");
}
The existing logic is incorrect. You need to separate the creation of file and sheets into different sections if you want to call the call method twice. Try this:
public static void call(Map<String, String[]> dataListLMS_IPS, FileOutputStream fileOut) throws IOException
{
XSSFWorkbook workbook = new XSSFWorkbook();
Set<String> keyid = dataListLMS_IPS.keySet();
int rowid = 0;
// writing the data into the sheets...
CellStyle style = workbook.createCellStyle();
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
for (String key : keyid) {
XSSFSheet spreadsheet = workbook.createSheet(key);
XSSFRow row;
row = spreadsheet.createRow(0);
String[] i = dataListLMS_IPS.get(key);
int cellid = 0;
int a = 0;
for (String obj : i) {
XSSFCell cell = row.createCell(cellid++);
cell.setCellValue(obj);
if (rowid != 1) {
if (i[2].equals(i[6]) && i[3].equals(i[7])) {
style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
cell.setCellValue(obj);
if (a == 2 || a == 3 || a == 6 || a == 7)
cell.setCellStyle(style);
a++;
}
}
}
}
workbook.write(fileOut);
}
public static void main(String[] arg) throws Exception {
Map<String, String[]> data = new LinkedHashMap<>();
data.put("A", new String[]{"ACC NO: ", "REPORT TYPE", "PAYMENTID", "AMOUNT", "REPORT TYPE", "PAYMENTID", "AMOUNT", "RESULT"});
data.put("v", new String[]{"ACC NO: ", "REPORT TYPE", "PAYMENTID", "AMOUNT", "REPORT TYPE", "PAYMENTID", "AMOUNT", "RESULT"});
FileOutputStream fileOut = null;
try {
String filePath = "d:\\APEX UPEX.xlsx";
File theDir = new File(filePath);
String filename = theDir.toString();
fileOut = new FileOutputStream(filename);
call(data, fileOut);
call(data, fileOut);
}
catch (Exception e)
{
e.printStackTrace();
} finally {
if (fileOut != null)
fileOut.close();
}
}
It will create 2 sheets in the same Excel file.

How to extract data from Excel sheet using Apache POI in java(lookup framing)

public class Array_Learn {
public static void main(String[] args) {
try {
FileInputStream ExcelFile = new FileInputStream(new File("C:\\Users\\Anbu.B\\Desktop\\POI-Test\\mediTask.xlsx"));
XSSFWorkbook book1 = new XSSFWorkbook(ExcelFile);
XSSFSheet sheet = book1.getSheetAt(0);
Iterator<Row> rowiter = sheet.iterator();
while (rowiter.hasNext()) {
XSSFRow row = (XSSFRow) rowiter.next();
if (row.getRowNum() == 2) {
Iterator cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
XSSFCell cell = (XSSFCell) cellIterator.next();
if (cell.getStringCellValue().contains("|")) {
String split[] = cell.getStringCellValue().split("\\|");
}
}
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
I need this output:
chest&&pain=J90
lung&&pneumonia=J54.9
lungs&&pneumonia=J54.9
bronchi&&pneumonia=J54.9
bronchus&&pneumonia=J54.9
colon&&ascending&tumor=D12.5
colon&&ascending&carcinoma=D12.5
colon&&ascending&cancer=D12.5
colon&&ascending&&tumor&&resection=D12.6
colon&&descending&&tumor&&resection=D12.6
colon&&ascending&&carcinoma&&resection=D12.6
colon&&descending&&carcinoma&&resection=D12.6
colon&&ascending&&cancer&&resection=D12.6
colon&&descending&&cancer&&resection=D12.6
The above code is doing read row and iterate each cell and check cell contains | symbol condition is true the split statement is working but, I need the above exact output. What I did in the above code:
Read the excel file.
Read sheet from the excel file.
then create row iterator.
create cell iterator.
check cell contains | symbol then split that cell strings and store into the string array.
You almost finished your task. The key here to use one of the algorithms to generate combinations. You could find a general description of such algorithms there, or more close examples with strings on java there.
Full code example (recursive algorithm):
The ParsedRow class for calculating of different combinations:
class ParsedRow {
private final List<List<String>> combinations;
private final String suffix;
public ParsedRow(List<List<String>> combinations, String suffix) {
this.combinations = combinations;
this.suffix = suffix;
}
public List<String> combine() {
List<String> res = new ArrayList<>();
combine(res, 0, "");
return res;
}
public void combine(List<String> res, int depth, String current) {
if (combinations.size() == depth) {
res.add(current + "=" + suffix);
return;
}
String delimiter = current.isEmpty() ? "" : "&&";
for (int i = 0; i < combinations.get(depth).size(); i++) {
combine(res, depth + 1, current + delimiter + combinations.get(depth).get(i));
}
}
}
The Main class for reading the xlsx file and printing results
public class Main {
public static void main(String[] args) throws IOException {
try (final FileInputStream file = new FileInputStream("diseases.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(file)) {
List<String> finalResult = new ArrayList<>();
for (Row row : workbook.getSheetAt(0)) {
List<List<String>> combinations = new ArrayList<>();
String suffix = "";
for (Cell cell : row) {
if (cell.getColumnIndex() != 4) {
final List<String> strings = Arrays.asList(cell.getStringCellValue().split("\\|"));
combinations.add(strings);
} else {
suffix = cell.getStringCellValue();
}
}
ParsedRow parsedRow = new ParsedRow(combinations, suffix);
finalResult.addAll(parsedRow.combine());
}
for (String line : finalResult) {
System.out.println(line);
}
}
}
}

How to copy .xlsx complete workbook

I have the solution to copy .xls workbook in java but not able to copy .xlsx workbook.
Anybody have solution.
I have searched google, stackoverflow and found solution only to copy xls files.
If anyone wants a simpler procedure just use Files.copy:
File originalWb = new File("orginalWb.xlsx");
File clonedWb = new File("clonedWb.xlsx");
Files.copy(originalWb.toPath(), clonedW.toPath());
No need for any bloated code
I didn't test my code. Just wrote it to give you a basic idea of what to do.
public class CopyXSSFWorkbook {
public static void main(String[] args) {
// Read xlsx file
XSSFWorkbook oldWorkbook = null;
try {
oldWorkbook = (XSSFWorkbook) WorkbookFactory.create(new File("old.xlsx"));
} catch (Exception e) {
e.printStackTrace();
return;
}
final XSSFWorkbook newWorkbook = new XSSFWorkbook();
// Copy style source
final StylesTable oldStylesSource = oldWorkbook.getStylesSource();
final StylesTable newStylesSource = newWorkbook.getStylesSource();
oldStylesSource.getFonts().forEach(font -> newStylesSource.putFont(font, true));
oldStylesSource.getFills().forEach(fill -> newStylesSource.putFill(new XSSFCellFill(fill.getCTFill())));
oldStylesSource.getBorders()
.forEach(border -> newStylesSource.putBorder(new XSSFCellBorder(border.getCTBorder())));
// Copy sheets
for (int sheetNumber = 0; sheetNumber < oldWorkbook.getNumberOfSheets(); sheetNumber++) {
final XSSFSheet oldSheet = oldWorkbook.getSheetAt(sheetNumber);
final XSSFSheet newSheet = newWorkbook.createSheet(oldSheet.getSheetName());
newSheet.setDefaultRowHeight(oldSheet.getDefaultRowHeight());
newSheet.setDefaultColumnWidth(oldSheet.getDefaultColumnWidth());
// Copy content
for (int rowNumber = oldSheet.getFirstRowNum(); rowNumber < oldSheet.getLastRowNum(); rowNumber++) {
final XSSFRow oldRow = oldSheet.getRow(rowNumber);
if (oldRow != null) {
final XSSFRow newRow = newSheet.createRow(rowNumber);
newRow.setHeight(oldRow.getHeight());
for (int columnNumber = oldRow.getFirstCellNum(); columnNumber < oldRow
.getLastCellNum(); columnNumber++) {
newSheet.setColumnWidth(columnNumber, oldSheet.getColumnWidth(columnNumber));
final XSSFCell oldCell = oldRow.getCell(columnNumber);
if (oldCell != null) {
final XSSFCell newCell = newRow.createCell(columnNumber);
// Copy value
setCellValue(newCell, getCellValue(oldCell));
// Copy style
XSSFCellStyle newCellStyle = newWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(newCellStyle);
}
}
}
}
}
try {
oldWorkbook.close();
newWorkbook.write(new FileOutputStream("new.xlsx"));
newWorkbook.close();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
private static void setCellValue(final XSSFCell cell, final Object value) {
if (value instanceof Boolean) {
cell.setCellValue((boolean) value);
} else if (value instanceof Byte) {
cell.setCellValue((byte) value);
} else if (value instanceof Double) {
cell.setCellValue((double) value);
} else if (value instanceof String) {
if (value.startsWith("=")) {
// Formula String
cell.setCellFormula(value.substring(1));
} else {
cell.setCellValue(cstr);
}
} else {
throw new IllegalArgumentException();
}
}
private static Object getCellValue(final XSSFCell cell) {
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
return cell.getBooleanCellValue(); // boolean
case ERROR:
return cell.getErrorCellValue(); // byte
case NUMERIC:
return cell.getNumericCellValue(); // double
case STRING:
case BLANK:
return cell.getStringCellValue(); // String
case FORMULA:
return "=" + cell.getCellFormula(); // String for formula
default:
throw new IllegalArgumentException();
}
}
}
Using Apache POI XSSF library:
public void copyFile(String sourcePath, String destinationPath) throws IOException {
FileInputStream excelFile = new FileInputStream(new File(sourcePath));
Workbook workbook = new XSSFWorkbook(excelFile);
FileOutputStream outputStream = new FileOutputStream(destinationPath);
workbook.write(outputStream);
workbook.close();
}
Clone a workbook in memory (if you can efford it):
public static Workbook cloneWorkbook(final Workbook workbook) {
try {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096);
workbook.write(outputStream);
return WorkbookFactory.create(new ByteArrayInputStream(outputStream.toByteArray()));
} catch (final IOException ex) {
log.warn("Error cloning workbook", ex);
return null; // or throw exception
}
}
Apache POI Busy Developers' Guide to HSSF and XSSF Features
POI performs these steps on XLS/XLSX File. XLS: HSSFWorkbook, XLSX: XSSFWorkbook
Read the File as Stream (FileStream): SystemFile to JavaFileObject
Create a WorkBook form Stream. Stream to XSSFWorkbook-JavaObject
Using POI functions you can perform CURD operations on Java Workbook Object.
Convert Java Workbook Object to file/stream.
NOTE: Index Starts form 0 for Row/Column/Sheet.
Following operations are performed on Sheets:
XLSX File with Sheet1, Sheet2
getSheet_RemoveOthers(Sheet1). XLSX File:Sheet1
public static XSSFSheet getSheet_RemoveOthers(String sourceFileSheetName) {
XSSFSheet srcSheet = workBook.getSheet(sourceFileSheetName);
//Sheet srcSheet = oldWorkbook.getSheetAt(0);
int srcSheetIndex = workBook.getSheetIndex(srcSheet);
System.out.println("srcSheetIndex:"+srcSheetIndex);
int numberOfSheets = workBook.getNumberOfSheets();
for (int indexAt = 0; indexAt < numberOfSheets; indexAt++) {
if (srcSheetIndex == indexAt) {
System.out.println("sourceFileSheetName:"+indexAt);
} else {
String sheetName = workBook.getSheetName(indexAt);
System.out.println("Removing sheetName:"+sheetName);
workBook.removeSheetAt(indexAt);
}
}
System.out.println("getSheetName : "+ srcSheet.getSheetName() );
int totalRows = srcSheet.getPhysicalNumberOfRows();
System.out.println("Total Number of Rows : "+ totalRows );
return srcSheet;
}
XSSFSheet.cloneSheet(Sheet1). XLSX File:Sheet1, Sheet1 (2)
public static XSSFSheet cloneSheet(String sourceFileSheetName) {
Sheet srcSheet = workBook.getSheet(sourceFileSheetName);
int srcSheetIndex = workBook.getSheetIndex(srcSheet);
System.out.println("srcSheetIndex:"+srcSheetIndex);
XSSFSheet cloneSheet = workBook.cloneSheet(srcSheetIndex);
return cloneSheet;
}
Full-Length Example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class POI_XLSX_Report {
static String filePath = "C:/Yash/",
sourceFile = filePath+"POIExcel.xlsx", sourceFileSheetName = "Sheet1",
destinationFile = filePath+"POIExcelCopy.xlsx";
static XSSFWorkbook workBook;
public static void main(String[] args) throws Exception {
File mySrcFile = new File(sourceFile);
FileInputStream stream = new FileInputStream(mySrcFile);
workBook = (XSSFWorkbook) WorkbookFactory.create( stream );
XSSFSheet sheet_RemoveOthers = getSheet_RemoveOthers(sourceFileSheetName);
setSheetValue(sheet_RemoveOthers, 4, 6, "Val2");
// New Sheet with exact copy of Source-Sheet
XSSFSheet cloneSheet = cloneSheet(sourceFileSheetName);
setSheetValue(cloneSheet, 4, 6, "Val2");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
workBook.write( byteArrayOutputStream );
byteArrayOutputStream.close();
File clonedWb = new File(destinationFile);
//Files.copy(mySrcFile.toPath(), clonedWb.toPath()); If a file is already available then throws exception
// Write the output to a file
FileOutputStream fileOutputStream = new FileOutputStream( clonedWb );
byteArrayOutputStream.writeTo(fileOutputStream);
}
public static XSSFSheet getSheet_RemoveOthers(String sourceFileSheetName) {
// ...
}
public static XSSFSheet cloneSheet(String sourceFileSheetName) {
// ...
}
public static void setSheetValue(Sheet sheet, int colIndex, int rowIndex, String value) {
// Row and Column index starts form 0
rowIndex = rowIndex - 1;
colIndex = colIndex - 1;
Row row = sheet.getRow(rowIndex);
if (row == null) {
System.out.println("createRow:");
Row createRow = sheet.createRow(rowIndex);
row= createRow;
}
short lastCellNum = row.getLastCellNum();
System.out.println("Col:"+lastCellNum);
Cell createCell = row.createCell(colIndex, CellType.STRING);
System.out.println("New cell:"+createCell.getStringCellValue());
createCell.setCellValue(value);
}
}

Apache POI append data to excel sheet not working

I am reading data from an arraylist and writing this to an excel sheet. The problem is my excel is getting overwritten each time. Here is my code. I can't figure out what is wrong here :( Can someone please help?
public static void main( String[] args ) throws Exception
{
List<String> fileData = new ArrayList<String>();
for(File file:files) {
fileData.add(readFileContents(file.getAbsolutePath()));
}
for(String fileContent:fileData) {
//do some stuff that in turn calls the writeDataToExcel method
}
}
private static void writeDataToExcel(String test,Map<String,String> dataMap,Object object) throws IOException {
File file = new File("input/data.xls");
Map<String,Object[]> data = new LinkedHashMap<String,Object[]>();
XSSFWorkbook workbook = null;
int count = 0;
XSSFSheet sheet = null;
if(file.exists()) {
workbook = new XSSFWorkbook(new FileInputStream(file));
sheet = workbook.getSheet("Data Sheet");
}
else {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Data Sheet");
//count = sheet.getLastRowNum();
}
data.put("1", new Object[]{"Id","Name","Field","Description","Value"});
for(Map.Entry<String, String> dataMp:dataMap.entrySet()) {
data.put(Integer.toString(count+2), new Object[]{id,object.getClass().getSimpleName(),dataMp.getKey(),dataMp.getValue(),"null"});
count++;
}
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if(obj instanceof String)
cell.setCellValue((String)obj);
}
}
FileOutputStream fis = new FileOutputStream("input/data.xls");
workbook.write(fis);
if(fis!=null)
fis.close();
}
I think problem is at line
int rownum = 0;
this will set rowNUm to zero each time and sheet will be written from zero row
You need to persist this rowNum value if you want to append data in the sheet

Categories