-
-
Save sameer/882e04ecc45cfe3f4c5fd72386508805 to your computer and use it in GitHub Desktop.
Prints out all cellls in an Excel XLS sheet along with named references for analyzing XLM macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.poi.ss.formula.EvaluationWorkbook; | |
import org.apache.poi.ss.formula.FormulaParser; | |
import org.apache.poi.ss.formula.FormulaType; | |
import org.apache.poi.ss.formula.WorkbookEvaluator; | |
import org.apache.poi.ss.formula.eval.BlankEval; | |
import org.apache.poi.ss.formula.eval.FunctionEval; | |
import org.apache.poi.ss.formula.eval.ValueEval; | |
import org.apache.poi.ss.formula.function.FunctionMetadataRegistry; | |
import org.apache.poi.ss.formula.functions.Fixed2ArgFunction; | |
import org.apache.poi.ss.usermodel.*; | |
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.util.Iterator; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class XlmAnalyze { | |
private static final String FILE_NAME = "2307013695_03162021.xls"; | |
public static void main(String[] args) throws Throwable { | |
var excelFile = new FileInputStream(new File(FILE_NAME)); | |
var workbook = new HSSFWorkbook(excelFile); | |
var pictures = workbook.getAllPictures(); | |
for (int i = 0; i < pictures.size(); i++) { | |
var pic = pictures.get(i); | |
var picFile = new FileOutputStream("pic_" + i + "." + pic.getMimeType().split("/")[1]); | |
picFile.write(pic.getData()); | |
picFile.close(); | |
} | |
System.out.println("#### Workbook breakdown"); | |
System.out.println("| Sheet | Address | Type | Value |"); | |
System.out.println("|--|--|--|--|"); | |
for (var sheet : workbook) { | |
for (var row : sheet) { | |
for (var cell : row) { | |
String cellValue; | |
System.out.printf("|%s|%s|%s|", sheet.getSheetName(), cell.getAddress(), cell.getCellType()); | |
try { | |
cellValue = switch (cell.getCellType()) { | |
case BLANK -> "null"; | |
case BOOLEAN -> Boolean.toString(cell.getBooleanCellValue()); | |
case ERROR -> "#ERROR"; | |
case FORMULA -> cell.getCellFormula(); | |
case NUMERIC -> Double.toString(cell.getNumericCellValue()); | |
case STRING -> "\"" + cell.getRichStringCellValue() + "\""; | |
default -> throw new IllegalArgumentException("Unexpected cell type: " + cell.getCellType()); | |
}; | |
} catch (Throwable t) { | |
String cellString = cell.getStringCellValue(); | |
cellValue = "(\u001B[91mfailed to parse\u001B[0m) " + cellString; | |
cell.removeFormula(); | |
} | |
System.out.println(cellValue + "|"); | |
} | |
} | |
} | |
if (workbook.getAllNames().size() != 0) { | |
System.out.println("This workbook contains defined names: "); | |
for (var name : workbook.getAllNames()) { | |
System.out.print("* " + name.getNameName() + " in " + name.getSheetName() + ": "); | |
if (name.isFunctionName()) { | |
System.out.println("is a function"); | |
} else { | |
System.out.println("refers to " + name.getRefersToFormula()); | |
} | |
} | |
} | |
System.out.println(workbook.getInternalWorkbook().getRecords()); | |
workbook.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment