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

Ram*_*amm 0 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    
Run Code Online (Sandbox Code Playgroud)

现在,我将此数据转换为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>
Run Code Online (Sandbox Code Playgroud)

我试过这个.我知道我必须使用'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();        
}   
}
Run Code Online (Sandbox Code Playgroud)

}

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

谢谢Ramm

g05*_*051 5

您不应该使用InputStream直接读取文件.打开工作簿并访问工作表后,您可以迭代工作表中的所有行,然后迭代行中的单元格.示例位于Apache POI站点http://poi.apache.org/spreadsheet/quick-guide.html#Iterator上.然后,您可以像手动一样手动打印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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这会产生输出:

<?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>
Run Code Online (Sandbox Code Playgroud)