如何使用Java从Excel(XLSX)构建XML

时间:2011-07-04 13:27:04

标签: java xml apache-poi

我在netbeans 7.0中使用Java读取excel(XSLX)文件。 我能够读取excel表格内容并将其打印到输出中。

现在我必须将excel数据转换为XML文件。标签将是列标题,每行都会进入相应的标签。

这是xslx文件中的输入工作表。 ID,Variable,desc和notes是列标题。

ID          Variable     Desc     Notes
B0001                    VSI_C  
B0001           1        VSI_C_R    
B0001           2        VSI_C_P    
B0002                    VSI_C_L    
B0003                    VSI_C_H    
B0004                    VSI_C_O    

现在,我正在将此数据转换为XML文件。 我期待的输出是,

<?xml version="1.0" encoding="UTF-8"?>
<Bin_code>
<DCT>
    <ID>B0001</ID>
    <Variable/>
    <Desc>VSI_C</Desc>
    <Notes/>
</DCT>
<DCT>
    <ID>B0001</ID>
    <Variable/>
    <Desc>VSI_C_R</Desc>
    <Notes/>
</DCT>
     ............
     ...............
</Bin_code>

我试过这个。我知道我必须使用'sheet'对象。但我不确定,如何使用它。

  

import java.io.File;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel. Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class XSLXReader {   
public static void main(String[] args) 
{   
    DataInputStream in = null;
    BufferedReader br = null;
    FileWriter fostream;
    FileWriter fostreamBatch;
    BufferedWriter out = null;
    BufferedWriter outBatch = null;
    String strOutputPath = "D:\\Proj\\Current_\\";
    String strFilePrefix = "Master 5.2-B";
    String strLine;

    try {           
    InputStream inputStream = new FileInputStream(new File("D:\\Proj\\Current_\\Master A-B.xlsx"));
    Workbook wb = WorkbookFactory.create(inputStream);            
    Sheet sheet = wb.getSheet("Bin-code");

    in = new DataInputStream(inputStream);
    br = new BufferedReader(new InputStreamReader(in));

    fostream = new FileWriter(strOutputPath+"\\"+strFilePrefix+".xml");
    out = new BufferedWriter(fostream);

    out.write("<Bin-code>");

    while ((strLine = br.readLine()) != null)
    {
        out.write("<DCT>");
        out.write("<ID>" + strLine.substring(1, strLine.length()) + "</ID>");

        out.write("</DCT>");

    }
    out.write("</Bin-code>");
} catch (Exception e) {         
    e.printStackTrace();        
}   
}

}

如上图所示,请帮助我将xslx中的输入数据输出到xml的输出数据。

由于 RAMM

2 个答案:

答案 0 :(得分:5)

您不应该使用InputStream直接读取文件。打开工作簿并访问工作表后,您可以迭代工作表中的所有行,然后迭代行中的单元格。示例位于{PO-3}的Apache POI站点上。然后,您可以像手动一样手动打印XML,也可以使用众多XML库和DTD之一为您完成。

这是完整的工作源:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import java.text.*;

public class XSLXReader {
    static DecimalFormat df = new DecimalFormat("#####0");

