Search in sources :

Example 1 with WorkbookSettings

use of jxl.WorkbookSettings in project cubrid-manager by CUBRID.

the class PstmtDataTask method getRowParameterListFromExcel.

/**
	 *
	 * Get the parameter of PSTMT list from excel file
	 *
	 * @return List<List<PstmtParameter>>
	 */
private List<List<PstmtParameter>> getRowParameterListFromExcel() {
    // FIXME move this logic to core module
    File file = new File(fileName);
    Workbook workbook = null;
    List<List<PstmtParameter>> rowParaList = new ArrayList<List<PstmtParameter>>();
    try {
        if (fileCharset == null) {
            workbook = Workbook.getWorkbook(file);
        } else {
            WorkbookSettings workbookSettings = new WorkbookSettings();
            workbookSettings.setEncoding(fileCharset);
            workbook = Workbook.getWorkbook(file, workbookSettings);
        }
        Sheet[] sheets = workbook.getSheets();
        for (int sheetNum = 0; sheetNum < sheets.length; sheetNum++) {
            Sheet sheet = sheets[sheetNum];
            int rows = sheet.getRows();
            int start = 0;
            if (isFirstRowAsColumn) {
                start = 1;
            }
            for (int i = start; i < rows; i++) {
                List<PstmtParameter> paraList = new ArrayList<PstmtParameter>();
                for (int j = 0; j < parameterList.size(); j++) {
                    PstmtParameter pstmtParameter = parameterList.get(j);
                    PstmtParameter newParam = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
                    int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                    Cell cell = sheet.getCell(column, i);
                    String content = null;
                    String pattern = null;
                    if (cell == null) {
                        content = null;
                    } else if (cell instanceof EmptyCell) {
                        content = null;
                    } else {
                        content = cell.getContents();
                        CellFormat format = cell.getCellFormat();
                        if (format != null && format.getFormat() != null) {
                            pattern = format.getFormat().getFormatString();
                        }
                    }
                    String dataType = DataType.getRealType(pstmtParameter.getDataType());
                    content = FieldHandlerUtils.getRealValueForImport(dataType, content, parentFile);
                    FormatDataResult formatDataResult = null;
                    if (StringUtil.isEmpty(pattern)) {
                        formatDataResult = DBAttrTypeFormatter.format(dataType, content, false, dbCharset, true);
                    } else {
                        formatDataResult = DBAttrTypeFormatter.format(dataType, content, pattern, false, dbCharset, true);
                    }
                    if (formatDataResult.isSuccess()) {
                        newParam.setCharSet(fileCharset);
                        newParam.setParamValue(content);
                    } else {
                        dataTypeErrorHandling(getErrorMsg(i, column, dataType));
                        newParam.setCharSet(fileCharset);
                        newParam.setParamValue(null);
                    }
                    paraList.add(newParam);
                }
                rowParaList.add(paraList);
            }
        }
    } catch (BiffException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    } finally {
        if (workbook != null) {
            workbook.close();
        }
    }
    return rowParaList;
}
Also used : BiffException(jxl.read.biff.BiffException) CellFormat(jxl.format.CellFormat) ArrayList(java.util.ArrayList) EmptyCell(jxl.biff.EmptyCell) WorkbookSettings(jxl.WorkbookSettings) IOException(java.io.IOException) Workbook(jxl.Workbook) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Sheet(jxl.Sheet) Cell(jxl.Cell) EmptyCell(jxl.biff.EmptyCell)

Example 2 with WorkbookSettings

use of jxl.WorkbookSettings in project cubrid-manager by CUBRID.

the class XLSImportFileHandler method getSourceFileInfo.

/**
	 * Get the source file information
	 *
	 * @return ImportFileDescription
	 * @throws Exception in process.
	 */
