java實現excel文件轉換為xml文件


一、導包:

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>

二、代碼部分:

public class AnalysisEtoX {

    public static void main(String[] args) {
        try {
            System.out.println("=============");
            // 用輸入流從本地拿到對應的Excel文件
            InputStream stream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\試題.xlsx");
            // 指定要生成的xml的路徑,並構建文件對象
            File f = new File("test.xml");// 新建個file對象把解析之后得到的xml存入改文件中
            writerXML(stream, f);// 將數據以xml形式寫入文本
        } catch (FileNotFoundException e) {
            System.out.println("未找到指定路徑的文件!");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void writerXML(InputStream stream, File f) throws IOException {
        System.out.println("into writerXML");
        FileOutputStream fo = new FileOutputStream(f);// 得到輸入流
        Document doc = readExcell(stream);// 讀取EXCEL函數
        Format format = Format.getCompactFormat().setEncoding("UTF-8").setIndent("");
        XMLOutputter XMLOut = new XMLOutputter(format);// 在元素后換行,每一層元素縮排四格
        XMLOut.output(doc, fo);
        fo.close();
    }

    private static Document readExcell(InputStream stream) {
        System.out.println("into readExcell");
        // 設置根<tax_institutions></tax_institutions>元素
        Element root = new Element("tax_institutions");
        Document doc = new Document(root);
        try {
            HSSFWorkbook hw = new HSSFWorkbook(stream);
            // 獲取工作薄的個數,即一個excel文件中包含了多少個Sheet工作簿
            int WbLength = hw.getNumberOfSheets();
            System.out.println("WbLength=" + WbLength);

            for (int i = 0; i < WbLength; i++) {
                HSSFSheet shee = hw.getSheetAt(i);
                int length = shee.getLastRowNum();
                System.out.println("行數:" + length);
                for (int j = 1; j <= length; j++) {
                    HSSFRow row = shee.getRow(j);
                    if (row == null) {
                        continue;
                    }
                    int cellNum = row.getPhysicalNumberOfCells();// 獲取一行中最后一個單元格的位置
                    System.out.println("列數cellNum:" + cellNum);
                    Element e = null;
                    // 設置根元素下的並列元素<tax_institution></tax_institution>
                    e = new Element("tax_institution");
                    // Element[] es = new Element[16];
                    for (int k = 0; k < cellNum; k++) {
                        HSSFCell cell = row.getCell((short) k);
                        String temp = get(k);
                        System.out.print(k + " " + temp + ":");
                        Element item = new Element(temp);
                        if (cell == null) {
                            item.setText("");
                            e.addContent(item);
                            cellNum++;// 如果存在空列,那么cellNum增加1,這一步很重要。
                            continue;
                        }

                        else {
                            String cellvalue = "";
                            switch (cell.getCellType()) {
                            // 如果當前Cell的Type為NUMERIC
                            case HSSFCell.CELL_TYPE_NUMERIC:
                            case HSSFCell.CELL_TYPE_FORMULA: {
                                // 判斷當前的cell是否為Date
                                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                    // 如果是Date類型則,轉化為Data格式

                                    // 方法1:這樣子的data格式是帶時分秒的:2011-10-12 0:00:00
                                    // cellvalue =
                                    cell.getDateCellValue().toLocaleString();

                                    // 方法2:這樣子的data格式是不帶帶時分秒的:2011-10-12
                                    Date date = cell.getDateCellValue();
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                    cellvalue = sdf.format(date);
                                    item.setText(cellvalue);

                                }
                                // 如果是純數字
                                else {
                                    // 取得當前Cell的數值
                                    cellvalue = String.valueOf((int) cell.getNumericCellValue());
                                    item.setText(cellvalue);
                                }
                                break;
                            }
                            // 如果當前Cell的Type為STRIN
                            case HSSFCell.CELL_TYPE_STRING:
                                // 取得當前的Cell字符串
                                cellvalue = cell.getRichStringCellValue().getString();
                                item.setText(cellvalue);
                                break;
                            // 默認的Cell值
                            default:
                                cellvalue = " ";
                                item.setText(cellvalue);
                            }
                            e.addContent(item);
                            System.out.println(cellvalue);
                        }
                    }
                    root.addContent(e);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
        return doc;
    }

    // 設置並列元素里的子元素名
    private static String get(int k) {
        String test = "";
        switch (k) {
        case 0:
            test = "org_name";
            break;
        case 1:
            test = "legal_mobile_phone";
            break;
        case 2:
            test = "org_address";
            break;
        case 3:
            test = "cert_type";
            break;
        case 4:
            test = "postal_code";
            break;
        case 5:
            test = "reg_sum";
            break;
        case 6:
            test = "business_scope";
            break;
        case 7:
            test = "social_credit_code";
            break;
        case 8:
            test = "reg_type";
            break;
        case 9:
            test = "legal_person_name";
            break;
        case 10:
            test = "cert_number";
            break;
        case 11:
            test = "found_time";
            break;
        case 12:
            test = "service_status";
            break;
        case 13:
            test = "staff_sum";
            break;
        case 14:
            test = "partner_sum";
            break;
        case 15:
            test = "is_branch_org";
            break;
        default:
        }
        return test;

    }

}

轉載地址csdn:https://blog.csdn.net/weixin_40420734/article/details/79538772

 

注意:

excel版本保存文件的后綴名問題,如果是.xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook。

版本錯誤會拋出OfficeXmlFileException!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM