如题·~ 在网上找了几个都用不成 希望各位大侠们 能把代码贴出来 最好有注释的~~ 不甚感激~~ 在线等

解决方案 »

  1.   

    用POI就可以了,
    不过建议直接导出csv文件,用excel打开就可以了,这样效率会高。
      

  2.   

    1.下载poi的包包// createXls(存放路径如:(c:\test.xls),要导出的东西,excel的头一行)
    2.public void createXls(String filePath,List<对象> phList,String[] headers) throws Exception {

    // 建工作薄
    HSSFWorkbook wb = new HSSFWorkbook(); // 建名为example的工作表
    HSSFSheet sheet = wb.createSheet("测试");
    System.out.println("abc");
    // 设置表格默认列宽度为15个字节
    sheet.setDefaultColumnWidth(15);
    // 产生表格表头
    HSSFRow headerrow = sheet.createRow(0);
    for (int i = 0; i < headers.length; i++) {
    HSSFCell cell = headerrow.createCell(i);
    HSSFRichTextString text = new HSSFRichTextString(headers[i]);
    cell.setCellValue(text);
    }
    Iterator<对象> it = (Iterator<对象>) phList.iterator();
    int index = 0;
    while (it.hasNext()) {
    index++;
    HSSFRow datarow = sheet.createRow(index);
    Phone a =  it.next();
    HSSFCell data1 = datarow.createCell(0);
    HSSFRichTextString richString1 = new HSSFRichTextString(a.get属性());
    data1.setCellValue(richString1);
    }
    FileOutputStream fileout = new FileOutputStream(filePath);
    wb.write(fileout);
    fileout.close();
    }
      

  3.   

    http://www.java2000.net/p1681
      

  4.   

    用jxl吧   一个类包  网上关于它的说明应该很多 但是这个东西比较耗资源 
      

  5.   

    需要一个jxl.jar;代码如下package com.webapp.officialdocument.filing.action;import java.io.File;
    import java.io.OutputStream;
    import java.util.List;
    import java.util.Map;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;import com.webapp.app.util.DateUtils;
    import com.webapp.core.common.domain.PageInfo;
    import com.webapp.officialdocument.filing.domain.Filing;
    import com.webapp.officialdocument.filing.iservice.IFilingService;
    import com.webapp.struct.module.action.EmpowermentAction;public class FilingExcelAction extends EmpowermentAction
    {
    private IFilingService service;
    public void setService(IFilingService service) {
    this.service = service;
    }
    public void execute(HttpServletRequest req, HttpServletResponse resp,
    Map views) throws Exception 
    {
    String fileName = "filing";
    String zbmc = fileName;
    OutputStream os = resp.getOutputStream();
    resp.setContentType("application/ms-excel;charset=utf-8");
    resp.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ".xls");

    Map search = (Map) views.get("search");

    long old = (long)(System.currentTimeMillis()/86400000);
    if(search.get("filingdate2").toString().equals(old+""))
    {
    search.put("filingdate2", System.currentTimeMillis());
    }
    PageInfo pageInfo = (PageInfo) views.get("pageInfo");
    List list = service.list(search, pageInfo);
    {   
            try {   
                WritableWorkbook book = Workbook.createWorkbook(os);
                WritableSheet sheet = book.createSheet("Sheet_1", 0);   
                Label label = new Label(0, 0, "档案编号");
                Label label1 = new Label(1, 0, "公文编号");
                Label label2 = new Label(2, 0, "公文标题");
                Label label3 = new Label(3, 0, " 公文分类");
                Label label4 = new Label(4, 0, "归档时间");
                Label label5 = new Label(5, 0, "保存时间");
                Label label6 = new Label(6, 0, "归档姓名");
                Label label7 = new Label(7, 0, "公文作者");
                Label label8 = new Label(8, 0, "档案分类");
                sheet.addCell(label);
                sheet.addCell(label1);
                sheet.addCell(label2);
                sheet.addCell(label3);
                sheet.addCell(label4);
                sheet.addCell(label5);
                sheet.addCell(label6);
                sheet.addCell(label7);
                sheet.addCell(label8);
            catch (Exception e) 
            {   
                e.printStackTrace();   
            }  
            
            
    }
    }}
      

  6.   

    10楼写得很详细啦,其实JXL跟POI的写法都差不多,看自己习惯或喜欢用哪个了
      

  7.   

    如果数据是从数据库取得,oracle建议用ociuldr导出,sqlserver用BCP导出,使用数据库工具导出,速度会非常快