兄弟,使用POI吧!下载地址:
http://apache.linuxforum.net/dist/jakarta/poi/release/bin/poi-bin-2.5.1-final-20040804.zip内面有许多的例子!

解决方案 »

  1.   

    其实POI下面的几个子项目侧重不同读写 Word 的HDF正在开发当中.XML下的FOP(http://xml.apache.org/fop/index.html)可以输出pdf文件,也是比较好的一个工具目录:创建一个workbook创建一个sheet创建cells创建日期cells设定单元格格式说明:以下可能需要使用到如下的类import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;创建workbookHSSFWorkbook wb = new HSSFWorkbook();//使用默认的构造方法创建workbookFileOutputStream fileOut = new FileOutputStream("workbook.xls");//指定文件名wb.write(fileOut);//输出到文件fileOut.close();创建一个sheetHSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet1 = wb.createSheet("new sheet");//workbook创建sheetHSSFSheet sheet2 = wb.createSheet("second sheet");//workbook创建另外的sheetFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();创建cellsHSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");//注意以下的代码很多方法的参数是short 而不是int 所以需要做一次类型转换HSSFRow row = sheet.createRow((short)0);//sheet 创建一行HSSFCell cell = row.createCell((short)0);//行创建一个单元格cell.setCellValue(1);//设定单元格的值//值的类型参数有多中double ,String ,boolean,row.createCell((short)1).setCellValue(1.2);row.createCell((short)2).setCellValue("This is a string");row.createCell((short)3).setCellValue(true);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();创建日期cellsHSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");HSSFRow row = sheet.createRow((short)0);HSSFCell cell = row.createCell((short)0);//设定值为日期cell.setCellValue(new Date());HSSFCellStyle cellStyle = wb.createCellStyle();//指定日期显示格式cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));cell = row.createCell((short)1);cell.setCellValue(new Date());//设定单元格日期显示格式cell.setCellStyle(cellStyle);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();设定单元格格式单元格格式的设定有很多形式包括单元格的对齐方式,内容的字体设置,单元格的背景色等,因为形式比较多,只举一些例子.以下的例子在POI1.5中可能会有所改变具体查看API...........// Aqua backgroundHSSFCellStyle style = wb.createCellStyle();//创建一个样式style.setFillBackgroundColor(HSSFCellStyle.AQUA);//设定此样式的的背景颜色填充style.setFillPattern(HSSFCellStyle.BIG_SPOTS);//样式的填充类型。//有多种式样如://HSSFCellStyle.BIG_SPOTS//HSSFCellStyle.FINE_DOTS//HSSFCellStyle.SPARSE_DOTS等style.setAlignment(HSSFCellStyle.ALIGN_CENTER );//居中对齐style.setFillBackgroundColor(HSSFColor.GREEN.index);//设定单元个背景颜色style.setFillForegroundColor(HSSFColor.RED.index);//设置单元格显示颜色HSSFCell cell = row.createCell((short) 1);cell.setCellValue("X");cell.setCellStyle(style);