Example usage for org.apache.poi.hssf.usermodel HSSFSheet getSheetName

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet getSheetName

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFSheet getSheetName.

Prototype

@SuppressWarnings("resource")
@Override
public String getSheetName() 

Source Link

Document

Returns the name of this sheet

Usage

From source file:com.wangzhu.poi.ExcelToHtmlConverter.java

License:Apache License

protected void processSheetHeader(Element htmlBody, HSSFSheet sheet) {
    Element h2 = this.htmlDocumentFacade.createHeader2();
    h2.appendChild(this.htmlDocumentFacade.createText(sheet.getSheetName()));
    htmlBody.appendChild(h2);//from  ww  w. j a v  a  2  s.  c o  m
}

From source file:com.zhu.action.CarAction.java

public void exportpeople(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) {/*from   w w w .  jav a 2 s.  c o  m*/
    CarForm carForm = (CarForm) form;

    // webbookExcel
    HSSFWorkbook wb = new HSSFWorkbook();
    // webbooksheet,Excelsheet
    HSSFSheet sheet = wb.createSheet("?");
    // sheet0,??poiExcel?short
    HSSFRow row = sheet.createRow((int) 0);
    // ? 
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell(1);
    cell.setCellValue("???");
    cell.setCellStyle(style);
    // ? ??
    OrderService orderService = new OrderService();
    List<Orderinfo> list = orderService.getOrderCarDetail(carForm.getId());
    System.out.println(list.size());
    for (int i = 0; i < list.size(); i++) {
        row = sheet.createRow(i + 1);
        Orderinfo order = (Orderinfo) list.get(i);
        // ?
        row.createCell(0).setCellValue(order.getN1());
        row.createCell(1).setCellValue(order.getI1());
        row = sheet.createRow(i + list.size() + 1);
        row.createCell(0).setCellValue(order.getN2());
        row.createCell(1).setCellValue(order.getI2());
        row = sheet.createRow(i + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN3());
        row.createCell(1).setCellValue(order.getI3());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN4());
        row.createCell(1).setCellValue(order.getI4());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN5());
        row.createCell(1).setCellValue(order.getI5());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN6());
        row.createCell(1).setCellValue(order.getI6());
        row = sheet.createRow(
                i + list.size() + list.size() + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN7());
        row.createCell(1).setCellValue(order.getI7());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN8());
        row.createCell(1).setCellValue(order.getI8());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN9());
        row.createCell(1).setCellValue(order.getI9());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN10());
        row.createCell(1).setCellValue(order.getI10());
    }
    // ?
    String filePath = "";
    Date dt = new Date();
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    String date = df.format(dt).toString();
    filePath = "/Users/Nemo/Documents/carpeople" + date + ".xls";
    File file = new File(filePath);

    try {
        // FileOutputStream fout = new FileOutputStream(
        // "/Users/Nemo/Documents/carpeople.xls");
        // wb.write(fout);
        // fout.close();
        OutputStream out = new FileOutputStream(file);
        wb.write(out);
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    int key = 0;
    int MaxRowNum = 0, MaxCellNum = 0;
    try {
        FileInputStream in = new FileInputStream(filePath);
        POIFSFileSystem fs = new POIFSFileSystem(in);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        FileOutputStream out = new FileOutputStream("/Users/Nemo/Documents/carpeople" + date + ".xls");
        int number = workbook.getNumberOfSheets();
        for (int i = 0; i < number; i++) { // ?sheet
            sheet = workbook.getSheetAt(i); // 14
            System.out.println("" + sheet.getSheetName() + " ? "
                    + (sheet.getLastRowNum() + 1));
            MaxRowNum = 0;
            for (int k = 0; k <= sheet.getLastRowNum(); k++) {
                HSSFRow hRow = sheet.getRow(k);
                // System.out.println((k + 1) + "");
                if (isBlankRow(hRow)) // 
                {
                    int m = 0;
                    for (m = k + 1; m <= sheet.getLastRowNum(); m++) {
                        HSSFRow nhRow = sheet.getRow(m);
                        if (!isBlankRow(nhRow)) {
                            // System.out.println("?" + (m + 1));
                            sheet.shiftRows(m, sheet.getLastRowNum(), k - m);
                            break;
                        }
                    }
                    if (m > sheet.getLastRowNum())
                        break; // ?
                } else { // ?
                    MaxRowNum++;
                    if (MaxCellNum < hRow.getLastCellNum())
                        MaxCellNum = hRow.getLastCellNum();
                }
            }
            workbook.setPrintArea(i, 0, MaxCellNum, 0, MaxRowNum);
            System.out
                    .println("?? " + sheet.getSheetName() + "  " + MaxRowNum);
        }
        workbook.write(out);
        in.close();
        out.close();
    } catch (IOException e) {
        System.out.println(key + " " + e.getMessage() + " ");
        e.printStackTrace();

    }

    System.out.println("??");

}

From source file:com.zxy.commons.poi.excel.ExcelUtils.java

License:Apache License

/**
 * ?Excelsheet//from   w  ww  .j  a  v a 2 s. c om
 * 
 * @param inputPath ???Excel
 * @return Excel?
 * @throws IOException IOException
 */
