Search in sources :

Example 16 with WorkbookSettings

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

the class Export method exportXls.

/**
	 * export all data in Query Editor result table cache as xls
	 *
	 * @param monitor IProgressMonitor
	 * @throws IOException if failed
	 * @throws FileNotFoundException if failed
	 * @throws UnsupportedEncodingException if failed
	 * @throws RowsExceededException if failed
	 * @throws NumberFormatException if failed
	 * @throws WriteException if failed
	 *
	 */
private void exportXls(final IProgressMonitor monitor) throws IOException, FileNotFoundException, UnsupportedEncodingException, RowsExceededException, NumberFormatException, WriteException {
    // FIXME move this logic to core module
    workbook = null;
    try {
        if (fileCharset == null || fileCharset.trim().length() == 0) {
            workbook = Workbook.createWorkbook(file);
        } else {
            WorkbookSettings workbookSettings = new WorkbookSettings();
            workbookSettings.setEncoding(fileCharset);
            workbook = Workbook.createWorkbook(file, workbookSettings);
        }
        int totalSheetNum = 0;
        // 65536: limit xls row number except the column row
        final int rowLimit = ImportFileConstants.XLS_ROW_LIMIT - 1;
        // 256: limit xls column number..
        final int columnLimit = ImportFileConstants.XLS_COLUMN_LIMIT;
        for (int k = 0; k < resultDataList.size(); k++) {
            List<ColumnInfo> columnList = resultColsList.get(k);
            List<Map<String, String>> dataList = resultDataList.get(k);
            WritableSheet sheet = workbook.createSheet("Sheet " + (k + 1), totalSheetNum++);
            int colCount = columnList.size();
            int itemCount = dataList.size();
            if (colCount > columnLimit) {
                if (!CommonUITool.openConfirmBox(Messages.columnCountOver)) {
                    return;
                }
                colCount = columnLimit;
            }
            //export columns
            exportColumnsForXls(sheet, k, columnLimit);
            int sheetNum = 0;
            for (int i = 0, xlsRecordNum = 1; i < itemCount; i++) {
                if (!CommonUITool.isAvailableMemory(ExportTableDataTask.REMAINING_MEMORY_SIZE)) {
                    throw new OutOfMemoryError();
                }
                int start = 0;
                for (int j = start; j < colCount; j++) {
                    String colType = columnList.get(j).getType();
                    String colIndex = columnList.get(j).getIndex();
                    String value = dataList.get(i).get(colIndex);
                    int colNumber = j - start;
                    FieldHandlerUtils.setValue2XlsCell(sheet, colNumber, xlsRecordNum, colType, value);
                }
                xlsRecordNum++;
                if (((i + 1) % rowLimit) == 0 && (i + 1) < itemCount) {
                    sheetNum++;
                    sheet = workbook.createSheet("Sheet " + (k + 1) + "_" + sheetNum, totalSheetNum++);
                    exportColumnsForXls(sheet, k, columnLimit);
                    xlsRecordNum = 1;
                }
                exportedCount++;
                monitor.subTask(Messages.bind(com.cubrid.common.ui.cubrid.table.Messages.msgExportDataRow, exportedCount));
            }
        }
    } finally {
        try {
            if (workbook != null) {
                workbook.write();
            }
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
            } catch (WriteException e) {
                LOGGER.error("", e);
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : WriteException(jxl.write.WriteException) WorkbookSettings(jxl.WorkbookSettings) WritableSheet(jxl.write.WritableSheet) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

WorkbookSettings (jxl.WorkbookSettings)16 WritableWorkbook (jxl.write.WritableWorkbook)10 File (java.io.File)9 WritableSheet (jxl.write.WritableSheet)8 IOException (java.io.IOException)7 WritableCellFormat (jxl.write.WritableCellFormat)6 WriteException (jxl.write.WriteException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 WritableFont (jxl.write.WritableFont)3 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Cell (jxl.Cell)2 Workbook (jxl.Workbook)2 BiffException (jxl.read.biff.BiffException)2 Label (jxl.write.Label)2 Constraint (com.cubrid.common.core.common.model.Constraint)1 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)1