public ImportFileDescription getSourceFileInfo() throws Exception {
    // FIXME move this logic to core module
    synchronized (this) {
        if (importFileDescription == null) {
            final List<String> colsLst = new ArrayList<String>();
            final List<Integer> itemsNumberOfSheets = new ArrayList<Integer>();
            importFileDescription = new ImportFileDescription(0, 0, colsLst);
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                public void run(final IProgressMonitor monitor) {
                    monitor.beginTask("", IProgressMonitor.UNKNOWN);
                    Workbook workbook = null;
                    int totalRowCount = 0;
                    int sheetNum = 0;
                    try {
                        if (fileCharset == null) {
                            workbook = Workbook.getWorkbook(new File(fileName));
                        } else {
                            WorkbookSettings workbookSettings = new WorkbookSettings();
                            workbookSettings.setEncoding(fileCharset);
                            workbook = Workbook.getWorkbook(new File(fileName), workbookSettings);
                        }
                        // get column count and total row count
                        sheetNum = workbook.getNumberOfSheets();
                        if (sheetNum > 0) {
                            int columnCount = workbook.getSheet(0).getColumns();
                            for (int j = 0; !monitor.isCanceled() && j < columnCount; j++) {
                                Cell cell = workbook.getSheet(0).getCell(j, 0);
                                //$NON-NLS-1$
                                colsLst.add(cell == null ? "" : cell.getContents());
                            }
                        }
                        for (int i = 0; !monitor.isCanceled() && i < sheetNum; i++) {
                            int rowsInSheet = workbook.getSheet(i).getRows();
                            itemsNumberOfSheets.add(Integer.valueOf(rowsInSheet));
                            totalRowCount += rowsInSheet;
                        }
                        if (monitor.isCanceled()) {
                            throw new InterruptedException();
                        }
                    } catch (Exception e) {
                        LOGGER.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                    } finally {
                        importFileDescription.setSheetNum(sheetNum);
                        importFileDescription.setTotalCount(totalRowCount);
                        importFileDescription.setFirstRowCols(colsLst);
                        importFileDescription.setItemsNumberOfSheets(itemsNumberOfSheets);
                        if (workbook != null) {
                            workbook.close();
                        }
                        monitor.done();
                    }
                }
            };
            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
        }
        return importFileDescription;
    }
}
Also used : ArrayList(java.util.ArrayList) WorkbookSettings(jxl.WorkbookSettings) Workbook(jxl.Workbook) BiffException(jxl.read.biff.BiffException) IOException(java.io.IOException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) File(java.io.File) Cell(jxl.Cell)

Example 3 with WorkbookSettings

use of jxl.WorkbookSettings in project cubrid-manager by CUBRID.

the class RunSQLProcessManager method createExcel.

/**
	 * create excel workbook
	 * @param excelFile
	 * @return
	 */
public WritableWorkbook createExcel(File excelFile) {
    WritableWorkbook wwb = null;
    try {
        WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setEncoding(charset);
        wwb = Workbook.createWorkbook(excelFile, workbookSettings);
    } catch (Exception e) {
        LOGGER.error("create excel error", e);
    }
    return wwb;
}
Also used : WritableWorkbook(jxl.write.WritableWorkbook) WorkbookSettings(jxl.WorkbookSettings) WriteException(jxl.write.WriteException)

Example 4 with WorkbookSettings

use of jxl.WorkbookSettings in project cubrid-manager by CUBRID.

the class ExportHostStatusDialog method saveAllData.