    public static void main(String[] args) {
        FileWriter fostream;
        PrintWriter out = null;
        String strOutputPath = "D:\\Proj\\Current_\\";
        String strFilePrefix = "Master 5.2-B";

        try {
            InputStream inputStream = new FileInputStream(new File("D:\\Proj\\Current_\\Master A-B.xlsx"));
            Workbook wb = WorkbookFactory.create(inputStream);
            Sheet sheet = wb.getSheet("Bin-code");

            fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+ ".xml");
            out = new PrintWriter(new BufferedWriter(fostream));

            out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            out.println("<Bin-code>");

            boolean firstRow = true;
            for (Row row : sheet) {
                if (firstRow == true) {
                    firstRow = false;
                    continue;
                }
                out.println("\t<DCT>");
                out.println(formatElement("\t\t", "ID", formatCell(row.getCell(0))));
                out.println(formatElement("\t\t", "Variable", formatCell(row.getCell(1))));
                out.println(formatElement("\t\t", "Desc", formatCell(row.getCell(2))));
                out.println(formatElement("\t\t", "Notes", formatCell(row.getCell(3))));
                out.println("\t</DCT>");
            }
            out.write("</Bin-code>");
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String formatCell(Cell cell)
    {
        if (cell == null) {
            return "";
        }
        switch(cell.getCellType()) {
            case Cell.CELL_TYPE_BLANK:
                return "";
            case Cell.CELL_TYPE_BOOLEAN:
                return Boolean.toString(cell.getBooleanCellValue());
            case Cell.CELL_TYPE_ERROR:
                return "*error*";
            case Cell.CELL_TYPE_NUMERIC:
                return XSLXReader.df.format(cell.getNumericCellValue());
            case Cell.CELL_TYPE_STRING:
                return cell.getStringCellValue();
            default:
                return "<unknown value>";
        }
    }

    private static String formatElement(String prefix, String tag, String value) {
        StringBuilder sb = new StringBuilder(prefix);
        sb.append("<");
        sb.append(tag);
        if (value != null && value.length() > 0) {
            sb.append(">");
            sb.append(value);
            sb.append("</");
            sb.append(tag);
            sb.append(">");
        } else {
            sb.append("/>");
        }
        return sb.toString();
    }
}

这会产生输出:

<?xml version="1.0" encoding="UTF-8"?>
<Bin-code>
    <DCT>
        <ID>B0001</ID>
        <Variable/>
        <Desc>VSI_C</Desc>
        <Notes/>
    </DCT>
    <DCT>
        <ID>B0001</ID>
        <Variable>1</Variable>
        <Desc>VSI_C_R</Desc>
        <Notes/>
    </DCT>
    <DCT>
        <ID>B0001</ID>
        <Variable>2</Variable>
        <Desc>VSI_C_P</Desc>
        <Notes/>
    </DCT>
    <DCT>
        <ID>B0002</ID>
        <Variable/>
        <Desc>VSI_C_L</Desc>
        <Notes/>
    </DCT>
    <DCT>
        <ID>B0003</ID>
        <Variable/>
        <Desc>VSI_C_H</Desc>
        <Notes/>
    </DCT>
    <DCT>
        <ID>B0004</ID>
        <Variable/>
        <Desc>VSI_C_O</Desc>
        <Notes/>
    </DCT>
</Bin-code>

答案 1 :(得分:0)

import java.io。*;

import org.apache.poi.ss.usermodel。*;

import java.text。*;

公共类XSLXReader {

static DecimalFormat df = new DecimalFormat("#####0");

public static void main(String[] args) {

    FileWriter fostream;

    PrintWriter out = null;

    String strOutputPath = "C://Users//853053//Downloads//poi-bin-3.17-20170915.tar";

    // String strFilePrefix = "Master 5.2-B";

    try {

        InputStream inputStream = new FileInputStream(new File("C://Users//853053//Downloads//poi-bin-3.17-20170915.tar//Master 5.2-B.xls"));

        Workbook wb = WorkbookFactory.create(inputStream);

        Sheet sheet = wb.getSheet("Sheet1");

        // fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+
        // ".xml");

        // out = new PrintWriter(new BufferedWriter(fostream));

        System.out.println("try");

        boolean firstRow = true;

        for (Row row : sheet) {

            if (firstRow == true) {

                firstRow = false;

                continue;

            }

            fostream = new FileWriter(new File(strOutputPath
                    + File.separator + formatCell(row.getCell(4)) + ".xml"));

            System.out.println("try1" + strOutputPath + File.separator
                    + formatCell(row.getCell(4)) + ".xml");

            out = new PrintWriter(new BufferedWriter(fostream));

            out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

            out.println("<Bin-code>");

            out.println("\t<DCT>");

            out.println(formatElement("\t\t", "ID",
                    formatCell(row.getCell(0))));

            out.println(formatElement("\t\t", "Variable",
                    formatCell(row.getCell(1))));

            out.println(formatElement("\t\t", "Desc",
                    formatCell(row.getCell(2))));

            out.println(formatElement("\t\t", "Notes",
                    formatCell(row.getCell(3))));

            out.println(formatElement("\t\t", "txn_id",
                    formatCell(row.getCell(4))));

            out.println("\t</DCT>");

            out.write("</Bin-code>");

            out.flush();

            out.close();

        }

        // out.write("</Bin-code>");

        // out.flush();

        // out.close();

    } catch (Exception e) {

        e.printStackTrace();

    }

}

@SuppressWarnings("deprecation")
private static String formatCell(Cell cell)

{

    if (cell == null) {

        return "";

    }

    switch (cell.getCellType()) {

    case Cell.CELL_TYPE_BLANK:

        return "";

    case Cell.CELL_TYPE_BOOLEAN:

        return Boolean.toString(cell.getBooleanCellValue());

    case Cell.CELL_TYPE_ERROR:

        return "*error*";

    case Cell.CELL_TYPE_NUMERIC:

        return XSLXReader.df.format(cell.getNumericCellValue());

    case Cell.CELL_TYPE_STRING:

        return cell.getStringCellValue();

    default:

        return "<unknown value>";

    }

}

private static String formatElement(String prefix, String tag, String value) {

    StringBuilder sb = new StringBuilder(prefix);

    sb.append("<");

    sb.append(tag);

    if (value != null && value.length() > 0) {

        sb.append(">");

        sb.append(value);

        sb.append("</");

        sb.append(tag);
        sb.append(">");

    } else {

        sb.append(">");

        sb.append("<");

        sb.append(tag);

        sb.append("/>");

    }

    return sb.toString();

}

}