on 08-10-2017 5:29 PM
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.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.