jxl应用事例
实例中主要目的是解析jxl使用流程以及jxl绘制Excel的写法思路,代码掩去了项目中的真实数据,请根据需求酌情修改,如果有帮助到有需要的人,不胜欢喜。
Dao层为查询数据库,返回list数据,此处省略。
==============service
public void exportExcel(HttpServletResponse response){
try{
//列坐标
int columIndex[] = {0,1,2,3,4,5,6,};
//每一列宽度
int colwidth[] = {10,10,10,10,10,10};
//每一列字段key
String[] dataKey = {"test1","test2","test3","test4","test5","test6"};
//列表数据
List<Map<String,Object>> dataList = tableTestDao.findTableData();
//合计每一列的key
String[] countKey = {"test1","test2","test3","test4","test5","test6"}; //导出execl
tableExcelService.exportExcel( FILE_NAME, response, "测试表格", columIndex, colwidth, dataKey, dataList, countKey, "three" );
}catch (Exception e) {
throw new BusinessException(e, ExceptionEnum.NULL, e.getMessage());
}
}
=================TableExcelService工具类方法(1)
/**
* 导出excel
* @param fileName文件名
* @param response
* @param title标题名
* @param columIndex列坐标
* @param colwidth每一列宽度
* @param dataKey每一列字段key
* @param dataList列表数据
* @throws Exception
*/
public void exportExcel(String fileName,HttpServletResponse response,String title,
int[] columIndex,int[] colwidth,String[] dataKey,List<Map<String,Object>> dataList,
String falg ) throws Exception{
OutputStream os=null;
WritableWorkbook workbook=null;
try{
os=response.getOutputStream();
fileName=URLEncoder.encode(fileName,"GB2312");
fileName=URLDecoder.decode(fileName, "ISO8859_1");
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
response.setContentType("application/msexcel");
workbook = Workbook.createWorkbook(os);
// 自定义的颜色
Color color = Color.decode("#CCFFCC");
workbook.setColourRGB(Colour.GREEN, color.getRed(),color.getGreen(), color.getBlue());
//创建excel第一个sheet页
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
//创建标题和表头
if("three".equals( falg )||"four".equals( falg )){
sheet = createTableHead(sheet,title,columIndex,colwidth);
}else if("one".equals( falg )){
sheet = createTableHeadOne(sheet,title,columIndex,colwidth);
}else if("two".equals( falg )){
sheet = createTableHeadTwo(sheet,title,columIndex,colwidth);
}
//表格样式
WritableCellFormat tdWcfF = new WritableCellFormat();
tdWcfF.setBorder(Border.ALL, BorderLineStyle.THIN);
tdWcfF.setAlignment(Alignment.CENTRE);
tdWcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
//填入列表数据
int startRow = 4;//从第五行开始填入值
Map<String,Object> dataMap = null;
Object obj=null;
for(int row = 0;row <dataList.size();row++){
dataMap = dataList.get( row );
for(int col=0;col<dataKey.length;col++){
obj = dataMap.get(dataKey[col]);
sheet.addCell(new Label(col,row+startRow,obj==null?"0":obj.toString(),tdWcfF));
}
}
//合计
if("three".equals( falg )||"four".equals( falg )){
int numStartRow = startRow+dataList.size();//到第几行了
sheet.mergeCells(0, numStartRow, 1, numStartRow);
sheet.addCell(new Label(0,numStartRow,"测试",tdWcfF));
for(int col=0;col<countKey.length;col++){
obj = countMap.get(countKey[col]);
sheet.addCell(new Label(col+2,numStartRow,obj==null?"0":obj.toString(),tdWcfF));
}
}else if("one".equals( falg )){
int numStartRow = startRow+dataList.size();//到第几行了
sheet.mergeCells(0, numStartRow, 0, numStartRow);
sheet.addCell(new Label(0,numStartRow,"测试",tdWcfF));
for(int col=0;col<countKey.length;col++){
obj = countMap.get(countKey[col]);
sheet.addCell(new Label(col+1,numStartRow,obj==null?"0":obj.toString(),tdWcfF));
}
}else if("two".equals( falg )){
int numStartRow = startRow+dataList.size();//到第几行了
sheet.mergeCells(0, numStartRow, 1, numStartRow);
sheet.addCell(new Label(0,numStartRow,"测试",tdWcfF));
for(int col=0;col<countKey.length;col++){
obj = countMap.get(countKey[col]);
sheet.addCell(new Label(col+2,numStartRow,obj==null?"0":obj.toString(),tdWcfF));
}
} workbook.write();
}catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);
}finally{
if(workbook != null) {
workbook.close();
workbook=null;
}
if(os != null) {
os.flush();
os.close();
os=null;
}
}
}
===========TableExcelService工具类方法(2)
/**
* 创建标题和表头
* @param sheet
* @param title
* @return
* @throws Exception
*/
private WritableSheet createTableHead(WritableSheet sheet,String title,
int[] columIndex,int[] colwidth) throws Exception{
/*****格式设置******/
//标题
WritableCellFormat titleWcfF = new WritableCellFormat();
//边线细线
titleWcfF.setBorder(Border.ALL, BorderLineStyle.THIN);
//水平居中
titleWcfF.setAlignment(Alignment.CENTRE);
//垂直居中
titleWcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
//设置显示的字体样式,字体,字号,是否粗体,字体颜色
titleWcfF.setFont(new WritableFont(WritableFont.createFont("楷体_GB2312"),14,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.BLACK));
//表头
WritableCellFormat thWcfF = new WritableCellFormat();
thWcfF.setBorder(Border.ALL, BorderLineStyle.THIN);
thWcfF.setAlignment(Alignment.CENTRE);
thWcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
thWcfF.setBackground(jxl.format.Colour.GREEN);
//创建标题,列,行,到第19列,到第0行
sheet.mergeCells(0, 0, 19, 0);
sheet.addCell(new Label(0,0,title,titleWcfF));
//表头创建
sheet.mergeCells(0, 1, 1, 3);//名称
sheet.addCell(new Label(0,1,"测试1",thWcfF));
//业务数(单位:个)列,行,到第19列,到第1行
sheet.mergeCells(2, 1, 19, 1);
sheet.addCell(new Label(2,1,"测试2",thWcfF));
sheet.mergeCells(2, 2, 10, 2);
sheet.addCell(new Label(2,2,"测试3",thWcfF));
sheet.mergeCells(11, 2, 19, 2);
sheet.addCell(new Label(11,2,"测试4",thWcfF));
sheet.mergeCells(2, 3, 3, 3);
sheet.addCell(new Label(2, 3,"测试5",thWcfF));
sheet.mergeCells(4, 3, 5, 3);
sheet.addCell(new Label(4, 3,"测试6",thWcfF));
//单个单元格表头第6列,第3行,单元格内容,单元格样式
sheet.addCell(new Label(6, 3,"测试7",thWcfF));
sheet.addCell(new Label(7, 3,"测试8",thWcfF));
sheet.addCell(new Label(8, 3,"测试9",thWcfF));
sheet.addCell(new Label(9, 3,"测试10",thWcfF));
sheet.addCell(new Label(10, 3,"测试11",thWcfF));
sheet.mergeCells(11, 3, 12, 3); //设置列宽
for(int i=0;i<columIndex.length;i++){
sheet.setColumnView(columIndex[i], colwidth[i]);
}
return sheet;
}
jxl应用事例的更多相关文章
- java的jxl技术导入Excel
项目结构: http://www.cnblogs.com/hongten/gallery/image/112177.html 在项目中我们看到Reference Libraries中的jxl.jar包 ...
- jxl导入导出实例
1 package com.tgb.test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.Ar ...
- jxl读取Excel表格数据
调用jxl包实现Excel表格数据的读取,代码如下: import java.io.File; import java.io.IOException; import java.util.ArrayLi ...
- 通过jxl 读取excel 文件中的日期,并计算时间间隔
java读取excel里面的日期会出现相差8小时的问题. 比如excel里面有一个日期是:2012-7-2 17:14:03秒,用Cell cell=readSheet.getCell(colNo, ...
- java中使用jxl导出Excel表格详细通用步骤
该方法一般接收两个参数,response和要导出的表格内容的list. 一般我们将数据库的数据查询出来在页面进行展示,根据用户需求,可能需要对页面数据进行导出. 此时只要将展示之前查询所得的数据放入s ...
- jxl读数据库数据生成xls 并下载
1.所需jar jxl-2.6.10.jar jxls-core-1.0-RC-3.jar jxls-reader-1.0-RC-3.jar 2. excel修改行宽度封装 SheetColumn.j ...
- JXL操作Excel
jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文 ...
- OAF_文件系列11_实现OAF读写Excel包JXL和POI的区别(概念)
20150803 Created By BaoXinjian
- OAF_文件系列10_实现OAF将数据资料导出Excel到本地JXL(案例)
20150729 Created By BaoXinjian
随机推荐
- Leetcode 88. Merge Sorted Array(easy)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- Neutron flat network 学习
flat network 是不带 tag 的网络,要求宿主机的物理网卡直接与 linux bridge 连接,这意味着: 每个 flat network 都会独占一个物理网卡. 在 ML2 配置中 ...
- Object的所有方法
Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. const object1 = { a: 1, b: 2, c: 3 }; const ...
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- Shell命令-文件及内容处理之cut、rev
文件及内容处理 - cut.rev 1. cut:切割文件内容 cut命令的功能说明 cut 命令用于显示每行从开头算起num1 到 num2 的文字. cut命令的语法格式 cut [OPTION] ...
- 带拦截器配置的 struts.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- 舵机&数据处理&stm32内存之堆栈溢出(遇到的问题)
产品名称:TOWER PRO(辉盛)大扭力舵机MG996R (MG995升级产品)6v/11Kg厂家编号:MG996R产品净重: 55g产品尺寸: 40.7*19.7*42.9mm产品拉力: 9.4k ...
- php支持大文件上传
打开php.ini找到 upload_max_filesize . memory_limit . post_max_size 这三个参数! upload_max_filesize = 2G 是上传最大 ...
- Cucumber常用关键字
常用关键字(中英文对应) 对应的测试用例 Feature(功能) test suite (测试用例集) background(背景) Scenario(场景) test case(测试用例) Sc ...
- Netty 源码分析
https://segmentfault.com/a/1190000007282628 netty社区-简书闪电侠 :https://netty.io/wiki/related-articles.ht ...