Search in sources :

Example 16 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project javautils by jiadongpo.

the class XSSFCellTest method testPoiExcle2007.

@Test
public void testPoiExcle2007() {
    List<Map<Integer, String>> list = null;
    BufferedInputStream bis = null;
    try {
        // 把一张xls的数据表读到wb里
        String xlsxFilePath = "/Users/yp-tc-m-2684/Downloads/B2T/20160905142026sale.xls";
        File xlsxFile = new File(xlsxFilePath);
        InputStream in = new FileInputStream(xlsxFile);
        bis = new BufferedInputStream(in);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bis);
        list = new ArrayList<Map<Integer, String>>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
            if (xssfSheet == null) {
                continue;
            }
            /**
             * 列字段
             */
            for (int j = 4; j < 5; j++) {
                XSSFRow xssfRow = xssfSheet.getRow(j);
                if (xssfRow == null) {
                    System.out.println("列字段名称行为空,跳过!行[" + xssfRow + "]");
                    continue;
                }
                Map<String, Integer> indexColumnMaps = new HashMap<String, Integer>();
                for (Cell cell : xssfRow) {
                    XSSFCell xh = (XSSFCell) cell;
                    indexColumnMaps.put(getValue(xh).trim(), xh.getColumnIndex());
                }
                fileParse.setIndexColumnMaps(indexColumnMaps);
                System.out.println("通过parseXLS2007MUB2T获取列字段名[" + transMapToString(indexColumnMaps) + "]");
            }
            // 循环行Row
            for (int rowNum = 4; rowNum <= xssfSheet.getLastRowNum() - 6; rowNum++) {
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                if (xssfRow == null) {
                    continue;
                }
                Map<Integer, String> map = new HashMap<Integer, String>();
                int i = 1;
                for (Cell cell : xssfRow) {
                    XSSFCell xh = (XSSFCell) cell;
                    if (i == 18) {
                        xh.setCellType(XSSFCell.CELL_TYPE_STRING);
                        Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.valueOf(xh.getStringCellValue()));
                        String value = XSSFCellTest.sdfDateTime.format(date);
                        map.put(Integer.valueOf(i - 1), value);
                    } else {
                        map.put(Integer.valueOf(i - 1), getValue(xh).trim());
                    }
                    i++;
                }
                // System.out.println("map----size:" + map.size());
                list.add(map);
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) BufferedInputStream(java.io.BufferedInputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Example 17 with XSSFRow

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

the class ItemStatIndexer method run.

public static void run() {
    FileInputStream stream = null;
    List<Item> itemList = new ArrayList<Item>();
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter("items.txt"));
        BufferedWriter writerUnknown = new BufferedWriter(new FileWriter("itemsUnknown.txt"));
        long startIndex = Starts.ITEM;
        int ch;
        stream = new FileInputStream(EXE_NAME);
        stream.skip(startIndex);
        Set<String> unknownAttributes = new TreeSet<String>();
        // Name
        InputStreamReader isr = new InputStreamReader(stream, "ISO-8859-1");
        Reader in = new BufferedReader(isr);
        int rowNumber = 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();
            Item item = new Item();
            item.parameters = new TreeMap<String, Object>();
            item.parameters.put("id", rowNumber);
            item.parameters.put("name", name.toString());
            short constlevel = getBytes1(startIndex + 36);
            item.parameters.put("constlevel", constlevel == -1 ? "" : constlevel * 2);
            short mainpath = getBytes1(startIndex + 37);
            String[] paths = { "F", "A", "W", "E", "S", "D", "N", "B" };
            item.parameters.put("mainpath", mainpath == -1 ? "" : paths[mainpath]);
            item.parameters.put("mainlevel", getBytes1(startIndex + 39));
            short secondarypath = getBytes1(startIndex + 38);
            item.parameters.put("secondarypath", secondarypath == -1 ? "" : paths[secondarypath]);
            short secondarylevel = getBytes1(startIndex + 40);
            item.parameters.put("secondarylevel", secondarylevel == 0 ? "" : secondarylevel);
            short type = getBytes1(startIndex + 41);
            String[] types = { "1-h wpn", "2-h wpn", "missile", "shield", "armor", "helm", "boots", "misc" };
            item.parameters.put("type", type < 1 ? "" : types[type - 1]);
            int weapon = getBytes2(startIndex + 44);
            item.parameters.put("weapon", weapon == 0 ? "" : weapon);
            int armor = getBytes2(startIndex + 46);
            item.parameters.put("armor", armor == 0 ? "" : armor);
            item.parameters.put("itemspell", getString(startIndex + 48));
            List<AttributeValue> attributes = getAttributes(startIndex + Starts.ITEM_ATTRIBUTE_OFFSET, Starts.ITEM_ATTRIBUTE_GAP);
            for (AttributeValue attr : attributes) {
                boolean found = false;
                for (int x = 0; x < KNOWN_ITEM_ATTRS.length; x++) {
                    if (KNOWN_ITEM_ATTRS[x][0].equals(attr.attribute)) {
                        found = true;
                        if (KNOWN_ITEM_ATTRS[x][1].endsWith("#")) {
                            if (KNOWN_ITEM_ATTRS[x][1].startsWith("restricted")) {
                                int i = 1;
                                for (String value : attr.values) {
                                    item.parameters.put(KNOWN_ITEM_ATTRS[x][1].replace("#", i + ""), Integer.parseInt(value) - 100);
                                    i++;
                                }
                            } else {
                                int i = 1;
                                for (String value : attr.values) {
                                    item.parameters.put(KNOWN_ITEM_ATTRS[x][1].replace("#", i + ""), Integer.parseInt(value));
                                    i++;
                                }
                            }
                        } else {
                            switch(KNOWN_ITEM_ATTRS[x][1]) {
                                case ("elemental"):
                                    item.parameters.put("F", attr.values.get(0));
                                    item.parameters.put("A", attr.values.get(0));
                                    item.parameters.put("W", attr.values.get(0));
                                    item.parameters.put("E", attr.values.get(0));
                                    break;
                                case ("sorcery"):
                                    item.parameters.put("S", attr.values.get(0));
                                    item.parameters.put("D", attr.values.get(0));
                                    item.parameters.put("N", attr.values.get(0));
                                    item.parameters.put("B", attr.values.get(0));
                                    break;
                                case ("all"):
                                    item.parameters.put("F", attr.values.get(0));
                                    item.parameters.put("A", attr.values.get(0));
                                    item.parameters.put("W", attr.values.get(0));
                                    item.parameters.put("E", attr.values.get(0));
                                    item.parameters.put("S", attr.values.get(0));
                                    item.parameters.put("D", attr.values.get(0));
                                    item.parameters.put("N", attr.values.get(0));
                                    item.parameters.put("B", attr.values.get(0));
                                    break;
                                case ("elemental range"):
                                    item.parameters.put("firerange", attr.values.get(0));
                                    item.parameters.put("airrange", attr.values.get(0));
                                    item.parameters.put("waterrange", attr.values.get(0));
                                    item.parameters.put("earthrange", attr.values.get(0));
                                    break;
                                case ("sorcery range"):
                                    item.parameters.put("astralrange", attr.values.get(0));
                                    item.parameters.put("deathrange", attr.values.get(0));
                                    item.parameters.put("naturerange", attr.values.get(0));
                                    item.parameters.put("bloodrange", attr.values.get(0));
                                    break;
                                case ("all range"):
                                    item.parameters.put("firerange", attr.values.get(0));
                                    item.parameters.put("airrange", attr.values.get(0));
                                    item.parameters.put("waterrange", attr.values.get(0));
                                    item.parameters.put("earthrange", attr.values.get(0));
                                    item.parameters.put("astralrange", attr.values.get(0));
                                    item.parameters.put("deathrange", attr.values.get(0));
                                    item.parameters.put("naturerange", attr.values.get(0));
                                    item.parameters.put("bloodrange", attr.values.get(0));
                                    break;
                                case // HACK
                                ("forbidden light"):
                                    item.parameters.put("F", 2);
                                    item.parameters.put("S", 2);
                                    break;
                                default:
                                    item.parameters.put(KNOWN_ITEM_ATTRS[x][1], attr.values.get(0));
                            }
                        }
                    }
                }
                if (!found) {
                    item.parameters.put("\tUnknown Attribute<" + attr.attribute + ">", attr.values.get(0));
                    unknownAttributes.add(attr.attribute);
                }
            }
            if (attributes.contains(new AttributeValue("8500"))) {
                item.parameters.put("startbattlespell", "");
                item.parameters.put("autocombatspell", getString(startIndex + 84));
            } else {
                item.parameters.put("startbattlespell", getString(startIndex + 84));
                item.parameters.put("autocombatspell", "");
            }
            List<String> largeBitmap = largeBitmap(startIndex + 208, values);
            for (String bit : largeBitmap) {
                if (bit.equals("airshield")) {
                    item.parameters.put(bit, 80);
                } else {
                    item.parameters.put(bit, 1);
                }
            }
            itemList.add(item);
            stream = new FileInputStream(EXE_NAME);
            startIndex = startIndex + 232l;
            stream.skip(startIndex);
            isr = new InputStreamReader(stream, "ISO-8859-1");
            in = new BufferedReader(isr);
            rowNumber++;
        }
        in.close();
        stream.close();
        XSSFWorkbook wb = new XSSFWorkbook();
        FileOutputStream fos = new FileOutputStream("BaseI.xlsx");
        XSSFSheet sheet = wb.createSheet();
        int rowNum = 0;
        for (Item item : itemList) {
            if (rowNum == 0) {
                XSSFRow row = sheet.createRow(rowNum);
                for (int i = 0; i < items_columns.length; i++) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(items_columns[i]);
                }
                rowNum++;
            }
            XSSFRow row = sheet.createRow(rowNum);
            for (int i = 0; i < items_columns.length; i++) {
                Object object = item.parameters.get(items_columns[i]);
                if (object != null) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(object.toString());
                }
            }
            dumpTextFile(item, writer);
            rowNum++;
        }
        dumpUnknown(unknownAttributes, writerUnknown);
        writerUnknown.close();
        writer.close();
        wb.write(fos);
        fos.close();
        wb.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) TreeSet(java.util.TreeSet) 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 18 with XSSFRow

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

