您的位置:首页 > Web前端 > CSS

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

2016-01-26 15:26 633 查看
最近一段时间一直在做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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: