cancel
Showing results for 
Search instead for 
Did you mean: 

XLSX To XML Conversion Using Java Mapping

Former Member
0 Kudos

Hi

I can able to successfully convert XLSX file to XML using the below code in Eclipse.

import java.io.*;

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

import java.text.*;

public class XSLXReader {

static DataFormatter df = new DataFormatter();

public static void main(String[] args) {

FileWriter fostream;

PrintWriter out = null;

String strOutputPath = "C:\\Test";

String strFilePrefix = "ExcelToXML";

try {

InputStream inputStream = new FileInputStream(new File("C:\\Test\\Test1.xlsx"));

Workbook wb = WorkbookFactory.create(inputStream);

Sheet sheet = wb.getSheetAt(0);

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

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

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

out.println("<Record>");

boolean firstRow = true;

for (Row row : sheet) {

if (firstRow == true) {

firstRow = false;

continue;

}

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

out.println(formatElement("\t\t", "Employee_ID", df.formatCellValue(row.getCell(0)))); out.println(formatElement("\t\t", "Variable", df.formatCellValue(row.getCell(1)))); out.println(formatElement("\t\t", "Desc", df.formatCellValue(row.getCell(2)))); out.println(formatElement("\t\t", "Notes", df.formatCellValue(row.getCell(3)))); out.println("\t</Row>");

}

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

out.flush(); out.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

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();

}

}

Now, I want to use this code in Java mapping to convert XLSX to XML.

Can anyone please help me how to use this code in Java mapping.

I am using PO 7.5

Regards,

Raju.

Former Member
0 Kudos

Could anyone help me to frame the above logic in Java mapping.

Regards,

Raju.

manoj_khavatkopp
Active Contributor
0 Kudos

Please share a sample screen shot of your xlsx file to test and provide you the code.

Former Member
0 Kudos

Hi Manoj,

Please find the attached sample file screen shot.

sample-file.png

Thanks,

Raju.

Accepted Solutions (0)

Answers (0)