fix import multiple excel with mulitple sheets issue #1328

This commit is contained in:
Jacky 2017-11-15 22:58:47 -05:00
parent c2db998b93
commit 6f42df95e2
2 changed files with 30 additions and 14 deletions

View File

@ -44,6 +44,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLException;
import org.apache.poi.common.usermodel.Hyperlink;
@ -54,6 +55,7 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -83,9 +85,9 @@ public class ExcelImporter extends TabularImportingParserBase {
JSONArray sheetRecords = new JSONArray();
JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
try {
if (fileRecords.size() > 0) {
JSONObject firstFileRecord = fileRecords.get(0);
File file = ImportingUtilities.getFile(job, firstFileRecord);
for (int index = 0;index < fileRecords.size();index++) {
JSONObject fileRecord = fileRecords.get(index);
File file = ImportingUtilities.getFile(job, fileRecord);
InputStream is = new FileInputStream(file);
if (!is.markSupported()) {
@ -98,19 +100,18 @@ public class ExcelImporter extends TabularImportingParserBase {
new HSSFWorkbook(new POIFSFileSystem(is));
int sheetCount = wb.getNumberOfSheets();
boolean hasData = false;
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = wb.getSheetAt(i);
int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
JSONObject sheetRecord = new JSONObject();
JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName());
JSONUtilities.safePut(sheetRecord, "name", file.getName() + "#" + sheet.getSheetName());
JSONUtilities.safePut(sheetRecord, "fileNameAndSheetIndex", file.getName() + "#" + i);
JSONUtilities.safePut(sheetRecord, "rows", rows);
if (hasData) {
JSONUtilities.safePut(sheetRecord, "selected", false);
} else if (rows > 1) {
if (rows > 1) {
JSONUtilities.safePut(sheetRecord, "selected", true);
hasData = true;
} else {
JSONUtilities.safePut(sheetRecord, "selected", false);
}
JSONUtilities.append(sheetRecords, sheetRecord);
}
@ -181,9 +182,22 @@ public class ExcelImporter extends TabularImportingParserBase {
return;
}
int[] sheets = JSONUtilities.getIntArray(options, "sheets");
for (int sheetIndex : sheets) {
final Sheet sheet = wb.getSheetAt(sheetIndex);
JSONArray sheets = JSONUtilities.getArray(options, "sheets");
for(int i=0;i<sheets.length();i++) {
String[] fileNameAndSheetIndex = new String[2];
try {
JSONObject sheetObj = sheets.getJSONObject(i);
// value is fileName#sheetIndex
fileNameAndSheetIndex = sheetObj.getString("fileNameAndSheetIndex").split("#");
} catch (JSONException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
if (!fileNameAndSheetIndex[0].equals(fileSource))
continue;
final Sheet sheet = wb.getSheetAt(Integer.parseInt(fileNameAndSheetIndex[1]));
final int lastRow = sheet.getLastRowNum();
TableDataReader dataReader = new TableDataReader() {

View File

@ -76,10 +76,12 @@ Refine.ExcelParserUI.prototype.getOptions = function() {
}
return def;
};
var self = this;
this._optionContainerElmts.sheetRecordContainer.find('input').each(function() {
if (this.checked) {
options.sheets.push(parseInt(this.getAttribute('index'),10));
options.sheets.push(self._config.sheetRecords[parseInt(this.getAttribute('index'))]);
}
});