我下面的代码将excel文件读入java代码,然后通过创建适当的标记解析为XML文档。我遇到的问题是空单元格。 想象一下,有几列填充数据的列和行,如果单元格为空,则它会搞砸XML文档,因为在这种情况下它会插入下一个非空的单元格的值,因此结果不会是正确的。有关代码编辑的任何建议吗?
public class ExcelXMLParser {
public ExcelXMLParser () {
}
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("records");
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 ();
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
// NUMERIC CELL TYPE
//System.out.println ("Numeric: " + Integer.toString((int) cell.getNumericCellValue ()));
rowData.add(Integer.toString((int) 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 numOfData = data.size();
for (int i = 1; i < numOfData; i++){
Element dataElement = document.createElement("data");
rootElement.appendChild(dataElement);
int index = 0;
for(String s: data.get(i)) {
String headerString = data.get(0).get(index);
Element headerElement = document.createElement(headerString);
dataElement.appendChild(headerElement);
System.out.println(headerElement);
System.out.println(s);
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");
// Define source
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("c:\\Users\\Temped\\Desktop\\dataXML.xml"));
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)
{
ExcelXMLParser generateOutput = new ExcelXMLParser ();
String xlsPath ="C:\\Users\\Temped\\Desktop\\dataXLS.xls";
generateOutput.displayFromExcel (xlsPath);
}
}
答案 0 :(得分:0)
修复了使用地图的问题。在LinkedHashMap的帮助下创建Map列表。