public static Map<String, Table<Integer, String, String>> readAll2table(String inputPath) throws IOException {
    Map<String, Table<Integer, String, String>> tables = Maps.newLinkedHashMap();
    FileInputStream inputStream = null;
    HSSFWorkbook wb = null;
    try {
        inputStream = new FileInputStream(inputPath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        // HSSFWorkbook
        POIFSFileSystem fs = new POIFSFileSystem(bufferedInputStream);
        wb = new HSSFWorkbook(fs);
        List<String> columnNames = Lists.newLinkedList();
        for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
            Table<Integer, String, String> table = TreeBasedTable.create();
            HSSFSheet st = wb.getSheetAt(sheetIndex);
            String sheetName = st.getSheetName();
            for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
                HSSFRow row = st.getRow(rowIndex);
                for (int columnIndex = 0; columnIndex < row.getLastCellNum(); columnIndex++) {
                    HSSFCell cell = row.getCell(columnIndex);
                    if (cell != null) {
                        if (rowIndex == 0) { // 
                            columnNames.add(cell.getStringCellValue());
                        } else {
                            String value = cell.getStringCellValue();
                            table.put(rowIndex, columnNames.get(columnIndex), value);
                        }
                    }
                }
            }
            tables.put(sheetName, table);
        }
        return tables;
    } finally {
        if (wb != null) {
            wb.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

From source file:data.services.FreeOptionService.java

public void updateFromXml(File fl) {
    try {/*from   w w w .  j  av  a2 s  . c  om*/
        FileInputStream fi = new FileInputStream(fl);

        int i = 1;
        String listName = "? .";
        int s = 39191091;
        List<FreeOption> foForSave = new ArrayList();
        List<FreeOption> foForUpd = new ArrayList();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(fi);
            int sheetNumber = workbook.getNumberOfSheets();
            while (i < sheetNumber) {
                HSSFSheet sheet = workbook.getSheetAt(i);
                i++;
                listName = sheet.getSheetName();
                Iterator<Row> it = sheet.iterator();
                Car car = new Car();
                s = 0;
                while (it.hasNext()) {
                    Row row = it.next();
                    s++;
                    Cell idCell = row.getCell(0);
                    if (idCell.getCellType() == Cell.CELL_TYPE_STRING) {
                        String nameCell = idCell.getStringCellValue().trim();
                        if (nameCell.equals("CAR_ID")) {
                            row = it.next();
                            s++;
                            Cell carIdCell = row.getCell(0);
                            String carIdstr = StringAdapter.HSSFSellValue(carIdCell);
                            if (carIdstr.contains(".")) {
                                int point = carIdstr.indexOf(".");
                                carIdstr = carIdstr.substring(0, point);
                            }
                            Long carId = StringAdapter.toLong(carIdstr);
                            car = carDao.find(carId);
                            //throw new Exception(" carIdstr="+carIdstr+"; carId="+carId+"; ");
                        } else if (nameCell.equals("OPTION_ID")) {
                            while (it.hasNext()) {
                                row = it.next();
                                s++;
                                Cell optIdCell = row.getCell(0);
                                if (optIdCell != null) {
                                    if (optIdCell.getCellType() == Cell.CELL_TYPE_STRING) {
                                        String optIdstr = optIdCell.getStringCellValue().trim();
                                        if (optIdstr.equals("CAR_ID")) {
                                            /*it.remove();
                                             break;*/
                                            row = it.next();
                                            s++;
                                            Cell carIdCell = row.getCell(0);
                                            String carIdstr = StringAdapter.HSSFSellValue(carIdCell);
                                            if (carIdstr.contains(".")) {
                                                int point = carIdstr.indexOf(".");
                                                carIdstr = carIdstr.substring(0, point);
                                            }
                                            Long carId = StringAdapter.toLong(carIdstr);
                                            car = carDao.find(carId);
                                            it.next();
                                            s++;
                                        } else {
                                            String oIdstr = StringAdapter.HSSFSellValue(optIdCell);
                                            if (oIdstr.contains(".")) {
                                                int point = oIdstr.indexOf(".");
                                                oIdstr = oIdstr.substring(0, point);
                                            }
                                            Long optId = Long.valueOf(oIdstr);
                                            FreeOption fored = freeOptionDao.find(optId);
                                            if (fored != null) {
                                                FreeOption supfo = getOptFromRow(row);
                                                fored.setAudial(supfo.getAudial());
                                                fored.setDescription(supfo.getDescription());
                                                fored.setKinestetic(supfo.getKinestetic());
                                                fored.setParamValue(supfo.getParamValue());
                                                fored.setPercentValue(supfo.getPercentValue());
                                                fored.setPrice(supfo.getPrice());
                                                fored.setRadical(supfo.getRadical());
                                                fored.setTitle(supfo.getTitle());
                                                fored.setType(supfo.getType());
                                                fored.setUid(supfo.getUid());
                                                fored.setVisual(supfo.getVisual());
                                                //throw new Exception("4!");
                                                if (validate(fored)) {
                                                    //freeOptionDao.update(fored);
                                                    foForUpd.add(fored);
                                                }
                                            } else {
                                                FreeOption fo = getOptFromRow(row);
                                                if (car != null) {
                                                    fo.setCar(car);
                                                    if (validate(fo)) {
                                                        foForSave.add(fo);
                                                    }
                                                }
                                                //addError(":  ?    ,     .");
                                            }
                                        }
                                    } else if (optIdCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                                        String oIdstr = StringAdapter.HSSFSellValue(optIdCell);
                                        if (oIdstr.contains(".")) {
                                            int point = oIdstr.indexOf(".");
                                            oIdstr = oIdstr.substring(0, point);
                                        }
                                        Long optId = Long.valueOf(oIdstr);
                                        //Long optId = StringAdapter.toLong(StringAdapter.getString(optIdCell.getNumericCellValue()));
                                        FreeOption fored = freeOptionDao.find(optId);
                                        if (fored != null) {
                                            FreeOption supfo = getOptFromRow(row);
                                            fored.setAudial(supfo.getAudial());
                                            fored.setDescription(supfo.getDescription());
                                            fored.setKinestetic(supfo.getKinestetic());
                                            fored.setParamValue(supfo.getParamValue());
                                            fored.setPercentValue(supfo.getPercentValue());
                                            fored.setPrice(supfo.getPrice());
                                            fored.setRadical(supfo.getRadical());
                                            fored.setTitle(supfo.getTitle());
                                            fored.setType(supfo.getType());
                                            fored.setUid(supfo.getUid());
                                            fored.setVisual(supfo.getVisual());
                                            //throw new Exception("3!");
                                            if (validate(fored)) {
                                                //freeOptionDao.update(fored);
                                                foForUpd.add(fored);
                                            }
                                        } else {
                                            FreeOption fo = getOptFromRow(row);
                                            if (car != null) {
                                                fo.setCar(car);
                                                if (validate(fo)) {
                                                    foForSave.add(fo);
                                                }
                                            }
                                        }
                                    } else if (optIdCell.getCellType() == Cell.CELL_TYPE_BLANK) {
                                        if (car != null) {
                                            FreeOption fo = getOptFromRow(row);
                                            fo.setCar(car);
                                            //throw new Exception("2!");
                                            if (validate(fo)) {
                                                foForSave.add(fo);
                                            }
                                        }
                                    }
                                } else {
                                    if (car != null) {
                                        FreeOption fo = getOptFromRow(row);
                                        fo.setCar(car);
                                        //throw new Exception("1! rad="+fo.getRadical()+"; a="+fo.getAudial()+"; v="+fo.getVisual()+"; perc="+fo.getPercentValue()+"; val="+fo.getParamValue()+"; price="+fo.getPrice()+"; ");
                                        if (validate(fo)) {
                                            foForSave.add(fo);
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }
            workbook.close();
        } catch (Exception e) {
            addError(": ?:" + i + ", " + listName + ", ?:" + s + ", "
                    + StringAdapter.getStackTraceException(e));
        }
        fi.close();
        for (FreeOption fo : foForSave) {
            freeOptionDao.save(fo);
        }
        for (FreeOption fo : foForUpd) {
            freeOptionDao.update(fo);
        }
    } catch (Exception e) {
        addError("  xml");
        addError(e.getMessage());
    }
}

From source file:de.bund.bfr.knime.openkrise.db.imports.GeneralXLSImporter.java

License:Open Source License

public boolean doImport(final String filename, final JProgressBar progress, final boolean showResults) {
    Runnable runnable = new Runnable() {
        public void run() {
            try {
                if (progress != null) {
                    progress.setVisible(true);
                    progress.setStringPainted(true);
                    progress.setString("Importiere Excel Datei...");
                    progress.setMinimum(0);
                }/*from  w w w.  java  2 s. co  m*/

                InputStream is = null;
                System.out.println(filename);
                if (filename.startsWith("http://")) {
                    URL url = new URL(filename);
                    URLConnection uc = url.openConnection();
                    is = uc.getInputStream();
                } else if (filename.startsWith("/de/bund/bfr/knime/openkrise/db/res/")) {
                    is = this.getClass().getResourceAsStream(filename);
                } else {
                    is = new FileInputStream(filename);
                }

                try (HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is))) {
                    HSSFSheet sheet;
                    HSSFRow row;

                    int numSuccess = 0;
                    int numFailed = 0;
                    String unusedFields = "";
                    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                        sheet = wb.getSheetAt(i);
                        String tableName = sheet.getSheetName();
                        MyTable myT = DBKernel.myDBi.getTable(tableName);
                        if (myT != null) {
                            int numRows = sheet.getLastRowNum();
                            if (progress != null) {
                                progress.setMaximum(numRows);
                                progress.setValue(0);
                            }

                            row = sheet.getRow(0);
                            String sql1 = "";
                            String sql2 = "";
                            String sql3 = "";
                            Vector<String> codeSql1 = new Vector<>();
                            Vector<String> codeSql2 = new Vector<>();
                            LinkedHashMap<MyTable, Vector<Integer>> foreignTables = new LinkedHashMap<>();
                            int numCols = row.getLastCellNum();
                            String[] fieldNames = new String[numCols];
                            String[] fieldTypes = new String[numCols];//getTypes(fieldNames, myT);
                            String[] ffieldTypes = new String[numCols];
                            MyTable[] myForeignTables = new MyTable[numCols];
                            String[] kzS = new String[numCols];
                            String[] dbFieldnames = new String[numCols];
                            int lfdCol = 0;
                            Hashtable<String, String> dbFieldNames = new Hashtable<>();
                            for (int j = 0; j < numCols; j++) {
                                String fieldName = row.getCell(j).getStringCellValue();
                                fieldNames[j] = fieldName;
                                int ffe;
                                String dbFieldName = getDBFieldName(fieldName, myT, takecareofID);
                                if (dbFieldName != null) {
                                    String ft = getForeignTable(dbFieldName, myT);
                                    if (ft != null && ft.equals("DoubleKennzahlen")) {
                                        kzS[j] = getKZ(fieldName, dbFieldName);
                                        dbFieldnames[j] = dbFieldName;
                                    } else if (!dbFieldNames.containsKey(dbFieldName)) {
                                        dbFieldNames.put(dbFieldName, dbFieldName);
                                        sql1 += DBKernel.delimitL(dbFieldName) + ",";
                                        sql2 += "?,";
                                        sql3 += DBKernel.delimitL(dbFieldName) + "=?,";
                                        lfdCol++;
                                    }
                                    fieldTypes[j] = getType(dbFieldName, myT, takecareofID);
                                } else if ((ffe = foreignFieldExists(fieldName, myT)) >= 0) {
                                    if (!foreignTables.containsKey(myT.getForeignFields()[ffe]))
                                        foreignTables.put(myT.getForeignFields()[ffe], new Vector<Integer>());
                                    ffieldTypes[j] = getType(fieldName, myT.getForeignFields()[ffe], false);
                                    foreignTables.get(myT.getForeignFields()[ffe]).add(j);
                                    myForeignTables[j] = myT.getForeignFields()[ffe];
                                } else if (DBKernel.showHierarchic(tableName)
                                        && fieldName.toLowerCase().endsWith("-code")) {
                                    codeSql1.add(DBKernel.delimitL("CodeSystem") + ","
                                            + DBKernel.delimitL("Code") + "," + DBKernel.delimitL("Basis"));
                                    codeSql2.add(
                                            "'" + fieldName.substring(0, fieldName.length() - "-code".length())
                                                    + "',?,?");
                                } else if (!fieldName.equalsIgnoreCase("id")) {
                                    unusedFields += "," + fieldName;
                                }
                            }
                            if (sql1.length() > 0 && sql2.length() > 0) {
                                String sql = "INSERT INTO " + DBKernel.delimitL(tableName) + " ("
                                        + sql1.substring(0, sql1.length() - 1) + ") VALUES ("
                                        + sql2.substring(0, sql2.length() - 1) + ")";
                                PreparedStatement ps = DBKernel.getDBConnection().prepareStatement(sql,
                                        Statement.RETURN_GENERATED_KEYS);
                                int idCol = lfdCol + 1;
                                sql = "UPDATE " + DBKernel.delimitL(tableName) + " SET "
                                        + sql3.substring(0, sql3.length() - 1) + " WHERE "
                                        + DBKernel.delimitL("ID") + "=?";
                                PreparedStatement psUpdate = DBKernel.getDBConnection().prepareStatement(sql);
                                PreparedStatement[] psCodes = new PreparedStatement[codeSql1.size()];
                                boolean doCode[] = new boolean[codeSql1.size()];
                                int codesI;
                                for (codesI = 0; codesI < codeSql1.size(); codesI++) {
                                    sql = "INSERT INTO " + DBKernel.delimitL(DBKernel.getCodesName(tableName))
                                            + " (" + codeSql1.get(codesI) + ") VALUES (" + codeSql2.get(codesI)
                                            + ")";
                                    psCodes[codesI] = DBKernel.getDBConnection().prepareStatement(sql);
                                }
                                LinkedHashMap<MyTable, PreparedStatement> psForeign = new LinkedHashMap<>();
                                LinkedHashMap<MyTable, PreparedStatement> psForeignUpdate = new LinkedHashMap<>();
                                for (Map.Entry<MyTable, Vector<Integer>> entry : foreignTables.entrySet()) {
                                    Vector<Integer> vs = entry.getValue();
                                    String ssql1 = "", ssql2 = "", ssql3 = "";
                                    for (int ii = 0; ii < vs.size(); ii++) {
                                        ssql1 += "," + DBKernel.delimitL(fieldNames[vs.get(ii)]);
                                        ssql2 += ",?";
                                        ssql3 += "," + DBKernel.delimitL(fieldNames[vs.get(ii)]) + "=?";
                                    }
                                    if (ssql1.length() > 0 && ssql2.length() > 0 && ssql3.length() > 0) {
                                        sql = "INSERT INTO " + DBKernel.delimitL(entry.getKey().getTablename())
                                                + " (" + ssql1.substring(1) + ") VALUES (" + ssql2.substring(1)
                                                + ")";
                                        psForeign.put(entry.getKey(), DBKernel.getDBConnection()
                                                .prepareStatement(sql, Statement.RETURN_GENERATED_KEYS));
                                        sql = "UPDATE " + DBKernel.delimitL(entry.getKey().getTablename())
                                                + " SET " + ssql3.substring(1) + " WHERE "
                                                + DBKernel.delimitL("ID") + "=?";
                                        psForeignUpdate.put(entry.getKey(),
                                                DBKernel.getDBConnection().prepareStatement(sql));
                                    }
                                }

                                LinkedHashMap<Object, String> hashBL = null;
                                Iterator<Row> rows = sheet.rowIterator();
                                int lfd = 0;
                                while (rows.hasNext()) {
                                    row = (HSSFRow) rows.next();
                                    boolean setID = false;
                                    Integer lastID = null;
                                    ps.clearParameters();
                                    psUpdate.clearParameters();
                                    for (codesI = 0; codesI < codeSql1.size(); codesI++) {
                                        psCodes[codesI].clearParameters();
                                        doCode[codesI] = false;
                                    }
                                    LinkedHashMap<MyTable, Integer> lfdColsForeign = new LinkedHashMap<>();
                                    for (Map.Entry<MyTable, PreparedStatement> entry : psForeignUpdate
                                            .entrySet()) {
                                        MyTable myT1 = entry.getKey();
                                        psForeign.get(myT1).clearParameters();
                                        psForeignUpdate.get(myT1).clearParameters();
                                        lfdColsForeign.put(myT1, 0);
                                    }

                                    if (row.getRowNum() > 0) {
                                        lfdCol = 0;
                                        codesI = 0;
                                        Object[] kzVal = new Object[numCols];
                                        for (int j = 0; j < numCols; j++) {
                                            if (fieldTypes[j] != null) {
                                                /*
                                                 * if (fieldNames[j].equals(
                                                 * "Bundesland")) { hashBL =
                                                 * DBKernel
                                                 * .myDBi.getHashMap("County");
                                                 * } else { hashBL = null; }
                                                 */
                                                lfdCol++;
                                                if (fieldTypes[j].startsWith("VARCHAR(")
                                                        || fieldTypes[j].startsWith("CHAR(")
                                                        || fieldTypes[j].startsWith("BLOB(")
                                                                && !tableName.equals("DateiSpeicher"))
                                                    manageString(ps, psUpdate, lfdCol, row.getCell(j), hashBL);
                                                else if (fieldTypes[j].equals("BOOLEAN"))
                                                    manageBoolean(ps, psUpdate, lfdCol, row.getCell(j));
                                                else if (fieldTypes[j].equals("INTEGER"))
                                                    manageInteger(ps, psUpdate, lfdCol, row.getCell(j));
                                                else if (fieldTypes[j].equals("BIGINT"))
                                                    manageBigInteger(ps, psUpdate, lfdCol, row.getCell(j));
                                                else if (fieldTypes[j].equals("DATE"))
                                                    manageDate(ps, psUpdate, lfdCol, row.getCell(j));
                                                else if (fieldTypes[j].equals("DOUBLE")) {
                                                    if (kzS[j] != null) {
                                                        lfdCol--;
                                                        //System.err.println(dbFieldnames[j] + "\t" + kzS[j]);
                                                        if (DBKernel.kzIsString(kzS[j]))
                                                            kzVal[j] = manageString(null, null, lfdCol,
                                                                    row.getCell(j));
                                                        else if (DBKernel.kzIsBoolean(kzS[j]))
                                                            kzVal[j] = manageBoolean(null, null, lfdCol,
                                                                    row.getCell(j));
                                                        else
                                                            kzVal[j] = manageDouble(null, null, lfdCol,
                                                                    row.getCell(j));
                                                    } else {
                                                        manageDouble(ps, psUpdate, lfdCol, row.getCell(j));
                                                    }
                                                } else
                                                    System.err.println(
                                                            "Wasn hier los? Undefinierter Feldtyp???? ->\t"
                                                                    + fieldNames[j]);
                                            } else if (myForeignTables[j] != null && ffieldTypes[j] != null) {
                                                lfdColsForeign.put(myForeignTables[j],
                                                        lfdColsForeign.get(myForeignTables[j]) + 1);
                                                if (ffieldTypes[j].startsWith("VARCHAR(")
                                                        || ffieldTypes[j].startsWith("CHAR(")
                                                        || ffieldTypes[j].startsWith("BLOB(")
                                                                && !tableName.equals("DateiSpeicher"))
                                                    manageString(psForeign.get(myForeignTables[j]),
                                                            psForeignUpdate.get(myForeignTables[j]),
                                                            lfdColsForeign.get(myForeignTables[j]),
                                                            row.getCell(j), hashBL);
                                                else if (ffieldTypes[j].equals("BOOLEAN"))
                                                    manageBoolean(psForeign.get(myForeignTables[j]),
                                                            psForeignUpdate.get(myForeignTables[j]),
                                                            lfdColsForeign.get(myForeignTables[j]),
                                                            row.getCell(j));
                                                else if (ffieldTypes[j].equals("INTEGER"))
                                                    manageInteger(psForeign.get(myForeignTables[j]),
                                                            psForeignUpdate.get(myForeignTables[j]),
                                                            lfdColsForeign.get(myForeignTables[j]),
                                                            row.getCell(j));
                                                else if (ffieldTypes[j].equals("BIGINT"))
                                                    manageBigInteger(psForeign.get(myForeignTables[j]),
                                                            psForeignUpdate.get(myForeignTables[j]),
                                                            lfdColsForeign.get(myForeignTables[j]),
                                                            row.getCell(j));
                                                else if (fieldTypes[j].equals("DATE"))
                                                    manageDate(psForeign.get(myForeignTables[j]),
                                                            psForeignUpdate.get(myForeignTables[j]),
                                                            lfdColsForeign.get(myForeignTables[j]),
                                                            row.getCell(j));
                                                else if (ffieldTypes[j].equals("DOUBLE")) {
                                                    manageDouble(psForeign.get(myForeignTables[j]),
                                                            psForeignUpdate.get(myForeignTables[j]),
                                                            lfdColsForeign.get(myForeignTables[j]),
                                                            row.getCell(j));
                                                } else
                                                    System.err.println(fieldNames[j] + " Feldtype????");
                                            } else if (fieldNames[j].equals("ID")) {
                                                lastID = manageInteger(null, null, 0, row.getCell(j));
                                                if (lastID != null) {
                                                    if (DBKernel.hasID(tableName, lastID.intValue())) {
                                                        psUpdate.setInt(idCol, lastID.intValue());
                                                        setID = true;
                                                    }
                                                }
                                            } else if (DBKernel.showHierarchic(tableName)
                                                    && fieldNames[j].toLowerCase().endsWith("-code")) {
                                                String code = manageString(psCodes[codesI], null, 1,
                                                        row.getCell(j));
                                                if (code != null && code.length() > 0)
                                                    doCode[codesI] = true;
                                                codesI++;
                                            } else {
                                                //System.out.println(fieldNames[j]);                                 
                                            }
                                        }
                                        try {
                                            if (setID) {
                                                psUpdate.execute();
                                            } else {
                                                if (ps.executeUpdate() > 0) {// execute()
                                                    lastID = DBKernel.getLastInsertedID(ps);
                                                } else {
                                                    System.err.println("W");
                                                }
                                            }
                                            numSuccess++;
                                            if (lastID != null) {
                                                for (int j = 0; j < numCols; j++) {
                                                    if (dbFieldnames[j] != null && kzVal[j] != null) {
                                                        DBKernel.insertDBL(myT.getTablename(), dbFieldnames[j],
                                                                lastID, null, kzS[j], kzVal[j]);
                                                    }
                                                }
                                                for (codesI = 0; codesI < codeSql1.size(); codesI++) {
                                                    if (doCode[codesI]) {
                                                        psCodes[codesI].setInt(2, lastID);
                                                        try {
                                                            psCodes[codesI].execute();
                                                            numSuccess++;
                                                        } catch (SQLException e1) {
                                                            numFailed++;
                                                            System.err.println(psCodes[codesI]);
                                                        }
                                                    }
                                                }

                                                for (Map.Entry<MyTable, PreparedStatement> entry : psForeign
                                                        .entrySet()) {
                                                    MyTable myT1 = entry.getKey();
                                                    MyTable[] foreignTs = myT.getForeignFields();
                                                    for (int ii = 0; ii < foreignTs.length; ii++) {
                                                        if (foreignTs[ii] != null
                                                                && foreignTs[ii].equals(myT1)) {
                                                            if (psForeign.get(myT1).executeUpdate() > 0) { // INSERT
                                                                int lID = DBKernel
                                                                        .getLastInsertedID(psForeign.get(myT1));
                                                                // Das erstbeste Feld, das auf den Fremdtable verweist, wird mit dem Neueintrag verlinkt
                                                                DBKernel.sendRequest("UPDATE "
                                                                        + DBKernel.delimitL(tableName) + " SET "
                                                                        + DBKernel.delimitL(
                                                                                myT.getFieldNames()[ii])
                                                                        + "=" + lID + " WHERE "
                                                                        + DBKernel.delimitL("ID") + "="
                                                                        + lastID, false);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                                /*
                                                 * for (int j=0;j<numCols;j++) {
                                                 * if (myForeignTables[j] !=
                                                 * null && ffieldTypes[j] !=
                                                 * null) { MyTable[] foreignTs =
                                                 * myT.getForeignFields(); for
                                                 * (int
                                                 * ii=0;ii<foreignTs.length;
                                                 * ii++) { if (foreignTs[ii] !=
                                                 * null && foreignTs[ii].equals(
                                                 * myForeignTables[j])) { if
                                                 * (psForeign
                                                 * .get(myForeignTables
                                                 * [j]).executeUpdate() > 0) {
                                                 * // INSERT int lID =
                                                 * DBKernel.getLastInsertedID
                                                 * (psForeign
                                                 * .get(myForeignTables[j]));
                                                 * DBKernel
                                                 * .sendRequest("UPDATE " +
                                                 * DBKernel.delimitL(tableName)
                                                 * + " SET " +
                                                 * DBKernel.delimitL(
                                                 * myT.getFieldNames()[ii]) +
                                                 * "=" + lID + " WHERE " +
                                                 * DBKernel.delimitL("ID") + "="
                                                 * + lastID, false); } break; }
                                                 * } } }
                                                 */
                                            }
                                        } catch (Exception e1) {
                                            numFailed++;
                                            MyLogger.handleMessage(ps.toString());
                                            MyLogger.handleException(e1);
                                        }
                                    }
                                    if (progress != null) {
                                        lfd++;
                                        progress.setValue(lfd);
                                    }
                                }
                            }

                            myT.doMNs();
                            if (progress != null) {
                                // Refreshen:
                                MyDBTable myDB = DBKernel.mainFrame.getMyList().getMyDBTable();
                                if (myDB.getActualTable() != null) {
                                    String actTablename = myDB.getActualTable().getTablename();
                                    if (actTablename.equals(tableName)
                                            || actTablename.equals(DBKernel.getCodesName(tableName))) {
                                        myDB.setTable(myDB.getActualTable());
                                    }
                                }
                                MyDBTree myTR = DBKernel.mainFrame.getMyList().getMyDBTree();
                                if (myTR.getActualTable() != null) {
                                    String actTablename = myTR.getActualTable().getTablename();
                                    if (actTablename.equals(tableName)
                                            || actTablename.equals(DBKernel.getCodesName(tableName))) {
                                        myTR.setTable(myTR.getActualTable());
                                    }
                                }
                            }
                        } else {
                            System.err.println(tableName + " nicht in DB???");
                        }
                    }
                    if (progress != null) {
                        progress.setVisible(false);
                    }
                    String log = numSuccess + " erfolgreiche Importe.\n";
                    log += numFailed + " fehlgeschlagene Importe.\n";
                    if (unusedFields.length() > 0)
                        log += "Unbekannte Felder: " + unusedFields.substring(1) + "\n";
                    if (showResults) {
                        InfoBox ib = new InfoBox(log, true, new Dimension(400, 300), null);
                        ib.setVisible(true);
                    } else {
                        System.out.println("GeneralXLSImporter (" + filename + "):\n" + log);
                    }
                } catch (Exception e) {
                    MyLogger.handleException(e);
                }
            } catch (Exception e) {
                MyLogger.handleException(e);
            }
        }
    };

    Thread thread = new Thread(runnable);
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        MyLogger.handleException(e);
    }
    return true;
}

From source file:mat.server.service.impl.XLSGenerator.java

/** Creates the header row.
 * /*  w  w w  .j  ava2 s  .  com*/
 * @param wkst - HSSFSheet.
 * @param values - String Array.
 * @param names - String Array.
 * @param rownum - Integer.
 * @param style -HSSFCellStyle.
 * @return HSSFRow. * */
public final HSSFRow createHeaderRow(final HSSFSheet wkst, final String[] values, final String[] names,
        final int rownum, final HSSFCellStyle style) {
    HSSFRow headerRow = createXLSRow(wkst, values, rownum, style);
    HSSFWorkbook wkbk = wkst.getWorkbook();

    generateName(wkbk, names[measuredeveloper], "'" + wkst.getSheetName() + "'!$A$1");
    generateName(wkbk, names[oid], "'" + wkst.getSheetName() + "'!$B$1");
    generateName(wkbk, names[revisionDate], "'" + wkst.getSheetName() + "'!$C$1");
    generateName(wkbk, names[standardconcept], "'" + wkst.getSheetName() + "'!$D$1");
    /*
     * generateName(wkbk, names[standardcategory], "'" + wkst.getSheetName()
     * + "'!$E$1");
     */
    generateName(wkbk, names[standardtaxonomy], "'" + wkst.getSheetName() + "'!$E$1");
    generateName(wkbk, names[standardtaxonomyversion], "'" + wkst.getSheetName() + "'!$F$1");
    generateName(wkbk, names[code], "'" + wkst.getSheetName() + "'!$G$1");
    generateName(wkbk, names[codedescription], "'" + wkst.getSheetName() + "'!$H$1");

    return headerRow;
}

From source file:org.apdplat.platform.generator.ModelGenerator.java

License:Open Source License

/**
 * ???EXCEL//from   ww  w .  j a  v a2s .c o m
 * ?EXCELJAVA
 * @param inputStream ??EXCEL
 * @return JAVA
 */
private static List<ModelInfo> readModelInfos(InputStream inputStream) {
    List<ModelInfo> models = new ArrayList<>();
    try {
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            HSSFSheet sheet = workbook.getSheetAt(i);
            try {
                HSSFRow row = sheet.getRow(2);
                if (row == null) {
                    LOG.info("???" + sheet.getSheetName());
                    continue;
                }
                HSSFCell cell = row.getCell(1);
                //??
                String modelPackage = cell.getStringCellValue();
                row = sheet.getRow(3);
                cell = row.getCell(1);
                //??
                String modelEnglish = cell.getStringCellValue();
                row = sheet.getRow(4);
                cell = row.getCell(1);
                //??
                String modelChinese = cell.getStringCellValue();

                ModelInfo modelInfo = new ModelInfo();
                modelInfo.setModelPackage(modelPackage);
                modelInfo.setModelEnglish(modelEnglish);
                modelInfo.setModelChinese(modelChinese);

                int rows = sheet.getPhysicalNumberOfRows();
                //8??
                for (int rowNumber = 7; rowNumber < rows; rowNumber++) {
                    HSSFRow oneRow = sheet.getRow(rowNumber);

                    if (oneRow == null) {
                        continue;
                    }
                    Attr attr = new Attr();
                    //??
                    HSSFCell oneCell = oneRow.getCell(0);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setDes(cellValue);
                        } else {
                            continue;
                        }
                    }
                    //??
                    oneCell = oneRow.getCell(1);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setName(cellValue);
                        } else {
                            continue;
                        }
                    }
                    //
                    oneCell = oneRow.getCell(2);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setType(AttrType.validType(cellValue));
                        } else {
                            attr.setType(AttrType.validType("String"));
                        }
                    }
                    //?string
                    oneCell = oneRow.getCell(3);
                    if (oneCell != null) {
                        if (oneCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                try {
                                    int length = Integer.parseInt(cellValue);
                                    attr.setLength(length);
                                } catch (Exception e) {
                                    LOG.error("?" + cellValue);
                                }
                            }
                        }
                        if (oneCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            double length = oneCell.getNumericCellValue();
                            attr.setLength((int) length);
                        }
                    }
                    //??
                    oneCell = oneRow.getCell(4);
                    if (oneCell != null) {
                        boolean cellValue = oneCell.getBooleanCellValue();
                        attr.setSearchable(cellValue);
                    }
                    //??
                    oneCell = oneRow.getCell(5);
                    if (oneCell != null) {
                        boolean cellValue = oneCell.getBooleanCellValue();
                        attr.setRenderIgnore(cellValue);
                    }
                    //??
                    oneCell = oneRow.getCell(6);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setDic(DicType.validType(cellValue));
                        }
                    }
                    //???
                    oneCell = oneRow.getCell(7);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setDicName(cellValue);
                        }
                    }
                    //
                    oneCell = oneRow.getCell(8);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setMap(MapType.validType(cellValue));
                        }
                    }
                    //
                    oneCell = oneRow.getCell(9);
                    if (oneCell != null) {
                        String cellValue = oneCell.getStringCellValue();
                        if (cellValue != null && !"".equals(cellValue.trim())
                                && !"null".equals(cellValue.trim().toLowerCase())) {
                            attr.setAttrRef(cellValue);
                        }
                    }
                    if ("Date".equals(attr.getType()) || "Time".equals(attr.getType())) {
                        modelInfo.setHasDateTime(true);
                    }
                    if ("DicItem".equals(attr.getType())) {
                        modelInfo.setHasDicItem(true);
                        //DicItemdicSimpleDic
                        if (!"SimpleDic".equals(attr.getDic()) && !"TreeDic".equals(attr.getDic())) {
                            attr.setDic("SimpleDic");
                        }
                        if (attr.getDicName() == null || "".equals(attr.getDicName())) {
                            attr.setDicName(attr.getName());
                        }
                    }
                    modelInfo.addAttr(attr);
                }
                models.add(modelInfo);
            } catch (Exception e) {
                LOG.error("?:" + sheet.getSheetName() + " ", e);
            }
        }
    } catch (IOException e) {
        LOG.error("?MODEL", e);
    }
    return models;
}

