gudii9
asked on
xls to xml conversion application
Hi,
I have a xls file with two sheets namely sheet1 and sheet2
sheet 1 having 3 columns say
1. product 2. total 3. cost
has about 100 records
sheet2 also having same 3 columns say
1. product 2. total 3. cost
has about 10 records.
I need to convert above xls to xml with similar structure as follows
<sheet1>
<product1>
<product></product>
<total></total>
<cost></cost>
</product1>
<product2>
<product></product>
<total></total>
<cost></cost>
</product2>
.....more products
</sheet1>
<sheet2>
<product1>
<product></product>
<total></total>
<cost></cost>
</product1>
<product2>
<product></product>
<total></total>
<cost></cost>
</product2>
.....more products
</sheet2>
what is the best way to achieve it. Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
I have a xls file with two sheets namely sheet1 and sheet2
sheet 1 having 3 columns say
1. product 2. total 3. cost
has about 100 records
sheet2 also having same 3 columns say
1. product 2. total 3. cost
has about 10 records.
I need to convert above xls to xml with similar structure as follows
<sheet1>
<product1>
<product></product>
<total></total>
<cost></cost>
</product1>
<product2>
<product></product>
<total></total>
<cost></cost>
</product2>
.....more products
</sheet1>
<sheet2>
<product1>
<product></product>
<total></total>
<cost></cost>
</product1>
<product2>
<product></product>
<total></total>
<cost></cost>
</product2>
.....more products
</sheet2>
what is the best way to achieve it. Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
I think the XML you have paste is not valid xml, since in your xml root element is different(sheet1 and sheet2). But XML should always have one root element and inside you can have multiple elements.
You can create something like this <sheet name="1"><product name="1">
You can create something like this <sheet name="1"><product name="1">
ASKER
>>>
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
I have multiple sheets in xls. How to deal with multiple sheets. How does xml looks like for multiple sheets. Also this example does not talk how to read multiple sheets in xls. Please advise
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
I have multiple sheets in xls. How to deal with multiple sheets. How does xml looks like for multiple sheets. Also this example does not talk how to read multiple sheets in xls. Please advise
you can iterate the below line to work with multiple sheets in one excel
Sheet sheet = workbook.getSheets()[0];
try with the below code:
Sheet sheet = workbook.getSheets()[0];
try with the below code:
int noOfSheet = 2;
try {
Workbook workbook = Workbook.getWorkbook(excelFile);
for(int i=0;i<noOfSheet;i++){
Sheet sheet = workbook.getSheets()[i];
for (int row = 0; row < sheet.getRows(); row++) {
for (int column = 0; column < sheet.getColumns(); column++) {
String content = sheet.getCell(column, row).getContents();
System.out.println("content>>>>"+content);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can refer the below post as well:
https://www.experts-exchange.com/questions/27876672/java-program-to-convert-to-xml.html
https://www.experts-exchange.com/questions/27876672/java-program-to-convert-to-xml.html
To read the Excel use the below code:
package xmlparser;
import java.io.File;
import jxl.Sheet;
import jxl.Workbook;
public class HelloWorld {
public static void main(String[] args) {
File excelFile = new File("Book1.xls");
if (excelFile.exists()) {
try {
Workbook workbook = Workbook.getWorkbook(excel
Sheet sheet = workbook.getSheets()[0];
for (int row = 0; row < sheet.getRows(); row++) {
for (int column = 0; column < sheet.getColumns(); column++) {
String content = sheet.getCell(column, row).getContents();
System.out.println("conten
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("File does not exist");
}
}
}
to write it xml you can refer the below URL:
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/