Search in sources :

Example 21 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project dom5utils by larzm42.

the class ArmorStatIndexer method run.

public static void run() {
    FileInputStream stream = null;
    List<Armor> armorList = new ArrayList<Armor>();
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter("armors.txt"));
        long startIndex = Starts.ARMOR;
        int ch;
        stream = new FileInputStream(EXE_NAME);
        stream.skip(startIndex);
        // name
        InputStreamReader isr = new InputStreamReader(stream, "ISO-8859-1");
        Reader in = new BufferedReader(isr);
        int armorNumber = 1;
        while ((ch = in.read()) > -1) {
            StringBuffer name = new StringBuffer();
            while (ch != 0) {
                name.append((char) ch);
                ch = in.read();
            }
            if (name.length() == 0) {
                continue;
            }
            if (name.toString().equals("end")) {
                break;
            }
            in.close();
            Armor armor = new Armor();
            armor.parameters = new HashMap<String, Object>();
            armor.parameters.put("id", armorNumber);
            armor.parameters.put("name", name.toString());
            armor.parameters.put("def", getBytes2(startIndex + 62));
            armor.parameters.put("enc", getBytes2(startIndex + 64));
            armor.parameters.put("type", getBytes2(startIndex + 66));
            armor.parameters.put("rcost", getBytes2(startIndex + 68));
            List<Protection> protections = new ArrayList<Protection>();
            long newIndex = startIndex + 36;
            int zone = getBytes2(newIndex);
            while (zone != 0) {
                newIndex += 2;
                int prot = getBytes2(newIndex);
                protections.add(new Protection(zone, prot, armorNumber));
                newIndex += 2;
                zone = getBytes2(newIndex);
            }
            armor.protections = protections;
            List<Attribute> attributes = new ArrayList<Attribute>();
            newIndex = startIndex + 72;
            int attrib = getBytes4(newIndex);
            long valueIndex = newIndex + 16l;
            long value = getBytes4(valueIndex);
            while (attrib != 0) {
                attributes.add(new Attribute(armorNumber, attrib, value));
                newIndex += 4;
                valueIndex += 4;
                attrib = getBytes4(newIndex);
                value = getBytes4(valueIndex);
            }
            armor.attributes = attributes;
            armorList.add(armor);
            stream = new FileInputStream(EXE_NAME);
            startIndex = startIndex + Starts.ARMOR_SIZE;
            stream.skip(startIndex);
            isr = new InputStreamReader(stream, "ISO-8859-1");
            in = new BufferedReader(isr);
            armorNumber++;
        }
        in.close();
        stream.close();
        XSSFWorkbook wb = new XSSFWorkbook();
        FileOutputStream fos = new FileOutputStream("armors.xlsx");
        XSSFSheet sheet = wb.createSheet();
        XSSFWorkbook wb2 = new XSSFWorkbook();
        FileOutputStream fos2 = new FileOutputStream("protections_by_armor.xlsx");
        XSSFSheet sheet2 = wb2.createSheet();
        XSSFWorkbook wb3 = new XSSFWorkbook();
        FileOutputStream fos3 = new FileOutputStream("attributes_by_armor.xlsx");
        XSSFSheet sheet3 = wb3.createSheet();
        int rowNum = 0;
        int protectionsNum = 0;
        int attributesNum = 0;
        for (Armor armor : armorList) {
            // BaseA
            if (rowNum == 0) {
                XSSFRow row = sheet.createRow(rowNum);
                for (int i = 0; i < armors_columns.length; i++) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(armors_columns[i]);
                }
                rowNum++;
            }
            XSSFRow row = sheet.createRow(rowNum);
            for (int i = 0; i < armors_columns.length; i++) {
                Object object = armor.parameters.get(armors_columns[i]);
                if (object != null) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(object.toString());
                }
            }
            // protections_by_armor
            for (Protection prot : armor.protections) {
                if (protectionsNum == 0) {
                    row = sheet2.createRow(protectionsNum);
                    for (int i = 0; i < protections_by_armor_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(protections_by_armor_columns[i]);
                    }
                    protectionsNum++;
                }
                row = sheet2.createRow(protectionsNum);
                row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(prot.zone_number);
                row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(prot.protection);
                row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(prot.armor_number);
                protectionsNum++;
            }
            // attributes_by_armor
            for (Attribute attribute : armor.attributes) {
                if (attributesNum == 0) {
                    row = sheet3.createRow(attributesNum);
                    for (int i = 0; i < attributes_by_armor_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attributes_by_armor_columns[i]);
                    }
                    attributesNum++;
                }
                row = sheet3.createRow(attributesNum);
                row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attribute.object_number);
                row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attribute.attribute);
                row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attribute.raw_value);
                attributesNum++;
            }
            dumpTextFile(armor, writer);
            rowNum++;
        }
        wb.write(fos);
        fos.close();
        wb.close();
        wb2.write(fos2);
        fos2.close();
        wb2.close();
        wb3.write(fos3);
        fos3.close();
        wb3.close();
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) BufferedWriter(java.io.BufferedWriter) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader)