the class NationStatIndexer method run.

public static void run() {
    FileInputStream stream = null;
    List<Nation> nationList = new ArrayList<Nation>();
    try {
        long startIndex = Starts.NATION;
        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 rowNumber = 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();
            Nation nation = new Nation();
            nation.parameters = new HashMap<String, Object>();
            nation.parameters.put("id", rowNumber - 1);
            nation.parameters.put("name", name.toString());
            nation.parameters.put("epithet", getString(startIndex + 36l));
            nation.parameters.put("abbreviation", getString(startIndex + 72l));
            String fileNameBase = getString(startIndex + 77l);
            nation.parameters.put("file_name_base", fileNameBase);
            nation.parameters.put("era", getBytes2(startIndex + 168l));
            List<Attribute> attributes = new ArrayList<Attribute>();
            long newIndex = startIndex + 172l;
            int attrib = getBytes4(newIndex);
            long valueIndex = newIndex + 388l;
            long value = getBytes4(valueIndex);
            while (attrib != 0) {
                attributes.add(new Attribute(rowNumber - 1, attrib, value));
                newIndex += 4;
                valueIndex += 8;
                attrib = getBytes4(newIndex);
                value = getBytes4(valueIndex);
            }
            nation.attributes = attributes;
            Map<unitType, List<Troops>> unitMap = new HashMap<unitType, List<Troops>>();
            newIndex = startIndex + 1328l;
            unitType type = unitType.TROOP;
            if (unitMap.get(type) == null) {
                unitMap.put(type, new ArrayList<Troops>());
            }
            attrib = getBytes4(newIndex);
            while (attrib != 0) {
                if (attrib < 0) {
                    if (attrib != -1) {
                        type = unitType.fromValue(attrib);
                        if (unitMap.get(type) == null) {
                            unitMap.put(type, new ArrayList<Troops>());
                        }
                    }
                } else {
                    unitMap.get(type).add(new Troops(rowNumber - 1, attrib));
                }
                newIndex += 4;
                attrib = getBytes4(newIndex);
            }
            newIndex = startIndex + 1496l;
            attrib = getBytes4(newIndex);
            while (attrib != 0) {
                if (attrib < 0) {
                    if (unitMap.get(unitType.UNPRETENDER) == null) {
                        unitMap.put(unitType.UNPRETENDER, new ArrayList<Troops>());
                    }
                    unitMap.get(unitType.UNPRETENDER).add(new Troops(rowNumber - 1, Math.abs(attrib)));
                } else {
                    if (unitMap.get(unitType.PRETENDER) == null) {
                        unitMap.put(unitType.PRETENDER, new ArrayList<Troops>());
                    }
                    unitMap.get(unitType.PRETENDER).add(new Troops(rowNumber - 1, attrib));
                }
                newIndex += 4;
                attrib = getBytes4(newIndex);
            }
            nation.unitMap = unitMap;
            nationList.add(nation);
            stream = new FileInputStream(EXE_NAME);
            startIndex = startIndex + Starts.NATION_SIZE;
            stream.skip(startIndex);
            isr = new InputStreamReader(stream, "ISO-8859-1");
            in = new BufferedReader(isr);
            rowNumber++;
        }
        in.close();
        stream.close();
        XSSFWorkbook wb = new XSSFWorkbook();
        FileOutputStream fos = new FileOutputStream("nations.xlsx");
        XSSFSheet sheet = wb.createSheet();
        XSSFWorkbook wb2 = new XSSFWorkbook();
        FileOutputStream fos2 = new FileOutputStream("attributes_by_nation.xlsx");
        XSSFSheet sheet2 = wb2.createSheet();
        int rowNum = 0;
        int attributesNum = 0;
        for (Nation nation : nationList) {
            // nations
            if (rowNum == 0) {
                XSSFRow row = sheet.createRow(rowNum);
                for (int i = 0; i < nations_columns.length; i++) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(nations_columns[i]);
                }
                rowNum++;
            }
            XSSFRow row = sheet.createRow(rowNum);
            for (int i = 0; i < nations_columns.length; i++) {
                Object object = nation.parameters.get(nations_columns[i]);
                if (object != null) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(object.toString());
                }
            }
            // attributes_by_nation
            for (Attribute attribute : nation.attributes) {
                if (attributesNum == 0) {
                    row = sheet2.createRow(attributesNum);
                    for (int i = 0; i < attributes_by_nation_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attributes_by_nation_columns[i]);
                    }
                    attributesNum++;
                }
                row = sheet2.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++;
            }
            for (Map.Entry<unitType, List<Troops>> entry : nation.unitMap.entrySet()) {
                if (entry.getKey() == unitType.UNKNOWN) {
                    continue;
                }
                if (entry.getKey().getRowNum() == 0) {
                    entry.getKey().wb = new XSSFWorkbook();
                    entry.getKey().fos = new FileOutputStream(entry.getKey().getFilename() + ".xlsx");
                    entry.getKey().sheet = entry.getKey().wb.createSheet();
                    row = entry.getKey().sheet.createRow(0);
                    for (int i = 0; i < troops_by_nation_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(troops_by_nation_columns[i]);
                    }
                    entry.getKey().incrementRowNum();
                }
                for (Troops troop : entry.getValue()) {
                    row = entry.getKey().sheet.createRow(entry.getKey().getRowNum());
                    row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(troop.monster_number);
                    row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(troop.nation_number);
                    entry.getKey().incrementRowNum();
                }
            }
            rowNum++;
        }
        wb.write(fos);
        fos.close();
        wb.close();
        wb2.write(fos2);
        fos2.close();
        wb2.close();
        for (Nation nation : nationList) {
            for (Map.Entry<unitType, List<Troops>> entry : nation.unitMap.entrySet()) {
                if (entry.getKey().rowNum != -1) {
                    entry.getKey().wb.write(entry.getKey().fos);
                    entry.getKey().fos.close();
                    entry.getKey().wb.close();
                    entry.getKey().rowNum = -1;
                }
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ArrayList(java.util.ArrayList) List(java.util.List) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with XSSFRow

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

the class SpellStatIndexer method run.

public static void run() {
    FileInputStream stream = null;
    List<Spell> spellList = new ArrayList<Spell>();
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter("spells.txt"));
        long startIndex = Starts.SPELL;
        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 spellNumber = 0;
        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();
            Spell spell = new Spell();
            spell.parameters = new TreeMap<String, Object>();
            spell.parameters.put("id", spellNumber);
            spell.parameters.put("effect_record_id", spellNumber);
            spell.parameters.put("name", name.toString());
            spell.parameters.put("school", getBytes1(startIndex + 36l));
            spell.parameters.put("researchlevel", getBytes1(startIndex + 37l));
            spell.parameters.put("path1", getBytes1(startIndex + 38l));
            spell.parameters.put("path2", getBytes1(startIndex + 39l));
            spell.parameters.put("pathlevel1", getBytes1(startIndex + 40l));
            spell.parameters.put("pathlevel2", getBytes1(startIndex + 41l));
            int fat = getBytes2(startIndex + 42l);
            spell.parameters.put("fatiguecost", fat % 100);
            spell.parameters.put("gemcost", fat / 100);
            Effect effect = new Effect();
            int raw_area = getBytes2(startIndex + 44);
            switch(raw_area) {
                case 666:
                    effect.area_battlefield_pct = 100;
                    break;
                case 663:
                    effect.area_battlefield_pct = 50;
                    break;
                case 665:
                    effect.area_battlefield_pct = 25;
                    break;
                case 664:
                    effect.area_battlefield_pct = 10;
                    break;
                case 662:
                    effect.area_battlefield_pct = 5;
                    break;
                default:
                    effect.area_base = raw_area % 1000;
                    effect.area_per_level = raw_area / 1000;
                    break;
            }
            int effect_number = getBytes2(startIndex + 46);
            if (effect_number > 10000) {
                effect.ritual = 1;
            } else if (effect_number > 1000) {
                effect.duration = effect_number / 1000;
            }
            effect_number = effect_number % 1000;
            effect.effect_number = effect_number;
            int raw_range = getBytes2(startIndex + 48);
            if (raw_range > 0) {
                effect.range_base = raw_range % 1000;
                effect.range_per_level = raw_range / 1000;
            } else {
                effect.range_strength_divisor = -raw_range;
            }
            spell.parameters.put("precision", getBytes1(startIndex + 50l));
            spell.parameters.put("damage", getBytes8(startIndex + 56l));
            // spell.parameters.put("Unknown51", getBytes1(startIndex + 51l));
            // spell.parameters.put("Unknown52", getBytes2(startIndex + 52l));
            // spell.parameters.put("Unknown54", getBytes2(startIndex + 54l));
            effect.raw_argument = getBytes8(startIndex + 56);
            // spell.parameters.put("Unknown66", getBytes2(startIndex + 66l));
            // spell.parameters.put("Unknown68", getBytes2(startIndex + 68l));
            // spell.parameters.put("Unknown70", getBytes2(startIndex + 70l));
            // spell.parameters.put("Unknown72", getBytes2(startIndex + 72l));
            // spell.parameters.put("Unknown74", getBytes2(startIndex + 74l));
            // spell.parameters.put("Unknown76", getBytes2(startIndex + 76l));
            // spell.parameters.put("Unknown78", getBytes2(startIndex + 78l));
            // spell.parameters.put("Unknown90", getBytes2(startIndex + 90l));
            // 
            // spell.parameters.put("Unknown66-1", getBytes1(startIndex + 66l));
            // spell.parameters.put("Unknown67-1", getBytes1(startIndex + 67l));
            // spell.parameters.put("Unknown68-1", getBytes1(startIndex + 68l));
            // spell.parameters.put("Unknown69-1", getBytes1(startIndex + 69l));
            // spell.parameters.put("Unknown70-1", getBytes1(startIndex + 70l));
            // spell.parameters.put("Unknown71-1", getBytes1(startIndex + 71l));
            // spell.parameters.put("Unknown72-1", getBytes1(startIndex + 72l));
            // 
            spell.parameters.put("effects_count", getBytes2(startIndex + 64l));
            effect.modifiers_mask = getBytes8(startIndex + 80);
            spell.parameters.put("next_spell", getBytes2(startIndex + 88l));
            effect.record_number = spellNumber;
            effect.object_type = "Spell";
            spell.effect = effect;
            List<Attribute> attributes = new ArrayList<Attribute>();
            long newIndex = startIndex + 92l;
            int attrib = getBytes4(newIndex);
            long valueIndex = newIndex + 60l;
            long value = getBytes4(valueIndex);
            while (attrib != 0) {
                attributes.add(new Attribute(spellNumber, attrib, value));
                newIndex += 4;
                valueIndex += 4;
                attrib = getBytes4(newIndex);
                value = getBytes4(valueIndex);
            }
            spell.attributes = attributes;
            spellList.add(spell);
            stream = new FileInputStream(EXE_NAME);
            startIndex = startIndex + Starts.SPELL_SIZE;
            stream.skip(startIndex);
            isr = new InputStreamReader(stream, "ISO-8859-1");
            in = new BufferedReader(isr);
            spellNumber++;
        }
        in.close();
        stream.close();
        XSSFWorkbook wb = new XSSFWorkbook();
        FileOutputStream fos = new FileOutputStream("spells.xlsx");
        XSSFSheet sheet = wb.createSheet();
        XSSFWorkbook wb2 = new XSSFWorkbook();
        FileOutputStream fos2 = new FileOutputStream("effects_spells.xlsx");
        XSSFSheet sheet2 = wb2.createSheet();
        XSSFWorkbook wb3 = new XSSFWorkbook();
        FileOutputStream fos3 = new FileOutputStream("attributes_by_spell.xlsx");
        XSSFSheet sheet3 = wb3.createSheet();
        int rowNum = 0;
        int effectsNum = 0;
        int attributesNum = 0;
        for (Spell spell : spellList) {
            // spells
            if (rowNum == 0) {
                XSSFRow row = sheet.createRow(rowNum);
                for (int i = 0; i < spells_columns.length; i++) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spells_columns[i]);
                }
                rowNum++;
            }
            XSSFRow row = sheet.createRow(rowNum);
            for (int i = 0; i < spells_columns.length; i++) {
                Object object = spell.parameters.get(spells_columns[i]);
                if (object != null) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(object.toString());
                }
            }
            // effects_spells
            if (spell.effect != null) {
                if (effectsNum == 0) {
                    row = sheet2.createRow(effectsNum);
                    for (int i = 0; i < effects_spells_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(effects_spells_columns[i]);
                    }
                    effectsNum++;
                }
                row = sheet2.createRow(effectsNum);
                row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.record_number);
                row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.effect_number);
                row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.duration != 0 ? spell.effect.duration + "" : "");
                row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.ritual);
                row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.object_type);
                row.getCell(5, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(Long.toString(spell.effect.raw_argument));
                row.getCell(6, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(Long.toString(spell.effect.modifiers_mask));
                row.getCell(7, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.range_base);
                row.getCell(8, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.range_per_level);
                row.getCell(9, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.range_strength_divisor);
                row.getCell(10, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.area_base);
                row.getCell(11, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.area_per_level);
                row.getCell(12, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(spell.effect.area_battlefield_pct != 0 ? spell.effect.area_battlefield_pct + "" : "");
                effectsNum++;
            }
            // attributes_by_spell
            for (Attribute attribute : spell.attributes) {
                if (attributesNum == 0) {
                    row = sheet3.createRow(attributesNum);
                    for (int i = 0; i < attributes_by_spell_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attributes_by_spell_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(spell, writer);
            rowNum++;
        }
        writer.close();
        wb.write(fos);
        fos.close();
        wb.close();
        wb2.write(fos2);
        fos2.close();
        wb2.close();
        wb3.write(fos3);
        fos3.close();
        wb3.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 : InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 20 with XSSFRow

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

the class WeaponStatIndexer method run.

public static void run() {
    FileInputStream stream = null;
    List<Weapon> weaponList = new ArrayList<Weapon>();
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter("weapons.txt"));
        long startIndex = Starts.WEAPON;
        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 weaponNumber = 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();
            Weapon weapon = new Weapon();
            weapon.parameters = new HashMap<String, Object>();
            weapon.parameters.put("id", weaponNumber);
            weapon.parameters.put("name", name.toString());
            weapon.parameters.put("effect_record_id", weaponNumber);
            weapon.parameters.put("att", getBytes2(startIndex + 48));
            weapon.parameters.put("def", getBytes2(startIndex + 50));
            weapon.parameters.put("len", getBytes2(startIndex + 54));
            weapon.parameters.put("nratt", getBytes2(startIndex + 58));
            weapon.parameters.put("ammo", getBytes2(startIndex + 60));
            int bytes2 = getBytes2(startIndex + 72);
            weapon.parameters.put("secondaryeffect", bytes2 > 0 ? bytes2 : 0);
            weapon.parameters.put("secondaryeffectalways", bytes2 < 0 ? Math.abs(bytes2) : 0);
            weapon.parameters.put("rcost", getBytes2(startIndex + 86));
            List<Attribute> attributes = new ArrayList<Attribute>();
            long newIndex = startIndex + 88;
            int attrib = getBytes4(newIndex);
            long valueIndex = newIndex + 12l;
            long value = getBytes4(valueIndex);
            while (attrib != 0) {
                attributes.add(new Attribute(weaponNumber, attrib, value));
                newIndex += 4;
                valueIndex += 4;
                attrib = getBytes4(newIndex);
                value = getBytes4(valueIndex);
            }
            weapon.attributes = attributes;
            newIndex = startIndex + 52;
            int effect_number = getBytes2(newIndex);
            if (effect_number != 0) {
                Effect effect = new Effect();
                effect.effect_number = effect_number;
                effect.record_number = weaponNumber;
                effect.object_type = "Weapon";
                effect.ritual = 0;
                effect.modifiers_mask = getBytes6(startIndex + 64);
                effect.raw_argument = getBytes2(startIndex + 40);
                effect.range_base = getBytes2(startIndex + 56);
                effect.area_base = getBytes2(startIndex + 82);
                weapon.effect = effect;
            }
            weaponList.add(weapon);
            stream = new FileInputStream(EXE_NAME);
            startIndex = startIndex + Starts.WEAPON_SIZE;
            stream.skip(startIndex);
            isr = new InputStreamReader(stream, "ISO-8859-1");
            in = new BufferedReader(isr);
            weaponNumber++;
        }
        in.close();
        stream.close();
        XSSFWorkbook wb = new XSSFWorkbook();
        FileOutputStream fos = new FileOutputStream("weapons.xlsx");
        XSSFSheet sheet = wb.createSheet();
        XSSFWorkbook wb2 = new XSSFWorkbook();
        FileOutputStream fos2 = new FileOutputStream("effects_weapons.xlsx");
        XSSFSheet sheet2 = wb2.createSheet();
        XSSFWorkbook wb3 = new XSSFWorkbook();
        FileOutputStream fos3 = new FileOutputStream("attributes_by_weapon.xlsx");
        XSSFSheet sheet3 = wb3.createSheet();
        int rowNum = 0;
        int effectsNum = 0;
        int attributesNum = 0;
        for (Weapon weapon : weaponList) {
            // BaseW
            if (rowNum == 0) {
                XSSFRow row = sheet.createRow(rowNum);
                for (int i = 0; i < weapons_columns.length; i++) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(weapons_columns[i]);
                }
                rowNum++;
            }
            XSSFRow row = sheet.createRow(rowNum);
            for (int i = 0; i < weapons_columns.length; i++) {
                Object object = weapon.parameters.get(weapons_columns[i]);
                if (object != null) {
                    row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(object.toString());
                }
            }
            // effects_weapons
            if (weapon.effect != null) {
                if (effectsNum == 0) {
                    row = sheet2.createRow(effectsNum);
                    for (int i = 0; i < effects_weapons_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(effects_weapons_columns[i]);
                    }
                    effectsNum++;
                }
                row = sheet2.createRow(effectsNum);
                row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(weapon.effect.record_number);
                row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(weapon.effect.effect_number);
                row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(weapon.effect.object_type);
                row.getCell(5, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(weapon.effect.raw_argument);
                row.getCell(6, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(Long.toString(weapon.effect.modifiers_mask));
                if (weapon.effect.range_base >= 0) {
                    row.getCell(7, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(weapon.effect.range_base);
                } else {
                    row.getCell(9, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(Math.abs(weapon.effect.range_base));
                }
                row.getCell(10, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(Math.abs(weapon.effect.area_base));
                effectsNum++;
            }
            // attributes_by_weapon
            for (Attribute attribute : weapon.attributes) {
                if (attributesNum == 0) {
                    row = sheet3.createRow(attributesNum);
                    for (int i = 0; i < attributes_by_weapons_columns.length; i++) {
                        row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(attributes_by_weapons_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(weapon, writer);
            rowNum++;
        }
        wb.write(fos);
        fos.close();
        wb.close();
        wb2.write(fos2);
        fos2.close();
        wb2.close();
        wb3.write(fos3);
        fos3.close();
        wb3.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 : InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

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