From source file:org.drools.informer.load.spreadsheet.SpreadsheetData.java

License:Apache License

/**
 * Will split the sheet from the workbook up into {@link SpreadsheetRow} and {@link SpreadsheetItem}
 * //  w w w .  j  a v a 2s . com
 * @param sheet
 */
public SpreadsheetData(HSSFSheet sheet) {
    super();
    sheetName = sheet.getSheetName();
    for (Row row : sheet) {
        int rowNumber = row.getRowNum();
        SpreadsheetRow rowItems = new SpreadsheetRow(rowNumber);
        rows.add(rowItems);
        for (Cell cell : row) {
            if ((cell == null) || (cell.getCellType() == Cell.CELL_TYPE_BLANK)) {
                // null check is just in case - should never be!
                continue;
            }
            if ((keyColumn > 0) && (cell.getColumnIndex() < keyColumn)) {
                // comments column
                continue;
            }
            SpreadsheetItem item = new SpreadsheetItem(sheet.getSheetName(), cell);
            if (firstItemOnSheet == null) {
                // The first cell item must be sheet identifier/heading - previous columns will be treated as comments
                // and thus ignored
                firstItemOnSheet = item;
                keyColumn = cell.getColumnIndex();
            }
            String id = item.getCellIdentifier();

            //System.out.println("Sheet:" + sheet.getSheetName() + ", id=" + id + ", toString=" + item.toString());
            data.put(id, item);
            rowItems.addRowItem(item);
            cellList.add(item.getCellIdentifier());
        }
    }
}

