我想用JSP来操作Excel。在网上找到了相关资料。也下载了 poi-src-2.5.1-final-20040804.zip。但是不知道怎样用。我的环境是 Tomcat 5 ,JDK1.5.0_05  ,Win2k AS

解决方案 »

  1.   

    这是JAVA的例子,改成JSP不难吧:
    mport org.apache.poi.hssf.usermodel.*;
    import java.io.*;
    import java.util.*;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem; 
    public class exceltest {

    public static void writeexcel() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    // 建立新HSSFWorkbook对象
    HSSFSheet sheet = wb.createSheet("new sheet");
    // 建立新的sheet对象 0 开始
    HSSFRow row = sheet.createRow((short)0);
    // 建立新行
    HSSFCell cell = row.createCell((short)0);
    // 建立新cell
    cell.setCellValue(3.3);//设置cell的整数类型的值
    row.createCell((short)1).setCellValue(1.2);
    // 设置cell浮点类型的值
    row.createCell((short)2).setCellValue("test");
    // 设置cell字符类型的值
    row.createCell((short)3).setCellValue(true);
    // 设置cell布尔类型的值 
    HSSFCellStyle cellStyle = wb.createCellStyle();
    // 建立新的cell样式
    cellStyle.setDataFormat
    (HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    // 设置cell样式为定制的日期格式
    HSSFCell dCell =row.createCell((short)4);
    dCell.setCellValue(new Date());
    // 设置cell为日期类型的值
    dCell.setCellStyle(cellStyle); 
    // 设置该cell日期的显示格式
    HSSFCell csCell =row.createCell((short)5);
    csCell.setEncoding(HSSFCell.ENCODING_UTF_16);
    // 设置cell编码解决中文高位字节截断
    csCell.setCellValue("中文测试_Chinese Words Test");
    // 设置中西文结合字符串
    row.createCell((short)6).setCellType
    (HSSFCell.CELL_TYPE_ERROR);
    // 建立错误cell
    FileOutputStream fileOut = 
    new FileOutputStream("d:/workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    System.out.println("hahaha");
    }
        public static void readexcel() {
            POIFSFileSystem fs = null; 
          HSSFWorkbook wb = null; 
          try { 
              fs = new POIFSFileSystem(new FileInputStream("d:/workbook.xls"));
              wb = new HSSFWorkbook(fs); 
              } catch (IOException e) { 
              e.printStackTrace(); 
              } 
              HSSFSheet sheet = wb.getSheetAt(0); 
              HSSFRow row = sheet.getRow(0); 
              HSSFCell cell = row.getCell((short)0); 
              double msg  = cell.getNumericCellValue();
              System.out.println(msg);
        //      String msg1  = cell.getCellFormula();
        //      System.out.println(msg1);
        
        }
        
    public static void main(String[] args) throws IOException {
    writeexcel();
           System.out.println("Write OK");
            readexcel();
            System.out.println("Read OK");
            
    }
      

  2.   

    我要实现的功能是:JSP做的网页列出数据库的数据.完后将指定的数据写到 指定的 Excel文件的指定的单元格里.
      

  3.   

    JXL,我一直拿它来做Excel报表。
    设置格式,公式都比较方便。
    去sourceforge.net下载一个jxl吧,它自带的example看看就差不多了。
      

  4.   

    把上面的代码封装成一个JavaBean去实现就可以了。