gpt4 book ai didi

java - 使用java将excel数据解析为xml

转载 作者:行者123 更新时间:2023-12-01 18:39:57 26 4
gpt4 key购买 nike

我有下面的代码,它读取 Excel 文件并在 java 中显示它。我想在将excel文件中的数据读取到java后实现代码,它将转换为XML格式并将其保存在XML文件中。

任何代码示例或引用将不胜感激;

public class POIExcelReader {

public POIExcelReader (){
}

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 {
fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<?> rows = sheet.rowIterator ();

while (rows.hasNext ())
{
HSSFRow row = (HSSFRow) rows.next();

// display row number
System.out.println ("Row No.: " + row.getRowNum ());

// get a row, iterate through cells.
Iterator<?> cells = row.cellIterator ();

while (cells.hasNext ())
{
HSSFCell cell = (HSSFCell) cells.next ();
//System.out.println ("Cell : " + cell.getCellNum ());
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
// NUMERIC CELL TYPE
System.out.println ("Numeric: " + cell.getNumericCellValue ());
break;
}
case HSSFCell.CELL_TYPE_STRING :

{
// STRING CELL TYPE
HSSFRichTextString richTextString = cell.getRichStringCellValue ();

System.out.println ("String: " + richTextString.getString ());
break;
}
default:
{
// types other than String and Numeric.
System.out.println ("Type not supported.");
break;
}
}
}
}
}
catch(IOException e)
{
e.printStackTrace ();
}
}


public static void main (String[] args)
{
POIExcelReader poiExample = new POIExcelReader ();
String xlsPath ="c://Users//Secured//Desktop//artikli.xls";
poiExample.displayFromExcel (xlsPath);
}
}

最佳答案

您可以使用包中的类:javax.xml.parsers。该包提供了允许处理 XML 文档的类。例如DocumentBuilder、DocumentBuilderFactory 等等。

使用下面的java代码(扩展了您的原始代码)和位于此位置的文件:http://base.google.com/base/products.xls

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 POIExcelReader {

public POIExcelReader (){
}

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("products");
document.appendChild(rootElement);


fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<?> rows = sheet.rowIterator ();

ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
while (rows.hasNext ())
{
HSSFRow row = (HSSFRow) rows.next();

int rowNumber = row.getRowNum ();
// display row number
System.out.println ("Row No.: " + rowNumber);

// get a row, iterate through cells.
Iterator<?> cells = row.cellIterator ();

ArrayList<String> rowData = new ArrayList<String>();
while (cells.hasNext ())
{
HSSFCell cell = (HSSFCell) cells.next ();
//System.out.println ("Cell : " + cell.getCellNum ());
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
// NUMERIC CELL TYPE
System.out.println ("Numeric: " + cell.getNumericCellValue ());
rowData.add(cell.getNumericCellValue () + "");
break;
}
case HSSFCell.CELL_TYPE_STRING :

{
// STRING CELL TYPE
HSSFRichTextString richTextString = cell.getRichStringCellValue ();

System.out.println ("String: " + richTextString.getString ());
rowData.add(richTextString.getString ());
break;
}
default:
{
// types other than String and Numeric.
System.out.println ("Type not supported.");
break;
}
} // end switch

} // end while
data.add(rowData);


} //end while

int numOfProduct = data.size();

for (int i = 1; i < numOfProduct; i++){
Element productElement = document.createElement("product");
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)
{
POIExcelReader poiExample = new POIExcelReader ();
String xlsPath ="products.xls";
poiExample.displayFromExcel (xlsPath);
}
}

关于java - 使用java将excel数据解析为xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com