jxl 导出数据到excel的实例讲解
优点:
Jxl对中文支持非常好,操作简单,方法看名知意。
Jxl是纯javaAPI,在跨平台上表现的非常完美,代码可以再windows或者Linux上运行而无需重新编写
支持Excel95-2000的所有版本(网上说目前可以支持Excel2007了,还没有尝试过)
生成Excel2000标准格式
支持字体、数字、日期操作
能够修饰单元格属性
支持图像和图表,但是这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。
缺点:
效率低,图片支持不完善,对格式的支持不如POI强大
案例:
Stringtimes=(newSimpleDateFormat("yyyyMMddHHmmss")).format(newDate());
Stringfname="系统日志"+times;//文件名
Listlist=logsService.selectForList(hql.toString());
Stringpath=request.getSession().getServletContext().getRealPath("/")
+"xls/"
+(newSimpleDateFormat("yyyyMMdd")).format(newDate());
Filefile=newFile(path);
//如果文件夹不存在则创建
if(!file.exists()&&!file.isDirectory()){
file.mkdir();
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");////指定文件的保存类型。
response.setCharacterEncoding("utf-8");
ExportUtil.writer_log(request,fname,list,response);//下载到本地
writer_log导出方法如下
/**
*生成excel文件,导出到本地电脑
*@paramfname文件名
*@paramlist需要打印的数据,即数据库查询的数据列表
*/
publicstaticvoidwriter_log(HttpServletRequestrequest,Stringfname,Listlist,HttpServletResponseresponse){
try{
OutputStreamos=response.getOutputStream();//取得输出流
response.reset();//清空输出流
//下面是对中文文件名的处理开始
response.setCharacterEncoding("UTF-8");//设置相应内容的编码格式
if(isMsBrowser(request))
fname=java.net.URLEncoder.encode(fname,"UTF-8");
elsefname=newString(fname.getBytes("UTF-8"),"ISO-8859-1");
response.setHeader("Content-Disposition","attachment;filename="+fname+".xls");
response.setContentType("application/msexcel;charset=utf-8");//定义输出类型
//对中文文件名的处理结束
//此处的Workbook导入的是importjxl.Workbook;
WritableWorkbookwbook=Workbook.createWorkbook(os);//建立excel文件
WritableSheetsheet=wbook.createSheet("系统日志",0);//工作表名称
CellViewcellView=newCellView();
cellView.setAutosize(true);//设置自动大小
sheet.setColumnView(0,8);//设置单元格宽度,0是列号,8是宽度
sheet.setColumnView(1,20);//设置单元格宽度,1是列号,20是宽度
sheet.setColumnView(2,24);
sheet.setColumnView(3,20);
sheet.setColumnView(4,30);
sheet.setColumnView(5,13);
sheet.setColumnView(6,15);
sheet.setColumnView(7,32);
sheet.setColumnView(8,15);
//设置Excel字体
WritableFontwfont=newWritableFont(WritableFont.createFont("宋体"),22,
WritableFont.BOLD,false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);//设置单元格字体样式
WritableCellFormattitleFormat=newWritableCellFormat(wfont);//添加单元格字体
titleFormat.setAlignment(Alignment.CENTRE);//设置文字居中对齐方式;
String[]title={"系统日志"};
//设置Excel表头开始
for(inti=0;i
以上这篇jxl导出数据到excel的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。