private boolean saveAllData() {
    String fileName = saveExcelPath.getText() + saveExcelName.getText() + ".xls";
    WorkbookSettings workbookSettings = new WorkbookSettings();
    workbookSettings.setEncoding(fileCharsetCombo.getText());
    boolean isOK = true;
    try {
        wwb = Workbook.createWorkbook(new File(fileName), workbookSettings);
        /*System info*/
        saveTableData(com.cubrid.cubridmanager.ui.host.Messages.titleServerInfo, editor.getDbServerInfoText().getText(), com.cubrid.cubridmanager.ui.host.Messages.titleServerInfo, 0);
        /*DB info*/
        saveDBInfoData(editor.getDatabaseTable(), com.cubrid.cubridmanager.ui.host.Messages.titleDBInfo, 1);
        /*DB volume info*/
        saveTableData(editor.getVolumeTableViewer().getTable(), com.cubrid.cubridmanager.ui.host.Messages.titleVolumeInfo, 2);
        /*Broker info*/
        saveTableData(editor.getBrokerTableViewer().getTable(), com.cubrid.cubridmanager.ui.host.Messages.titleBrokerInfo, 3);
        /*System status*/
        saveTableData(editor.getServerTableViewer().getTable(), com.cubrid.cubridmanager.ui.host.Messages.titleSystemInfo, 4);
        wwb.write();
    } catch (IOException e) {
        isOK = false;
        LOGGER.error("Export to error", e);
    } catch (RowsExceededException e) {
        isOK = false;
        LOGGER.error("Export to error", e);
    } catch (WriteException e) {
        isOK = false;
        LOGGER.error("Export to error", e);
    } finally {
        if (wwb != null) {
            try {
                wwb.close();
            } catch (Exception ex) {
                LOGGER.error("close excel stream error", ex);
            }
        }
    }
    return isOK;
}
Also used : WriteException(jxl.write.WriteException) WorkbookSettings(jxl.WorkbookSettings) IOException(java.io.IOException) File(java.io.File) WriteException(jxl.write.WriteException) IOException(java.io.IOException) RowsExceededException(jxl.write.biff.RowsExceededException) RowsExceededException(jxl.write.biff.RowsExceededException)

Example 5 with WorkbookSettings

use of jxl.WorkbookSettings in project cubrid-manager by CUBRID.

the class ExportDashboardDialog method exportTableList.

/**
	 * export dashboard table list
	 * @return boolean sucess
	 */
public boolean exportTableList() throws Exception {
    WritableWorkbook wwb = null;
    WritableSheet ws = null;
    String fileName = saveExcelPath.getText() + saveExcelName.getText() + ".xls";
    try {
        WritableCellFormat normalCellStyle = getNormalCell();
        WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setEncoding(fileCharsetCombo.getText());
        wwb = Workbook.createWorkbook(new File(fileName), workbookSettings);
        for (int i = 0; i < exportTableList.size(); i++) {
            ws = wwb.createSheet(sheetNames[i], i);
            Table table = exportTableList.get(i);
            int rowIndex = 0;
            //title
            for (int j = 0; j < table.getColumnCount(); j++) {
                String cellString = table.getColumn(j).getText();
                ws.addCell(new jxl.write.Label(j, rowIndex, cellString, normalCellStyle));
                ws.setColumnView(j, 30);
            }
            rowIndex++;
            //row
            for (int j = 0; j < table.getItemCount(); j++) {
                TableItem tableItem = table.getItem(j);
                for (int k = 0; k < table.getColumnCount(); k++) {
                    String cellString = tableItem.getText(k);
                    ws.addCell(new jxl.write.Label(k, rowIndex, cellString, normalCellStyle));
                }
                rowIndex++;
            }
        }
        wwb.write();
        return true;
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        throw e;
    } finally {
        if (wwb != null) {
            try {
                wwb.close();
            } catch (Exception ex) {
                LOGGER.error("close excel stream error", ex);
            }
        }
    }
}
Also used : WritableWorkbook(jxl.write.WritableWorkbook) Table(org.eclipse.swt.widgets.Table) TableItem(org.eclipse.swt.widgets.TableItem) WritableSheet(jxl.write.WritableSheet) WorkbookSettings(jxl.WorkbookSettings) WritableCellFormat(jxl.write.WritableCellFormat) File(java.io.File) WriteException(jxl.write.WriteException)

Aggregations

WorkbookSettings (jxl.WorkbookSettings)25 WritableWorkbook (jxl.write.WritableWorkbook)16 WritableSheet (jxl.write.WritableSheet)14 File (java.io.File)11 IOException (java.io.IOException)9 WritableCellFormat (jxl.write.WritableCellFormat)7 WriteException (jxl.write.WriteException)7 Locale (java.util.Locale)6 List (java.util.List)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 WritableFont (jxl.write.WritableFont)4 FileNotFoundException (java.io.FileNotFoundException)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 Workbook (jxl.Workbook)3 BiffException (jxl.read.biff.BiffException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 Cell (jxl.Cell)2