我正在尝试将excel文件直接转换为XML文件。我已经有了xsd模式文件和xml文件,我可以用它在excel中导入,然后将数据添加到这些字段并生成一个xml文件。
我正在执行此任务,以便我可以减少创建解析器以解析excel文件,从xml架构文件生成java对象以将数据写入它们并从这些对象生成xml文件的额外任务。基本上是为了避免编组和取消编组过程。使用的模式是一个包含3个其他模式文件的复杂模式。
答案 0 :(得分:0)
请参阅本教程here
该教程提供了此示例
try {
XmlGenner x = XMLGennerSingleton.getXmlGenner();
x.setCustomRows(1000);
x.setSubRootElementName("Student");
x.setProgressBar(jProgressBar1);
x.generateXML_Item("c:/temp/in.xls", "c:/temp/out.xml");
} catch (Exception ex) {
// handler
}
为输入xls生成xml文件。
答案 1 :(得分:0)
您是否能够为用户提供“特殊”Excel文件?
如果将Excel保存为XML(在Excel中),Windows仍将其识别为Excel文件,其行为类似于Excel文件,但它是XML,您可以轻松阅读和修改。
您只需要小心解析空单元格 - 它们将以XML格式跳过。
答案 2 :(得分:0)
如果您只想从java / groovy应用程序中读取和写入Excel文件,您可能希望使用jexel-api或Apache POI - 这两个项目已经解决了这个问题。
答案 3 :(得分:0)
使用以下代码将.xls或.xlsx文件转换为xml。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ImportDataFromXLStoXML {
public ImportDataFromXLStoXML() {
}
public void displayFromExcel(String xlsPath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(xlsPath);
} catch (FileNotFoundException e) {
System.out.println("File not found in the specified path.");
e.printStackTrace();
}
POIFSFileSystem fileSystem = null;
try {
// Initializing the XML document
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement = document.createElement("categories");
document.appendChild(rootElement);
fileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
Integer count = workBook.getNumberOfSheets();
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
for (int i = 0; i < count; i++) {
HSSFSheet sheet = workBook.getSheetAt(i);
Iterator<?> rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator<?> cells = row.cellIterator();
ArrayList<String> rowData = new ArrayList<String>();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
HSSFRichTextString richTextString = cell.getRichStringCellValue();
System.out.println("String: " + richTextString.getString());
rowData.add(richTextString.getString());
} // end while
data.add(rowData);
} // end while
}
int numOfProduct = data.size();
for (int i = 0; i < numOfProduct; i++) {
Element productElement = document.createElement("categoryName");
rootElement.appendChild(productElement);
int index = 0;
for (String s : data.get(i)) {
String headerString = data.get(0).get(index);
if (data.get(0).get(index).equals("image link")) {
headerString = "image_link";
}
if (data.get(0).get(index).equals("product type")) {
headerString = "product_type";
}
Element headerElement = document
.createElement(headerString);
productElement.appendChild(headerElement);
headerElement.appendChild(document.createTextNode(s));
index++;
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
// Add indentation to output
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("products.xml"));
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
} catch (ParserConfigurationException e) {
System.out
.println("ParserConfigurationException " + e.getMessage());
} catch (TransformerConfigurationException e) {
System.out.println("TransformerConfigurationException "
+ e.getMessage());
} catch (TransformerException e) {
System.out.println("TransformerException " + e.getMessage());
}
}
public static void main(String[] args) {
ImportDataFromXLStoXML poiExample = new ImportDataFromXLStoXML();
String xlsPath = "/home/vpanchal/installApps/workspace/QTOExport/output/Ptest.xls";
poiExample.displayFromExcel(xlsPath);
}
}