使用jxl读取Excel表格,再将读取的内容转化为XMl文件,遇到了两个问题;
第一个是:当遇到Excel表格中有空格的时候就无法转化,如果某一行中有一个空单元格,那么这一整行都无法转换成xml,在xml文件中就没有这一行。例如:Excel表中是这样的形式:
姓名 性别 年龄   
张三 男 23
李四 女 34
charlie 男
Lily 33

那么转换成xml文件如下:
<?xml version="1.0" encoding="GB2312"?>
<Table>
  <Rows>
    <姓名>姓名</姓名>
    <性别>性别</性别>
    <年龄>年龄</年龄>
  </Rows>
  <Rows>
    <姓名>张三</姓名>
    <性别>男</性别>
    <年龄>23</年龄>
  </Rows>
  <Rows>
    <姓名>李四</姓名>
    <性别>女</性别>
    <年龄>34</年龄>
  </Rows>
</Table>
也就是后两行没有转化,并提示错误:java.lang.ArrayIndexOutOfBoundsException: 2
at ReadAndWrite.main(ReadAndWrite.java:48)
第二个问题:当某一个单元格中的字符有空格时,比如"年龄"如果内容是"年龄   " 也会出错,错误如下:
org.jdom.IllegalNameException: The name "年龄    " is not legal for JDOM/XML elements: XML names cannot contain the character " ".
at org.jdom.Element.setName(Element.java:207)
at org.jdom.Element.<init>(Element.java:141)
at org.jdom.Element.<init>(Element.java:153)
at ReadAndWrite.main(ReadAndWrite.java:56)这是怎么回事呢??
代码如下:
import java.io.*;import jxl.*;import org.jdom.*;public class ReadAndWrite {
 public static void main(String[] args){
 Element rootElement = new Element("Table");
     Document myDocument = new Document(rootElement);
   
    try{
     InputStream sourceFile = new FileInputStream("C:/work/Test.xls");
     Workbook NewFile = Workbook.getWorkbook(sourceFile);
     //获得Sheet 1
     Sheet sheet = NewFile.getSheet(0);
     
     String sheetName = sheet.getName();
     System.out.println(sheetName);   
     
     int colnum = sheet.getColumns();
     System.out.print(colnum+"列");   
     
     int rownum = sheet.getRows();
     System.out.println(rownum+"行");   
          String[] colsName = new String[colnum];
     for(int col=0; col<colnum; col++) {
      Cell cell = sheet.getCell(col,0);
      
      colsName[col] = cell.getContents();  
     }
     
    
     
     for(int row=0; row<rownum; row++) {
      Element rowElement = new Element("Rows");
      Cell[] cells = sheet.getRow(row);
      if(cells !=null && cells.length>0) {
      for(int col=0; col<colnum; col++) {
    
    
      String cellCon = cells[col].getContents();
         if(cellCon == null)  cellCon = "null";    
      cellCon = cellCon.replaceAll("\\s+", ",");
     cellCon = cellCon.replaceAll(" ", "");
 cellCon = cellCon.replace("", "null");
    
      
      Element cellElement = new Element(colsName[col]).addContent(cellCon);
      rowElement.addContent(cellElement);
     }
      }
      rootElement.addContent(rowElement);
     }
      
  }catch(Exception ex){
     ex.printStackTrace();
    }
    try {
     
         org.jdom.output.Format format = org.jdom.output.Format.getPrettyFormat();
         
         org.jdom.output.Format ft = format.setEncoding("GB2312");//setIndent("  ");
  
         XMLOutputter output = new XMLOutputter(ft);
         
         OutputStream os = new FileOutputStream(new File("c:/work/Test.xml"));
    
        
         output.output(myDocument,os);
         
         
         os.close();
         
     } catch (IOException IllegalNameException) {
      IllegalNameException.printStackTrace();
     }
   }
}