I am reading an excel (XSLX) file using Java in netbeans 7.0.
I am able to read the excel sheet contents and print it to output too.
Now I have to convert the excel data into XML file. The tags will be the column headers and each row goes into the corresponding tags.
This is the input worksheet in a xslx file. The ID, Variable, desc and notes are the column headers.
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
Now, I am converting this data into an XML file.
The output I am expecting is,
<?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>
I tried until this. I know i have to use ‘sheet’ object. But I am not sure, how to use this.
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();
}
}
}
Please help me in framing out the input data in the xslx to the output data into xml as shown above.
Thanks
Ramm
You shouldn’t use the InputStream to read the file directly. Once you’ve opened the workbook and accessed the Sheet, you can just iterate over all the rows in the sheet, then iterate the cells in the row. Example is on the Apache POI site at http://poi.apache.org/spreadsheet/quick-guide.html#Iterator. Then you can either print out your XML by hand like you’re doing, or use one of the many XML libraries and a DTD to do it for you.
Here’s the complete source that works:
This produces output: