亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Java 將 Excel 單元格值附加到 XML 中

使用 Java 將 Excel 單元格值附加到 XML 中

DIEA 2021-06-08 14:01:14
我一直在嘗試使用循環將 Excel 值附加到某些特定的 xml 節點中。我現在使用的代碼: public ExcelReaderAndWriter(String inputFileName,String outputFileName) throws IOException, InvalidFormatException {    // Creating a Workbook from an Excel file (.xls or .xlsx)    try (Workbook workbook = WorkbookFactory.create(new File(inputFileName))) {        // Getting the Sheet at index zero        Sheet sheet = workbook.getSheet("XSL_RULES");        // Create a DataFormatter to format and get each cell's value as String        DataFormatter dataFormatter = new DataFormatter();        //obtain a rowIterator and columnIterator and iterate over them        System.out.println("\n\nIterating over Rows and Columns using Iterator\n");        Iterator <Row> rowIterator = sheet.rowIterator();        while (rowIterator.hasNext()) {            Row row = rowIterator.next();            // Now let's iterate over the columns of the current row            Iterator <Cell> cellIterator = row.cellIterator();            if (cellIterator.hasNext()) {                Cell cell = cellIterator.next();                String cellValue = dataFormatter.formatCellValue(cell);                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();                DocumentBuilder docBuilder = null;                try {                    docBuilder = docFactory.newDocumentBuilder();                } catch (ParserConfigurationException e) {                    e.printStackTrace();                }所以這段代碼“以某種方式”工作,但我在遞歸獲取每個元素時遇到問題。當我執行它時,它會將文件創建到某個位置(通常是文件的末尾和最后一個單元格值。我想我在 While 循環中也犯了一個錯誤,因為它在一次執行后永遠不會停止,多次循環在最后停止。這里的錯誤在哪里,我怎樣才能以更好、更干凈的方式改進它?謝謝!編輯:我找到了解決方案并編輯了代碼。這是工作。問題是我的 for 循環,我找不到如何計算行號并根據該循環獲取數字。這是關于整張紙和行號。畢竟沒有那么難。希望對其他人有所幫助。
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

工作解決方案是:


public ExcelReaderAndWriter(String inputFileName,String outputFileName) throws IOException, InvalidFormatException {


// Creating a Workbook from an Excel file (.xls or .xlsx)



try (Workbook workbook = WorkbookFactory.create(new File(inputFileName))) {




    // Getting the Sheet at index zero

    Sheet sheet = workbook.getSheet("XSL_RULES");


    // Create a DataFormatter to format and get each cell's value as String

    DataFormatter dataFormatter = new DataFormatter();


    //obtain a rowIterator and columnIterator and iterate over them

    System.out.println("\n\nIterating over Rows and Columns using Iterator\n");


    Iterator <Row> rowIterator = sheet.rowIterator();

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();


        // Now let's iterate over the columns of the current row

        Iterator <Cell> cellIterator = row.cellIterator();


        if (cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            String cellValue = dataFormatter.formatCellValue(cell);


            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder docBuilder = null;

            try {

                docBuilder = docFactory.newDocumentBuilder();

            } catch (ParserConfigurationException e) {

                e.printStackTrace();

            }

            Document doc = docBuilder.newDocument();

            Element rootElement = doc.createElement("ROOT");

            doc.appendChild(rootElement);

            // TOMORROW CREATE A LOOP TO GET ALL THE FILES and NODES.


            for (int k = 1; k <= sheet.getLastRowNum(); k++) {

                Element xslt_rule = doc.createElement("RULES");


                    xslt_rule.setAttribute("ATTR1"sheet.getRow (k).getCell (0).getStringCellValue ());

                    xslt_rule.setAttribute("ATTR2", sheet.getRow (k).getCell (1).getStringCellValue ());

                    xslt_rule.setAttribute("ATTR3", sheet.getRow (k).getCell (2).getStringCellValue ());


                }


                xslt_rule.appendChild(doc.createTextNode(sheet.getRow (k).getCell (28).getStringCellValue ());

                rootElement.appendChild(xslt_rule);



            }

            TransformerFactory transformerFactory = TransformerFactory.newInstance();

            Transformer transformer = null;


            try


            {

                transformer = transformerFactory.newTransformer();

            } catch (TransformerConfigurationException e)


            {

                e.printStackTrace();

            }


            DOMSource source = new DOMSource(doc);

            StreamResult result = new StreamResult(new File(outputFileName));



            try {

                transformer.transform(source, result);

            } catch (TransformerException e) {

                e.printStackTrace();

            }



        }

        try {

            workbook.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


}

}


查看完整回答
反對 回復 2021-06-10
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號