Search in sources :

Example 1 with IExcelScreenHandler

use of com.kyj.fx.voeditor.visual.framework.excel.IExcelScreenHandler in project Gargoyle by callakrsos.

the class FxExcelUtil method createExcel.

/**
	 *  엑셀파일을 생성한다.
	 * @작성자 : KYJ
	 * @작성일 : 2016. 9. 6.
	 *  
	 * @param exportExcelFile 
	 *   	export하려는 파일 확장자는 .xlsx 사용 권고
	 *   
	 * @param dataSource 
	 *      데이터셋.  개발자 혼동(순서)을 피하기 위해 LinkedMap(순서유지)을 강제적으로 사용
	 *      값은 Map<Sheet명, Map<컬럼메타,List<값>> 순 매핑이되어있다.
	 * 
	 * @param handler
	 *     데이터 컨버터, 데이터내에 특화가 필요한 처리가 필요한경우 구현하여 사용하도록한다.
	 *     
	 * @throws Exception
	 */
public static void createExcel(File exportExcelFile, LinkedHashMap<String, LinkedHashMap<ExcelColumnExpression, List<Object>>> dataSource, IExcelDataSetHandler<Sheet, LinkedHashMap<ExcelColumnExpression, List<Object>>> handler, Map<String, Map<String, String>> metadata, boolean overwrite) throws Exception {
    //파일이 이미 존재하는 상황에서 overrite하려는 경우 에러.
    if (exportExcelFile.exists() && !overwrite) {
        throw new GargoyleException(String.format("output File : {%s} already exists.!", exportExcelFile.getName()));
    }
    Workbook createNewWorkBookXlsx = createNewWorkBookXlsx();
    //시트를 의미하는 iterator.
    Iterator<String> iterator = dataSource.keySet().iterator();
    int sheetIndex = 0;
    while (iterator.hasNext()) {
        String sheetName = iterator.next();
        Sheet createSheet = createNewWorkBookXlsx.createSheet(sheetName);
        Map<String, String> meta = metadata.get(sheetName);
        if (meta == null || meta.isEmpty())
            continue;
        //각 헤더컬럼의 개수(높이)
        String columnMaxHeight = meta.get($$META_COLUMN_MAX_HEIGHT$$);
        int maxColumnRowSize = ValueUtil.decode(columnMaxHeight, val -> Integer.parseInt(val), () -> 0);
        LinkedHashMap<ExcelColumnExpression, List<Object>> linkedHashMap = dataSource.get(sheetName);
        createHeaders(handler, createSheet, linkedHashMap, maxColumnRowSize);
        //컬럼 + 데이터리스트로 이루어진 데이터셋
        LinkedHashMap<ExcelColumnExpression, List<Object>> dataSet = dataSource.get(sheetName);
        drawBody(handler, createSheet, maxColumnRowSize, dataSet);
        //(short) (createSheet.getRow(maxColumnRowSize + 1).getLastCellNum() - 1);
        short lastColumnNum = (short) dataSet.size();
        IExcelScreenHandler screenHandler = handler.getExcelScreenHandler();
        if (handler.isApplyAutoFit()) {
            for (int i = 0; i < lastColumnNum; i++) {
                createSheet.autoSizeColumn(i);
            }
        }
        if (screenHandler != null) {
            //사용자 정의 sheet 처리를 지원한다.
            screenHandler.customSheetHandle(sheetIndex, createSheet);
            sheetIndex++;
        }
        //로고 이미지 처리.
        Utils.createDefaultLogo(createSheet);
        createSheet.setAutoFilter(new CellRangeAddress((maxColumnRowSize), (maxColumnRowSize), START_COLUMN_INDEX, lastColumnNum));
        createSheet.createFreezePane(0, (maxColumnRowSize + 1));
    }
    //사용자 정의 workbook 처리를 지원함.
    IExcelScreenHandler excelScreenHandler = handler.getExcelScreenHandler();
    if (excelScreenHandler != null)
        excelScreenHandler.customWorkbookHandle(createNewWorkBookXlsx);
    //파일 write처리.
    try (FileOutputStream fileOutputStream = new FileOutputStream(exportExcelFile)) {
        createNewWorkBookXlsx.write(fileOutputStream);
    }
}
Also used : IExcelScreenHandler(com.kyj.fx.voeditor.visual.framework.excel.IExcelScreenHandler) GargoyleException(com.kyj.fx.voeditor.visual.exceptions.GargoyleException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) ExcelColumnExpression(com.kyj.fx.voeditor.visual.framework.excel.ExcelColumnExpression) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) ObservableList(javafx.collections.ObservableList) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 2 with IExcelScreenHandler

