use of org.pentaho.di.core.spreadsheet.KWorkbook in project pentaho-kettle by pentaho.
the class ExcelInputDialog method getSheets.
/**
* Get the names of the sheets from the Excel workbooks and let the user select some or all of them.
*/
public void getSheets() {
List<String> sheetnames = new ArrayList<String>();
ExcelInputMeta info = new ExcelInputMeta();
getInfo(info);
FileInputList fileList = info.getFileList(transMeta);
for (FileObject fileObject : fileList.getFiles()) {
try {
KWorkbook workbook = WorkbookFactory.getWorkbook(info.getSpreadSheetType(), KettleVFS.getFilename(fileObject), info.getEncoding(), wPassword.getText());
int nrSheets = workbook.getNumberOfSheets();
for (int j = 0; j < nrSheets; j++) {
KSheet sheet = workbook.getSheet(j);
String sheetname = sheet.getName();
if (Const.indexOfString(sheetname, sheetnames) < 0) {
sheetnames.add(sheetname);
}
}
workbook.close();
} catch (Exception e) {
new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "ExcelInputDialog.ErrorReadingFile.DialogMessage", KettleVFS.getFilename(fileObject)), e);
}
}
// Put it in an array:
String[] lst = sheetnames.toArray(new String[sheetnames.size()]);
// Let the user select the sheet-names...
EnterListDialog esd = new EnterListDialog(shell, SWT.NONE, lst);
String[] selection = esd.open();
if (selection != null) {
for (int j = 0; j < selection.length; j++) {
wSheetnameList.add(new String[] { selection[j], "" });
}
wSheetnameList.removeEmptyRows();
wSheetnameList.setRowNums();
wSheetnameList.optWidth(true);
checkAlerts();
}
}
use of org.pentaho.di.core.spreadsheet.KWorkbook in project pentaho-kettle by pentaho.
the class StaxWorkBookIT method testReadCells.
@Test
public void testReadCells() throws Exception {
KWorkbook workbook = getWorkbook(sample, null);
KSheet sheet = workbook.getSheet(0);
KCell cell = sheet.getCell(1, 2);
assertEquals("One", cell.getValue());
assertEquals(KCellType.LABEL, cell.getType());
cell = sheet.getCell(2, 2);
assertEquals(KCellType.DATE, cell.getType());
assertEquals(new Date(1283817600000L), cell.getValue());
cell = sheet.getCell(1, 3);
assertEquals("Two", cell.getValue());
assertEquals(KCellType.LABEL, cell.getType());
}
use of org.pentaho.di.core.spreadsheet.KWorkbook in project pentaho-kettle by pentaho.
the class StaxWorkBookIT method testReadRowRA.
@Test
public void testReadRowRA() throws Exception {
KWorkbook workbook = getWorkbook(sample, null);
KSheet sheet1 = workbook.getSheet(0);
KCell[] row = sheet1.getRow(4);
assertEquals("Three", row[1].getValue());
row = sheet1.getRow(2);
assertEquals("One", row[1].getValue());
}
use of org.pentaho.di.core.spreadsheet.KWorkbook in project pentaho-kettle by pentaho.
the class ExcelInputDialogTest method getFieldsTest.
@Test
public /**
* http://jira.pentaho.com/browse/PDI-13930
*/
void getFieldsTest() throws Exception {
ExcelInputDialog dialog = Mockito.mock(ExcelInputDialog.class);
RowMeta fields = new RowMeta();
ExcelInputMeta info = Mockito.mock(ExcelInputMeta.class);
Mockito.doReturn(true).when(info).readAllSheets();
int[] startColumn = { 0 };
Mockito.doReturn(startColumn).when(info).getStartColumn();
int[] startRow = { 0 };
Mockito.doReturn(startRow).when(info).getStartRow();
KWorkbook workbook = Mockito.mock(KWorkbook.class);
Mockito.doReturn(1).when(workbook).getNumberOfSheets();
KSheet sheet = Mockito.mock(KSheet.class);
Mockito.doReturn(sheet).when(workbook).getSheet(0);
KCell cell = Mockito.mock(KCell.class);
int fieldCount = 400;
for (int i = 0; i <= fieldCount - 1; i++) {
Mockito.doReturn(cell).when(sheet).getCell(i, 0);
Mockito.doReturn(cell).when(sheet).getCell(i, 1);
}
Mockito.doReturn("testValue").when(cell).getContents();
Mockito.doReturn(KCellType.NUMBER).when(cell).getType();
PluginRegistry pluginRegistry = Mockito.mock(PluginRegistry.class);
PluginInterface stringPlugin = Mockito.mock(PluginInterface.class);
Mockito.doReturn(stringPlugin).when(pluginRegistry).getPlugin(ValueMetaPluginType.class, "1");
Mockito.doReturn(Mockito.mock(ValueMetaInterface.class)).when(pluginRegistry).loadClass(stringPlugin, ValueMetaInterface.class);
ValueMetaFactory.pluginRegistry = pluginRegistry;
Method processingWorkbookMethod = ExcelInputDialog.class.getDeclaredMethod("processingWorkbook", RowMetaInterface.class, ExcelInputMeta.class, KWorkbook.class);
processingWorkbookMethod.setAccessible(true);
processingWorkbookMethod.invoke(dialog, fields, info, workbook);
Assert.assertEquals(fieldCount, fields.size());
}
Aggregations