Skip to Content
0

XLSX To XML Conversion Using Java Mapping

Aug 10, 2017 at 04:29 PM

149

avatar image
Former Member

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.

10 |10000 characters needed characters left characters exceeded
Former Member

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

Regards,

Raju.

0

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

0
Former Member
Manoj K

Hi Manoj,

Please find the attached sample file screen shot.

sample-file.png

Thanks,

Raju.

sample-file.png (8.7 kB)
0
* Please Login or Register to Answer, Follow or Comment.

0 Answers