jsp输出excel文件【转载】

介绍:

Jakarta_POI 使用Java读写Excel(97-2002)文件,可以满足大部分的需要。文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

因为刚好有一个项目使用到了这个工具,花了点时间顺便翻译了一下POI本身文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

带的一个Guide.有一些节减和修改,希望给使用这个项目的人一些入门帮助。文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

POI 下面有几个自项目:HSSF用来实现Excel 的读写.以下是HSSF的主页文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

http://jakarta.apache.org/poi/hssf/index.html文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

下面的介绍是基于以下地址的翻译:文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

http://jakarta.apache.org/poi/hssf/quick-guide.html文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

目前的版本为1.51应该是很长时间之内的一个稳定版,但HSSF提供的Sample不是基于文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

1.51所写,所以使用的时候需要适当的注意.文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

其实POI下面的几个子项目侧重不同读写 Word 的HDF正在开发当中.文章源自堕落的鱼-https://www.duoluodeyu.com/49.html

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;

创建workbook

HSSFWorkbook wb = new HSSFWorkbook();

//使用默认的构造方法创建workbook

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

//指定文件名

wb.write(fileOut);

//输出到文件

fileOut.close();

创建一个sheet

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet1 = wb.createSheet("new sheet");

//workbook创建sheet

HSSFSheet sheet2 = wb.createSheet("second sheet");

//workbook创建另外的sheet

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

创建cells

HSSFWorkbook 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 file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

创建日期cells

HSSFWorkbook 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 background

HSSFCellStyle 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);

 最后更新:2010-2-17
如何获得Manus邀请码及体验资格 实用信息

如何获得Manus邀请码及体验资格

Manus,作为领先的人工智能平台,已经吸引了大量用户的关注。如果你也希望体验Manus的创新技术,下面将介绍几种获取Manus邀请码及内测资格的途径。通过这些途径,你有机会成为Manus平台的早期用...
100条组织生活会批评与自我批评意见 实用信息

100条组织生活会批评与自我批评意见

在工作压力较大时,情绪管理能力不足,容易将负面情绪传递给同事,破坏团队的和谐氛围,要提升情绪管控能力,保持积极的心态。 对于工作中的一些风险和隐患,缺乏敏锐的洞察力和预判能力,未能提前采取有效的防范措...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证