public static void writeExcel(OutputStream os) throws Exception {
    try{
      jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
      jxl.write.WritableSheet ws = wwb.createSheet("testSheet1", 0);
      jxl.write.Label labelC = new jxl.write.Label(0, 0, "我爱中国1");
      ws.addCell(labelC);
      jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.
          ARIAL,
          20, WritableFont.BOLD, false,
          UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);
      jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
      wcfFC.setBackground(jxl.format.Colour.RED);
      labelC = new jxl.write.Label(6, 0, "中国爱我", wcfFC);
      ws.addCell(labelC);
      wwb.write();
      wwb.close();
    }
    catch(Exception e){
      System.out.println(e.toString());    }
  }  public static void main(String[] args)  {
    try{
      File f = new File("kk.xls");
      f.createNewFile();
      writeExcel(new FileOutputStream(f));
    }
    catch(Exception e){
      System.out.println(e.toString());
    }
}

解决方案 »

  1.   

    //生成Excel的类 
    import java.io.*; 
    import jxl.*; 
    import jxl.write.*; public class CreateXLS 

    public static void main(String args[]) 

    try 

    //打开文件 
    WritableWorkbook book= 
    Workbook.createWorkbook(new File(“测试.xls”)); //生成名为“第一页”的工作表,参数0表示这是第一页 
    WritableSheet sheet=book.createSheet(“第一页”,0); //在Label对象的构造子中指名单元格位置是第一列第一行(0,0) 
    //以及单元格内容为test 
    Label label=new Label(0,0,”test”); //将定义好的单元格添加到工作表中 
    sheet.addCell(label); /*生成一个保存数字的单元格 
    必须使用Number的完整包路径,否则有语法歧义 
    单元格位置是第二列,第一行,值为789.123*/ 
    jxl.write.Number number = new jxl.write.Number(1,0,789.123); 
    sheet.addCell(number); //写入数据并关闭文件 
    book.write(); 
    book.close(); }catch(Exception e) 

    System.out.println(e); 


      

  2.   

    谢谢这位仁兄,但是我不想简单弄个代码,我想知道通过什么技术来解析xml和操作excel。
      

  3.   

    jxl的类包用来对excel表格操作。它是一个open source,下载和资料都能搜到。
    java解析XML的包有jdom,jaxp等。你可以上sun的网站查找jaxp,这个应该是最新的xml解析工具。
      

  4.   


    首先自己封装一个JDOM的类,如取名UniXML.
    import java.io.*;
    import java.util.*;import  org.jdom.*;
    import  org.jdom.input.*;
    import  org.jdom.output.*;import javax.xml.transform.*;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;public class  UniXML
    {
      private static String strRootTag = "xml";
      public Document unidoc = null;
      public Element rootElement = null;  public String Query(String strTags) {
        Element tmpElement = GetElementByTags(strTags);
        return tmpElement.getTextTrim();
      }  public Element GetElementByTags(String strTags) {
        Element rootElem = unidoc.getRootElement();
        int i = 0;
        Element elemEnd = null;
        StringTokenizer TokTags = new StringTokenizer(strTags, "/");
        while (TokTags.hasMoreTokens()) {
          i++;
          String strTok = TokTags.nextElement().toString();
          if (i == 1)
            elemEnd = rootElem.getChild(strTok);
          else
            elemEnd = elemEnd.getChild(strTok);
        }    return elemEnd;
      }
    ......
    }然后再定义如下:
    UniXML unixml= new UniXML("你的XML字串")
    String act = unixml.Query("map/act");
    String cmd = unixml.Query("map/cmd");
    ......
      

  5.   

    你可以先读出放到一个数组里面在写到xlc里面
      

  6.   

    你是不是在读完excel文件后还要在保存到xml中啊
      

  7.   

    其实就是一个XMLDocumentAdapt,做了一个适配器.个人认为:上面老兄使用JXL并不太好,poi可能会更好一些(个人感觉).XML采用jdom解析就行了,非常简单.
      

  8.   

    crazyboy2003(疯狂男孩) poi 的文档有吗???