使用jxl导出Excel文件,并且设置Excel样式

来源:互联网 发布:云计算hadoop实战视频 编辑:程序博客网 时间:2024/04/29 01:17

最近一段时间一直在做Excel导出,主要是年关将近,有各类统计数据需要。Java导出Excel有两个jar,一个是jxl.jar,另一个是poi.jar,此处选用的是jxl。

jxl导出

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.List;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class ExportExcel {public void export() {// 获取用户数据List<Account> list = accountService.list();if(list != null && !list.isEmpty()){try {// 获取开始时间long start = System.currentTimeMillis();String fileName = "用户信息.xls";// 文件夹路径File dirFile = new File(Constants.DOWNLOAD_PATH);if(!dirFile.exists()){dirFile.mkdirs();}// 文件路径File file = new File(Constants.DOWNLOAD_PATH + "\\" + fileName);if(!file.exists()){file.createNewFile();}OutputStream os = new FileOutputStream(file);// 创建Excel工作薄WritableWorkbook wwb = Workbook.createWorkbook(os);// 添加第一个工作表并设置第一个sheet 的名称WritableSheet sheet = wwb.createSheet("用户信息", 0);String[] title = {"账号", "中文名", "年龄", "注册方式", "创建日期"};Label label;for (int i = 0; i < title.length; i++) {// label(列, 行, 内容)label = new Label(i, 0, title[i]);// 将定义好的单元格添加到工作表中sheet.addCell(label);}// 填充数据int temp = 1;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");for (Account account : list) {// 账号if(account.getLoginCode() != null){label = new Label(0, temp, account.getLoginCode());}else{label = new Label(0, temp, "");}sheet.addCell(label);// 中文名if(account.getChinaName() != null){label = new Label(1, temp, account.getChinaName());}else{label = new Label(1, temp, "");}sheet.addCell(label);// 年龄if(account.getAge() != null){label = new Label(2, temp, account.getAge());}else{label = new Label(2, temp, "保密");}sheet.addCell(label);// 注册方式if(account.getCreateType() != null){if(account.getCreateType() == Constants.USER_CREATE_TYPE_REGISTER)label = new Label(3, temp, "注册用户");if(account.getCreateType() == Constants.USER_CREATE_TYPE_QQ)label = new Label(3, temp, "QQ注册");if(account.getCreateType() == Constants.USER_CREATE_TYPE_WEIXIN)label = new Label(3, temp, "微信注册");}else{label = new Label(3, temp, "");}sheet.addCell(label);// 创建日期if(account.getCreateTime() != null){label = new Label(4, temp, sdf.format(account.getCreateTime()));}else{label = new Label(4, temp, "");}sheet.addCell(label);temp ++;}// 写入文件wwb.write();// 关闭wwb.close();os.close();long end = System.currentTimeMillis();logger.debug("########## \t 完成" + fileName + "导出共用的时间是:" + (end - start)/1000);Struts2Utils.getResponse().getWriter().print(request.getContextPath() + Constants.DOWNLOAD_DIR + "\\" + fileName);} catch (Exception e) {e.printStackTrace();}}}}

备注:

Account:用户实体

Constants:常量类

Constants.DOWNLOAD_PATH = "D:\apache-tomcat-6.0.36\webapps\项目名\download"   // 文件目录路径

Constants.DOWNLOAD_DIR = “\download”  //项目文件夹(下载时会用到)

项目中使用,参数最好配置在.properties文件中。

jxl样式与其他

设置列宽、单元格合并、使用Excel函数、添加超链接、添加图片

// 设置单元格高、宽sheet.setRowView(0, 20);sheet.setColumnView(0, 10);// 合并单元格(开始列, 开始行, 结束列, 结束行)sheet.mergeCells(0, 0, 2, 0);// 使用EXcel 函数jxl.write.Formulaformula = new jxl.write.Formula(0, 2, "SUM(A1:A2)");sheet.addCell(formula);formula = new jxl.write.Formula(0, 3, "A1/A2");sheet.addCell(formula);formula = new jxl.write.Formula(0, 4, "A1");sheet.addCell(formula);//添加超链接类单元格(开始列, 开始行, 最后一列激活这个链接, 最后一行激活这个链接, URL(必须加上协议,如 Http://), 说明 )WritableHyperlink wrlink = new WritableHyperlink(0,1,0,1,new URL("http://www.baidu.com/"), "百度一下");sheet.addHyperlink(wrlink);//添加图像(开始列, 开始行, 图片跨越的列数, 图片跨域的行数, 图片路径或者字节流)String imageFilepath = "D:\\work\\img.png";WritableImage wrimage=new WritableImage(1,5,10,10,new File(imageFilepath));sheet.addImage(wrimage);

设置样式

// 编写文字样式WritableFont font = new WritableFont(  WritableFont.createFont("宋体"),// 字体                    20,// 字号                    WritableFont.NO_BOLD,// 加粗样式                  false,  // 斜体                  UnderlineStyle.NO_UNDERLINE,        // 下划线样式                  Colour.RED,// 字体颜色                    ScriptStyle.NORMAL_SCRIPT// 脚本风格                  );// 编写单元格格式WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT);try {<span style="white-space:pre"></span>// 添加字体设置  <span style="white-space:pre"></span>headerFormat.setFont(font);<span style="white-space:pre"></span>// 设置单元格背景色:表头为黄色headerFormat.setBackground(Colour.YELLOW);//设置边框样式为粗线、黑色headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.BLACK);//表头内容水平居中显示  headerFormat.setAlignment(Alignment.CENTRE);//表头内容垂直居中显示 headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);} catch (WriteException e) {<span style="white-space:pre"></span>System.out.println("单元格样式设置失败!");  }// 将样式加入到单元格中label = new Label(3, 1, "loginCode", headerFormat);// 将单元格添加到工作表sheet.addCell(label);








0 0
原创粉丝点击