Skip to content

Commit

Permalink
Merge pull request #3 from roadrunner/master
Browse files Browse the repository at this point in the history
Thank you, nice done
  • Loading branch information
Tobias Seipke authored Jan 26, 2017
2 parents e413157 + 2d6da64 commit e5f3129
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ java -jar excel-to-json.jar -s sourcefile [options...]
-s,--source <arg> The source file which should be converted into json.
-?,--help This help text.
-df,--dateFormat The template to use for fomatting dates into strings.
-l,--rowLimit <arg> Limit the max number of rows to read.
-n,--maxSheets <arg> Limit the max number of sheets to read.
-o,--rowOffset <arg> Set the offset for begin to read.
-empty Include rows with no data in it.
-percent Parse percent values as floats.
-pretty To render output as pretty formatted json.
```

## output
Expand All @@ -30,3 +34,4 @@ java -jar excel-to-json.jar -s sourcefile [options...]
} ]
}
```

Binary file modified bin/excel-to-json.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main/java/net/nullpunkt/exceljson/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public static void main( String[] args ) throws Exception
options.addOption("s", "source", true, "The source file which should be converted into json.");
options.addOption("df", "dateFormat", true, "The template to use for fomatting dates into strings.");
options.addOption("?", "help", true, "This help text.");
options.addOption("n", "maxSheets", true, "Limit the max number of sheets to read.");
options.addOption("l", "rowLimit", true, "Limit the max number of rows to read.");
options.addOption("o", "rowOffset", true, "Set the offset for begin to read.");
options.addOption(new Option("percent", "Parse percent values as floats."));
options.addOption(new Option("empty", "Include rows with no data in it."));
options.addOption(new Option("pretty", "To render output as pretty formatted json."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ public ExcelWorkbook convert()
Workbook wb = WorkbookFactory.create(inp);

book.setFileName(config.getSourceFile());
int loopLimit = wb.getNumberOfSheets();
if (config.getNumberOfSheets() > 0 && loopLimit > config.getNumberOfSheets()) {
loopLimit = config.getNumberOfSheets();
}
int rowLimit = config.getRowLimit();
int startRowOffset = config.getRowOffset();
int currentRowOffset = -1;
int totalRowsAdded = 0;

for (int i = 0; i < wb.getNumberOfSheets(); i++) {

for (int i = 0; i < loopLimit; i++) {
Sheet sheet = wb.getSheetAt(i);
if (sheet == null) {
continue;
Expand All @@ -64,7 +72,15 @@ public ExcelWorkbook convert()
}
}
if(hasValues||!config.isOmitEmpty()) {
tmp.addRow(rowData);
currentRowOffset++;
if (rowLimit > 0 && totalRowsAdded == rowLimit) {
break;
}
if (startRowOffset > 0 && currentRowOffset < startRowOffset) {
continue;
}
tmp.addRow(rowData);
totalRowsAdded++;
}
}
if(config.isFillColumns()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ public class ExcelToJsonConverterConfig {
private boolean omitEmpty = false;
private boolean pretty = false;
private boolean fillColumns = false;
private int numberOfSheets = 0;
private int rowLimit = 0; // 0 -> no limit
private int rowOffset = 0;
private DateFormat formatDate = null;




public static ExcelToJsonConverterConfig create(CommandLine cmd) {
ExcelToJsonConverterConfig config = new ExcelToJsonConverterConfig();

Expand All @@ -25,11 +30,22 @@ public static ExcelToJsonConverterConfig create(CommandLine cmd) {
if(cmd.hasOption("df")) {
config.formatDate = new SimpleDateFormat(cmd.getOptionValue("df"));
}
if (cmd.hasOption("n")) {
config.setNumberOfSheets(Integer.valueOf(cmd.getOptionValue("n")));
}

if (cmd.hasOption("l")) {
config.setRowLimit(Integer.valueOf(cmd.getOptionValue("l")));
}
if (cmd.hasOption("o")) {
config.setRowOffset(Integer.valueOf(cmd.getOptionValue("o")));
}

config.parsePercentAsFloats = cmd.hasOption("percent");
config.omitEmpty = !cmd.hasOption("empty");
config.pretty = cmd.hasOption("pretty");
config.fillColumns = cmd.hasOption("fillColumns");


return config;
}
Expand All @@ -45,12 +61,36 @@ public String valid() {
if(!file.canRead()) {
return "Source file is not readable.";
}

return null;
}

// GET/SET


public int getRowLimit() {
return rowLimit;
}

public void setRowLimit(int rowLimit) {
this.rowLimit = rowLimit;
}

public int getRowOffset() {
return rowOffset;
}

public void setRowOffset(int rowOffset) {
this.rowOffset = rowOffset;
}

public int getNumberOfSheets()
{
return numberOfSheets;
}
public void setNumberOfSheets(int numberOfSheets)
{
this.numberOfSheets = numberOfSheets;
}
public String getSourceFile() {
return sourceFile;
}
Expand Down

0 comments on commit e5f3129

Please sign in to comment.