Example 22 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project mots by motech-implementations.

the class LocationImporter method parseFacilities.

private void parseFacilities(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<Facility> newFacilitySet = new HashSet<>();
    DataFormatter fmt = new DataFormatter();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(FACILITY_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(FACILITY_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        String facilityId = fmt.formatCellValue(row.getCell(ID_FACILITY_COL_NUMBER));
        FacilityType facilityType = FacilityType.getByDisplayName(row.getCell(NAME_FACILITY_COL_NUMBER).getStringCellValue());
        Facility facility = new Facility(cellText, facilityType, facilityId);
        String parentChiefdomName = row.getCell(CHIEFDOM_COL_NUMBER).getStringCellValue();
        String parentDistrictName = row.getCell(DISTRICT_COL_NUMBER).getStringCellValue();
        Chiefdom parent = currentChiefdomList.stream().filter(chiefdom -> chiefdom.getName().equals(parentChiefdomName) && chiefdom.getDistrict().getName().equals(parentDistrictName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("'%s' Facility parent " + "is not defined properly in spreadsheet", facility.getName())));
        facility.setChiefdom(parent);
        newFacilitySet.add(facility);
    }
    newFacilitySet.forEach(newFacility -> {
        if (!currentFacilityList.contains(newFacility)) {
            locationService.createImportedFacility(newFacility);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Facility(org.motechproject.mots.domain.Facility) FacilityType(org.motechproject.mots.domain.enums.FacilityType) HashSet(java.util.HashSet) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 23 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project mots by motech-implementations.

the class LocationImporter method parseDistricts.

private void parseDistricts(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<District> newDistrictSet = new HashSet<>();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(DISTRICT_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(DISTRICT_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        District district = new District(cellText);
        newDistrictSet.add(district);
    }
    newDistrictSet.forEach(newDistrict -> {
        if (!currentDistrictList.contains(newDistrict)) {
            locationService.createDistrict(newDistrict);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) District(org.motechproject.mots.domain.District) HashSet(java.util.HashSet)

Example 24 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project mots by motech-implementations.

the class LocationImporter method parseCommunities.

private void parseCommunities(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<Community> newCommunitySet = new HashSet<>();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(COMMUNITY_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(COMMUNITY_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        Community community = new Community(cellText);
        String parentFacilityName = row.getCell(FACILITY_COL_NUMBER).getStringCellValue();
        String parentChiefdomName = row.getCell(CHIEFDOM_COL_NUMBER).getStringCellValue();
        String parentDistrictName = row.getCell(DISTRICT_COL_NUMBER).getStringCellValue();
        Facility parent = currentFacilityList.stream().filter(facility -> facility.getName().equals(parentFacilityName) && facility.getChiefdom().getName().equals(parentChiefdomName) && facility.getChiefdom().getDistrict().getName().equals(parentDistrictName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("'%s' Community parent " + "is not defined properly in spreadsheet", community.getName())));
        community.setFacility(parent);
        newCommunitySet.add(community);
    }
    newCommunitySet.forEach(newCommunity -> {
        if (!currentCommunityList.contains(newCommunity)) {
            locationService.createImportedCommunity(newCommunity);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Facility(org.motechproject.mots.domain.Facility) Community(org.motechproject.mots.domain.Community) HashSet(java.util.HashSet)

Example 25 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project kindergarten by clear-group-ausbildung.

the class ExportReceipt method createItemRows.

private int createItemRows(int pRowCount, int pColIndex, PayoffSoldItemsData pPayoffSoldItemData) {
    int colIndexPrice = pColIndex + 1;
    Map<Integer, Double> soldItemMap = pPayoffSoldItemData.getSoldItemNumbersPricesMap();
    for (Entry<Integer, Double> entry : soldItemMap.entrySet()) {
        XSSFRow tempRow = sheet.createRow(pRowCount);
        XSSFCell numberCell = tempRow.createCell(pColIndex);
        numberCell.setCellValue(entry.getKey());
        numberCell.setCellStyle(numberStyle);
        XSSFCell priceCell = tempRow.createCell(colIndexPrice);
        priceCell.setCellValue(soldItemMap.get(entry.getKey()));
        priceCell.setCellStyle(priceStyle);
        pRowCount++;
    }
    XSSFRow sumRow = sheet.createRow(pRowCount);
    XSSFCell labelCell = sumRow.createCell(pColIndex);
    labelCell.setCellValue("Summe:");
    labelCell.setCellStyle(sumStyle);
    XSSFCell priceCell = sumRow.createCell(colIndexPrice);
    priceCell.setCellValue(pPayoffSoldItemData.getSoldItemSum());
    priceCell.setCellStyle(priceStyle);
    return ++pRowCount;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Aggregations

XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)59 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)39 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)39 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)35 FileOutputStream (java.io.FileOutputStream)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 BufferedReader (java.io.BufferedReader)10 FileNotFoundException (java.io.FileNotFoundException)10 Test (org.junit.Test)10 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 File (java.io.File)8 HashMap (java.util.HashMap)8 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)8 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5