From source file:org.drools.informer.load.spreadsheet.WorkbookData.java

License:Apache License

/**
 * Open and load the workbook sheets. Note: any sheet with an "!" in the name will be ignored.
 * /*from   www.  j a  va  2  s  . c  o  m*/
 * @param filename
 * @return
 */
public boolean loadWorkbook(String filename) {
    try {
        logger.debug("\n\n\nPROCESSING FILE: " + filename);
        InputStream inp = new FileInputStream(filename);
        HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            HSSFSheet sheet = wb.getSheetAt(i);
            SpreadsheetData sheetData = new SpreadsheetData(sheet);
            String sheetName = sheet.getSheetName();
            if (sheetName.indexOf("!") >= 0) {
                logger.debug("Ignoring sheet named: " + sheetName);
                continue;
            }
            data.put(sheetName, sheetData);
            sheetList.add(sheetName);
        }
        inp.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:org.hsh.bfr.db.imports.GeneralXLSImporter.java

License:Open Source License

public String doImport(final String filename, final JProgressBar progress, final boolean showResults) {
    Runnable runnable = new Runnable() {
        public void run() {
            try {
                if (progress != null) {
                    progress.setVisible(true);
                    progress.setStringPainted(true);
                    progress.setString("Importiere Excel Datei...");
                    progress.setMinimum(0);
                }/*from  w ww  .  ja  v a 2s .  co  m*/

                InputStream is = null;
                System.out.println(filename);
                if (filename.startsWith("http://")) {
                    URL url = new URL(filename);
                    URLConnection uc = url.openConnection();
                    is = uc.getInputStream();
                } else if (filename.startsWith("/org/hsh/bfr/db/res/")) {
                    is = this.getClass().getResourceAsStream(filename);
                } else {
                    is = new FileInputStream(filename);
                }

                POIFSFileSystem fs = new POIFSFileSystem(is);
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                HSSFSheet sheet;
                HSSFRow row;

                int numSuccess = 0;
                int numFailed = 0;
                String unusedFields = "";
                for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                    sheet = wb.getSheetAt(i);
                    String tableName = sheet.getSheetName();
                    MyTable myT = DBKernel.myDBi.getTable(tableName);
                    if (myT != null) {
                        int numRows = sheet.getLastRowNum();
                        if (progress != null) {
                            progress.setMaximum(numRows);
                            progress.setValue(0);
                        }

                        row = sheet.getRow(0);
                        String sql1 = "";
                        String sql2 = "";
                        String sql3 = "";
                        Vector<String> codeSql1 = new Vector<>();
                        Vector<String> codeSql2 = new Vector<>();
                        LinkedHashMap<MyTable, Vector<Integer>> foreignTables = new LinkedHashMap<>();
                        int numCols = row.getLastCellNum();
                        String[] fieldNames = new String[numCols];
                        String[] fieldTypes = new String[numCols];//getTypes(fieldNames, myT);
                        String[] ffieldTypes = new String[numCols];
                        MyTable[] myForeignTables = new MyTable[numCols];
                        String[] kzS = new String[numCols];
                        String[] dbFieldnames = new String[numCols];
                        int lfdCol = 0;
                        Hashtable<String, String> dbFieldNames = new Hashtable<>();
                        for (int j = 0; j < numCols; j++) {
                            String fieldName = row.getCell(j).getStringCellValue();
                            fieldNames[j] = fieldName;
                            int ffe;
                            String dbFieldName = getDBFieldName(fieldName, myT, takecareofID);
                            if (dbFieldName != null) {
                                String ft = getForeignTable(dbFieldName, myT);
                                if (ft != null && ft.equals("DoubleKennzahlen")) {
                                    kzS[j] = getKZ(fieldName, dbFieldName);
                                    dbFieldnames[j] = dbFieldName;
                                } else if (!dbFieldNames.containsKey(dbFieldName)) {
                                    dbFieldNames.put(dbFieldName, dbFieldName);
                                    sql1 += DBKernel.delimitL(dbFieldName) + ",";
                                    sql2 += "?,";
                                    sql3 += DBKernel.delimitL(dbFieldName) + "=?,";
                                    lfdCol++;
                                }
                                fieldTypes[j] = getType(dbFieldName, myT, takecareofID);
                            } else if ((ffe = foreignFieldExists(fieldName, myT)) >= 0) {
                                if (!foreignTables.containsKey(myT.getForeignFields()[ffe]))
                                    foreignTables.put(myT.getForeignFields()[ffe], new Vector<Integer>());
                                ffieldTypes[j] = getType(fieldName, myT.getForeignFields()[ffe], false);
                                foreignTables.get(myT.getForeignFields()[ffe]).add(j);
                                myForeignTables[j] = myT.getForeignFields()[ffe];
                            } else if (DBKernel.showHierarchic(tableName)
                                    && fieldName.toLowerCase().endsWith("-code")) {
                                codeSql1.add(DBKernel.delimitL("CodeSystem") + "," + DBKernel.delimitL("Code")
                                        + "," + DBKernel.delimitL("Basis"));
                                codeSql2.add("'" + fieldName.substring(0, fieldName.length() - "-code".length())
                                        + "',?,?");
                            } else if (!fieldName.equalsIgnoreCase("id")) {
                                unusedFields += "," + fieldName;
                            }
                        }
                        if (sql1.length() > 0 && sql2.length() > 0) {
                            String sql = "INSERT INTO " + DBKernel.delimitL(tableName) + " ("
                                    + sql1.substring(0, sql1.length() - 1) + ") VALUES ("
                                    + sql2.substring(0, sql2.length() - 1) + ")";
                            PreparedStatement ps = DBKernel.getDBConnection().prepareStatement(sql,
                                    Statement.RETURN_GENERATED_KEYS);
                            int idCol = lfdCol + 1;
                            sql = "UPDATE " + DBKernel.delimitL(tableName) + " SET "
                                    + sql3.substring(0, sql3.length() - 1) + " WHERE " + DBKernel.delimitL("ID")
                                    + "=?";
                            PreparedStatement psUpdate = DBKernel.getDBConnection().prepareStatement(sql);
                            PreparedStatement[] psCodes = new PreparedStatement[codeSql1.size()];
                            boolean doCode[] = new boolean[codeSql1.size()];
                            int codesI;
                            for (codesI = 0; codesI < codeSql1.size(); codesI++) {
                                sql = "INSERT INTO " + DBKernel.delimitL(DBKernel.getCodesName(tableName))
                                        + " (" + codeSql1.get(codesI) + ") VALUES (" + codeSql2.get(codesI)
                                        + ")";
                                psCodes[codesI] = DBKernel.getDBConnection().prepareStatement(sql);
                            }
                            LinkedHashMap<MyTable, PreparedStatement> psForeign = new LinkedHashMap<>();
                            LinkedHashMap<MyTable, PreparedStatement> psForeignUpdate = new LinkedHashMap<>();
                            for (Map.Entry<MyTable, Vector<Integer>> entry : foreignTables.entrySet()) {
                                Vector<Integer> vs = entry.getValue();
                                String ssql1 = "", ssql2 = "", ssql3 = "";
                                for (int ii = 0; ii < vs.size(); ii++) {
                                    ssql1 += "," + DBKernel.delimitL(fieldNames[vs.get(ii)]);
                                    ssql2 += ",?";
                                    ssql3 += "," + DBKernel.delimitL(fieldNames[vs.get(ii)]) + "=?";
                                }
                                if (ssql1.length() > 0 && ssql2.length() > 0 && ssql3.length() > 0) {
                                    sql = "INSERT INTO " + DBKernel.delimitL(entry.getKey().getTablename())
                                            + " (" + ssql1.substring(1) + ") VALUES (" + ssql2.substring(1)
                                            + ")";
                                    psForeign.put(entry.getKey(), DBKernel.getDBConnection()
                                            .prepareStatement(sql, Statement.RETURN_GENERATED_KEYS));
                                    sql = "UPDATE " + DBKernel.delimitL(entry.getKey().getTablename()) + " SET "
                                            + ssql3.substring(1) + " WHERE " + DBKernel.delimitL("ID") + "=?";
                                    psForeignUpdate.put(entry.getKey(),
                                            DBKernel.getDBConnection().prepareStatement(sql));
                                }
                            }

                            LinkedHashMap<Object, String> hashBL = null;
                            Iterator<Row> rows = sheet.rowIterator();
                            int lfd = 0;
                            while (rows.hasNext()) {
                                row = (HSSFRow) rows.next();
                                boolean setID = false;
                                Integer lastID = null;
                                ps.clearParameters();
                                psUpdate.clearParameters();
                                for (codesI = 0; codesI < codeSql1.size(); codesI++) {
                                    psCodes[codesI].clearParameters();
                                    doCode[codesI] = false;
                                }
                                LinkedHashMap<MyTable, Integer> lfdColsForeign = new LinkedHashMap<>();
                                for (Map.Entry<MyTable, PreparedStatement> entry : psForeignUpdate.entrySet()) {
                                    MyTable myT1 = entry.getKey();
                                    psForeign.get(myT1).clearParameters();
                                    psForeignUpdate.get(myT1).clearParameters();
                                    lfdColsForeign.put(myT1, 0);
                                }

                                if (row.getRowNum() > 0) {
                                    lfdCol = 0;
                                    codesI = 0;
                                    Object[] kzVal = new Object[numCols];
                                    for (int j = 0; j < numCols; j++) {
                                        if (fieldTypes[j] != null) {
                                            /*
                                             * if (fieldNames[j].equals(
                                             * "Bundesland")) { hashBL =
                                             * DBKernel
                                             * .myDBi.getHashMap("County");
                                             * } else { hashBL = null; }
                                             */
                                            lfdCol++;
                                            if (fieldTypes[j].startsWith("VARCHAR(")
                                                    || fieldTypes[j].startsWith("CHAR(")
                                                    || fieldTypes[j].startsWith("BLOB(")
                                                            && !tableName.equals("DateiSpeicher"))
                                                manageString(ps, psUpdate, lfdCol, row.getCell(j), hashBL);
                                            else if (fieldTypes[j].equals("BOOLEAN"))
                                                manageBoolean(ps, psUpdate, lfdCol, row.getCell(j));
                                            else if (fieldTypes[j].equals("INTEGER"))
                                                manageInteger(ps, psUpdate, lfdCol, row.getCell(j));
                                            else if (fieldTypes[j].equals("BIGINT"))
                                                manageBigInteger(ps, psUpdate, lfdCol, row.getCell(j));
                                            else if (fieldTypes[j].equals("DATE"))
                                                manageDate(ps, psUpdate, lfdCol, row.getCell(j));
                                            else if (fieldTypes[j].equals("DOUBLE")) {
                                                if (kzS[j] != null) {
                                                    lfdCol--;
                                                    //System.err.println(dbFieldnames[j] + "\t" + kzS[j]);
                                                    if (DBKernel.kzIsString(kzS[j]))
                                                        kzVal[j] = manageString(null, null, lfdCol,
                                                                row.getCell(j));
                                                    else if (DBKernel.kzIsBoolean(kzS[j]))
                                                        kzVal[j] = manageBoolean(null, null, lfdCol,
                                                                row.getCell(j));
                                                    else
                                                        kzVal[j] = manageDouble(null, null, lfdCol,
                                                                row.getCell(j));
                                                } else {
                                                    manageDouble(ps, psUpdate, lfdCol, row.getCell(j));
                                                }
                                            } else
                                                System.err
                                                        .println("Wasn hier los? Undefinierter Feldtyp???? ->\t"
                                                                + fieldNames[j]);
                                        } else if (myForeignTables[j] != null && ffieldTypes[j] != null) {
                                            lfdColsForeign.put(myForeignTables[j],
                                                    lfdColsForeign.get(myForeignTables[j]) + 1);
                                            if (ffieldTypes[j].startsWith("VARCHAR(")
                                                    || ffieldTypes[j].startsWith("CHAR(")
                                                    || ffieldTypes[j].startsWith("BLOB(")
                                                            && !tableName.equals("DateiSpeicher"))
                                                manageString(psForeign.get(myForeignTables[j]),
                                                        psForeignUpdate.get(myForeignTables[j]),
                                                        lfdColsForeign.get(myForeignTables[j]), row.getCell(j),
                                                        hashBL);
                                            else if (ffieldTypes[j].equals("BOOLEAN"))
                                                manageBoolean(psForeign.get(myForeignTables[j]),
                                                        psForeignUpdate.get(myForeignTables[j]),
                                                        lfdColsForeign.get(myForeignTables[j]), row.getCell(j));
                                            else if (ffieldTypes[j].equals("INTEGER"))
                                                manageInteger(psForeign.get(myForeignTables[j]),
                                                        psForeignUpdate.get(myForeignTables[j]),
                                                        lfdColsForeign.get(myForeignTables[j]), row.getCell(j));
                                            else if (ffieldTypes[j].equals("BIGINT"))
                                                manageBigInteger(psForeign.get(myForeignTables[j]),
                                                        psForeignUpdate.get(myForeignTables[j]),
                                                        lfdColsForeign.get(myForeignTables[j]), row.getCell(j));
                                            else if (fieldTypes[j].equals("DATE"))
                                                manageDate(psForeign.get(myForeignTables[j]),
                                                        psForeignUpdate.get(myForeignTables[j]),
                                                        lfdColsForeign.get(myForeignTables[j]), row.getCell(j));
                                            else if (ffieldTypes[j].equals("DOUBLE")) {
                                                manageDouble(psForeign.get(myForeignTables[j]),
                                                        psForeignUpdate.get(myForeignTables[j]),
                                                        lfdColsForeign.get(myForeignTables[j]), row.getCell(j));
                                            } else
                                                System.err.println(fieldNames[j] + " Feldtype????");
                                        } else if (fieldNames[j].equals("ID")) {
                                            lastID = manageInteger(null, null, 0, row.getCell(j));
                                            if (lastID != null) {
                                                if (DBKernel.hasID(tableName, lastID.intValue())) {
                                                    psUpdate.setInt(idCol, lastID.intValue());
                                                    setID = true;
                                                }
                                            }
                                        } else if (DBKernel.showHierarchic(tableName)
                                                && fieldNames[j].toLowerCase().endsWith("-code")) {
                                            String code = manageString(psCodes[codesI], null, 1,
                                                    row.getCell(j));
                                            if (code != null && code.length() > 0)
                                                doCode[codesI] = true;
                                            codesI++;
                                        } else {
                                            //System.out.println(fieldNames[j]);                                 
                                        }
                                    }
                                    try {
                                        if (setID) {
                                            psUpdate.execute();
                                        } else {
                                            if (ps.executeUpdate() > 0) {// execute()
                                                lastID = DBKernel.getLastInsertedID(ps);
                                            } else {
                                                System.err.println("W");
                                            }
                                        }
                                        numSuccess++;
                                        if (lastID != null) {
                                            for (int j = 0; j < numCols; j++) {
                                                if (dbFieldnames[j] != null && kzVal[j] != null) {
                                                    DBKernel.insertDBL(myT.getTablename(), dbFieldnames[j],
                                                            lastID, null, kzS[j], kzVal[j]);
                                                }
                                            }
                                            for (codesI = 0; codesI < codeSql1.size(); codesI++) {
                                                if (doCode[codesI]) {
                                                    psCodes[codesI].setInt(2, lastID);
                                                    try {
                                                        psCodes[codesI].execute();
                                                        numSuccess++;
                                                    } catch (SQLException e1) {
                                                        numFailed++;
                                                        System.err.println(psCodes[codesI]);
                                                    }
                                                }
                                            }

                                            for (Map.Entry<MyTable, PreparedStatement> entry : psForeign
                                                    .entrySet()) {
                                                MyTable myT1 = entry.getKey();
                                                MyTable[] foreignTs = myT.getForeignFields();
                                                for (int ii = 0; ii < foreignTs.length; ii++) {
                                                    if (foreignTs[ii] != null && foreignTs[ii].equals(myT1)) {
                                                        if (psForeign.get(myT1).executeUpdate() > 0) { // INSERT
                                                            int lID = DBKernel
                                                                    .getLastInsertedID(psForeign.get(myT1));
                                                            // Das erstbeste Feld, das auf den Fremdtable verweist, wird mit dem Neueintrag verlinkt
                                                            DBKernel.sendRequest("UPDATE "
                                                                    + DBKernel.delimitL(tableName) + " SET "
                                                                    + DBKernel.delimitL(myT.getFieldNames()[ii])
                                                                    + "=" + lID + " WHERE "
                                                                    + DBKernel.delimitL("ID") + "=" + lastID,
                                                                    false);
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                            /*
                                             * for (int j=0;j<numCols;j++) {
                                             * if (myForeignTables[j] !=
                                             * null && ffieldTypes[j] !=
                                             * null) { MyTable[] foreignTs =
                                             * myT.getForeignFields(); for
                                             * (int
                                             * ii=0;ii<foreignTs.length;
                                             * ii++) { if (foreignTs[ii] !=
                                             * null && foreignTs[ii].equals(
                                             * myForeignTables[j])) { if
                                             * (psForeign
                                             * .get(myForeignTables
                                             * [j]).executeUpdate() > 0) {
                                             * // INSERT int lID =
                                             * DBKernel.getLastInsertedID
                                             * (psForeign
                                             * .get(myForeignTables[j]));
                                             * DBKernel
                                             * .sendRequest("UPDATE " +
                                             * DBKernel.delimitL(tableName)
                                             * + " SET " +
                                             * DBKernel.delimitL(
                                             * myT.getFieldNames()[ii]) +
                                             * "=" + lID + " WHERE " +
                                             * DBKernel.delimitL("ID") + "="
                                             * + lastID, false); } break; }
                                             * } } }
                                             */
                                        }
                                    } catch (Exception e1) {
                                        numFailed++;
                                        MyLogger.handleMessage(ps.toString());
                                        MyLogger.handleException(e1);
                                    }
                                }
                                if (progress != null) {
                                    lfd++;
                                    progress.setValue(lfd);
                                }
                            }
                        }

                        myT.doMNs();
                        if (progress != null) {
                            // Refreshen:
                            MyDBTable myDB = DBKernel.mainFrame.getMyList().getMyDBTable();
                            if (myDB.getActualTable() != null) {
                                String actTablename = myDB.getActualTable().getTablename();
                                if (actTablename.equals(tableName)
                                        || actTablename.equals(DBKernel.getCodesName(tableName))) {
                                    myDB.setTable(myDB.getActualTable());
                                }
                            }
                            MyDBTree myTR = DBKernel.mainFrame.getMyList().getMyDBTree();
                            if (myTR.getActualTable() != null) {
                                String actTablename = myTR.getActualTable().getTablename();
                                if (actTablename.equals(tableName)
                                        || actTablename.equals(DBKernel.getCodesName(tableName))) {
                                    myTR.setTable(myTR.getActualTable());
                                }
                            }
                        }
                    } else {
                        System.err.println(tableName + " nicht in DB???");
                    }
                }
                if (progress != null) {
                    progress.setVisible(false);
                }
                String log = numSuccess + " erfolgreiche Importe.\n";
                log += numFailed + " fehlgeschlagene Importe.\n";
                if (unusedFields.length() > 0)
                    log += "Unbekannte Felder: " + unusedFields.substring(1) + "\n";
                if (showResults) {
                    InfoBox ib = new InfoBox(log, true, new Dimension(400, 300), null);
                    ib.setVisible(true);
                } else {
                    System.out.println("GeneralXLSImporter (" + filename + "):\n" + log);
                }
            } catch (Exception e) {
                MyLogger.handleException(e);
            }
        }
    };

    Thread thread = new Thread(runnable);
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        MyLogger.handleException(e);
    }
    return "";
}