use of com.kyj.fx.voeditor.visual.framework.excel.IExcelScreenHandler in project Gargoyle by callakrsos.

the class FxExcelUtil method createExcel.

/**
	 * @작성자 : KYJ
	 * @작성일 : 2016. 9. 7. 
	 * @param screen
	 * @param exportExcelFile
	 * @param tableViewList
	 * @param overrite
	 * @throws Exception
	 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void createExcel(IExcelScreenHandler screen, File exportExcelFile, List<TableView> tableViewList, boolean overrite) throws Exception {
    IExcelScreenHandler screenHandler = screen;
    /*
		 * Key : Sheet
		 * Value - Key :  ExcelColumnExpression
		 * Value - Value :  Object
		 */
    LinkedHashMap<String, LinkedHashMap<ExcelColumnExpression, List<Object>>> dataSet = new LinkedHashMap<>();
    Map<String, Map<String, String>> metadata = new HashMap<>();
    int sheetIndex = 0;
    for (TableView table : tableViewList) {
        Predicate<TableView<?>> useTableViewForExcel = screenHandler.useTableViewForExcel();
        if (useTableViewForExcel != null) {
            if (!useTableViewForExcel.test(table)) {
                continue;
            }
        }
        //Sheet.
        String sheetName = screenHandler.toSheetName(table);
        if (sheetName == null) {
            sheetName = String.format(DEFAULT_SHEET_NAME_FORMAT, sheetIndex++);
        }
        ObservableList<TableColumn> columns = table.getColumns();
        //계층형 테이블컬럼의 모든 값을 찾아냄.
        ArrayList<ExcelColumnExpression> allColumnsList = new ArrayList<ExcelColumnExpression>();
        int maxLevel = getMaxLevel(columns, /*ExcelColumnExpression :: 계층형 테이블컬럼들을 일렬로 찾아낸 리스트 */
        allColumnsList, screenHandler.useTableColumnForExcel());
        dataSet.put(sheetName, getDataSource(screen, table, allColumnsList));
        HashMap<String, String> meta = new HashMap<String, String>();
        meta.put($$META_COLUMN_MAX_HEIGHT$$, String.valueOf(maxLevel));
        metadata.put(sheetName, meta);
    }
    if (dataSet.isEmpty())
        dataSet.put("empty", new LinkedHashMap<>());
    createExcel(screenHandler, exportExcelFile, dataSet, metadata, overrite);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IExcelScreenHandler(com.kyj.fx.voeditor.visual.framework.excel.IExcelScreenHandler) ArrayList(java.util.ArrayList) TableColumn(javafx.scene.control.TableColumn) ExcelColumnExpression(com.kyj.fx.voeditor.visual.framework.excel.ExcelColumnExpression) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TableView(javafx.scene.control.TableView)

Aggregations

ExcelColumnExpression (com.kyj.fx.voeditor.visual.framework.excel.ExcelColumnExpression)2 IExcelScreenHandler (com.kyj.fx.voeditor.visual.framework.excel.IExcelScreenHandler)2 ArrayList (java.util.ArrayList)2 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ObservableList (javafx.collections.ObservableList)1 TableColumn (javafx.scene.control.TableColumn)1 TableView (javafx.scene.control.TableView)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1