java生成excel并下载(jxl)
2015-10-23 09:55
393 查看
/**
*该方法需导入jxl相关jar包,实现了页面触发、java生成excel并弹出下载框选择下载路径的进行保存的功能
*
*
*/
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.sq.common.model.TBorrowRepay;
import com.sq.constant.SqAccountConstants;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/*
* 生成访问记录Excel表格工具类
*/
public class ExpExcelUtil {
private static WritableCellFormat wcf_value;
// 表格数据样式
private static WritableCellFormat wcf_value_left;
private static WritableCellFormat wcf_key;
// 表头样式
private static WritableCellFormat wcf_name_left;
// 表名样式
private static WritableCellFormat wcf_name_right;
// 表名样式
private static WritableCellFormat wcf_name_center;
// 表名样式
private static WritableCellFormat wcf_title;
// 页名称样式
private static WritableCellFormat wcf_percent_float;
// 生成还款计划Excel文件
public void genarateBorrowRepayExcel(List<TBorrowRepay> list, String fileName,HttpServletResponse response) throws Exception{
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String((fileName).getBytes("GB2312"),"iso8859-1"));
OutputStream os = response.getOutputStream();
/****** 定义表格格式start *****/
WritableFont wf_key = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
WritableFont wf_value = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
//设置单元格样式
wcf_value = new WritableCellFormat(wf_value);
//单元格字体样式
wcf_value.setAlignment(jxl.format.Alignment.CENTRE);
//单元格水平对齐样式
wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
//单元格垂直对齐样式
wcf_value_left = new WritableCellFormat(wf_value);
wcf_value_left.setAlignment(jxl.format.Alignment.LEFT);
wcf_value_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_value_left.setWrap(true);
wcf_key = new WritableCellFormat(wf_key);
wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
wcf_key.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
wcf_name_left = new WritableCellFormat(wf_key);
wcf_name_left.setAlignment(Alignment.LEFT);
wcf_name_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_name_right = new WritableCellFormat(wf_key);
wcf_name_right.setAlignment(Alignment.LEFT);
wcf_name_center = new WritableCellFormat(wf_key);
wcf_name_center.setAlignment(Alignment.CENTRE);
jxl.write.NumberFormat wf_percent_float = new jxl.write.NumberFormat("0.00");
//定义单元浮点数据类型
wcf_percent_float = new jxl.write.WritableCellFormat(wf_value,wf_percent_float);
wcf_percent_float.setAlignment(jxl.format.Alignment.CENTRE);
wcf_percent_float.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_percent_float.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
WritableFont wf_title = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.NO_BOLD);
//定义标题样式
wcf_title = new WritableCellFormat(wf_title);
wcf_title.setAlignment(Alignment.CENTRE);
wcf_title.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
/****** 定义表格格式end *****/
//根据传输过来的list,定义excel表格的列数
try{
WritableWorkbook wb = Workbook.createWorkbook(os);
//设置Excel工作簿名称
WritableSheet ws = wb.createSheet("还款计划", 0);
int startRowNum = 0;
// 起始行
int startColNum = 0;
// 起始列
int maxColSize = 18;
// 最大列数
// 设置列宽
ws.setColumnView(0, 10);
ws.setColumnView(1, 18);
ws.setColumnView(2, 12);
ws.setColumnView(3, 12);
ws.setColumnView(4, 12);
ws.setColumnView(5, 28);
ws.setColumnView(6, 28);
ws.setColumnView(7, 15);
ws.setColumnView(8, 15);
ws.setColumnView(9, 15);
ws.setColumnView(10, 15);
ws.setColumnView(11, 15);
ws.setColumnView(12, 15);
ws.setColumnView(13, 15);
ws.setColumnView(14, 15);
ws.setColumnView(15, 15);
ws.setColumnView(16, 15);
ws.setColumnView(17, 15);
ws.addCell(new Label(startColNum, startRowNum, "还款计划", wcf_title));
ws.mergeCells(startColNum, startRowNum, startColNum + maxColSize- 1, startRowNum);
//合并单元格,合并(1,0)到(1,9)
startColNum = 0;
startRowNum++;
//第1行,绘制表头
ws.addCell(new Label(startColNum, startRowNum, "借款ID",wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "借款人名称", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "应还款日期", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "实际还款日期", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "应还利息", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "应还本金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "实还利息", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "实还本金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "提前还款违约金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "还款状态", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "回款状态", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "当前期数", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "总期数", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "罚金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "逾期天数", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "催收费", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "平台垫付金额", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "平台垫付时间", wcf_key));
startColNum++;
//将行数加1,列数重置为0
startRowNum++;
startColNum = 0;
//添加记录
for(TBorrowRepay ar :list){
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getBorrowId()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getLoginName()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, DateTimeUtils.DateToString(ar.getRepaymentTime(), "YYYY/MM/dd") ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getRepaymentActualTime()==null?"":DateTimeUtils.DateToString(ar.getRepaymentActualTime(), "YYYY/MM/dd") ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getInterest().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getCapital().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getInterestActual().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getCapitalActual().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getPenaltyMoney().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(SqAccountConstants.getNameByRepayStatusCode(ar.getRepayStatus())) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(SqAccountConstants.getNameByRepayReceiveStatusCode(ar.getReceiveStatus())) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getSortOrder()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getTotal()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getExpiredMoney().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getExpiredDays()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getCallFee().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getSubstituteMoney().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getSubstituteTime() ==null?"":DateTimeUtils.DateToString(ar.getSubstituteTime(),"YYYY/MM/dd") ,wcf_key));
startColNum++;
//将行数加1,列数重置为0
startRowNum++;
startColNum = 0;
}
wb.write();
//生成Excel工作簿
wb.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
*该方法需导入jxl相关jar包,实现了页面触发、java生成excel并弹出下载框选择下载路径的进行保存的功能
*
*
*/
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.sq.common.model.TBorrowRepay;
import com.sq.constant.SqAccountConstants;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/*
* 生成访问记录Excel表格工具类
*/
public class ExpExcelUtil {
private static WritableCellFormat wcf_value;
// 表格数据样式
private static WritableCellFormat wcf_value_left;
private static WritableCellFormat wcf_key;
// 表头样式
private static WritableCellFormat wcf_name_left;
// 表名样式
private static WritableCellFormat wcf_name_right;
// 表名样式
private static WritableCellFormat wcf_name_center;
// 表名样式
private static WritableCellFormat wcf_title;
// 页名称样式
private static WritableCellFormat wcf_percent_float;
// 生成还款计划Excel文件
public void genarateBorrowRepayExcel(List<TBorrowRepay> list, String fileName,HttpServletResponse response) throws Exception{
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String((fileName).getBytes("GB2312"),"iso8859-1"));
OutputStream os = response.getOutputStream();
/****** 定义表格格式start *****/
WritableFont wf_key = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
WritableFont wf_value = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
//设置单元格样式
wcf_value = new WritableCellFormat(wf_value);
//单元格字体样式
wcf_value.setAlignment(jxl.format.Alignment.CENTRE);
//单元格水平对齐样式
wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
//单元格垂直对齐样式
wcf_value_left = new WritableCellFormat(wf_value);
wcf_value_left.setAlignment(jxl.format.Alignment.LEFT);
wcf_value_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_value_left.setWrap(true);
wcf_key = new WritableCellFormat(wf_key);
wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
wcf_key.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
wcf_name_left = new WritableCellFormat(wf_key);
wcf_name_left.setAlignment(Alignment.LEFT);
wcf_name_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_name_right = new WritableCellFormat(wf_key);
wcf_name_right.setAlignment(Alignment.LEFT);
wcf_name_center = new WritableCellFormat(wf_key);
wcf_name_center.setAlignment(Alignment.CENTRE);
jxl.write.NumberFormat wf_percent_float = new jxl.write.NumberFormat("0.00");
//定义单元浮点数据类型
wcf_percent_float = new jxl.write.WritableCellFormat(wf_value,wf_percent_float);
wcf_percent_float.setAlignment(jxl.format.Alignment.CENTRE);
wcf_percent_float.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_percent_float.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
WritableFont wf_title = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.NO_BOLD);
//定义标题样式
wcf_title = new WritableCellFormat(wf_title);
wcf_title.setAlignment(Alignment.CENTRE);
wcf_title.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
/****** 定义表格格式end *****/
//根据传输过来的list,定义excel表格的列数
try{
WritableWorkbook wb = Workbook.createWorkbook(os);
//设置Excel工作簿名称
WritableSheet ws = wb.createSheet("还款计划", 0);
int startRowNum = 0;
// 起始行
int startColNum = 0;
// 起始列
int maxColSize = 18;
// 最大列数
// 设置列宽
ws.setColumnView(0, 10);
ws.setColumnView(1, 18);
ws.setColumnView(2, 12);
ws.setColumnView(3, 12);
ws.setColumnView(4, 12);
ws.setColumnView(5, 28);
ws.setColumnView(6, 28);
ws.setColumnView(7, 15);
ws.setColumnView(8, 15);
ws.setColumnView(9, 15);
ws.setColumnView(10, 15);
ws.setColumnView(11, 15);
ws.setColumnView(12, 15);
ws.setColumnView(13, 15);
ws.setColumnView(14, 15);
ws.setColumnView(15, 15);
ws.setColumnView(16, 15);
ws.setColumnView(17, 15);
ws.addCell(new Label(startColNum, startRowNum, "还款计划", wcf_title));
ws.mergeCells(startColNum, startRowNum, startColNum + maxColSize- 1, startRowNum);
//合并单元格,合并(1,0)到(1,9)
startColNum = 0;
startRowNum++;
//第1行,绘制表头
ws.addCell(new Label(startColNum, startRowNum, "借款ID",wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "借款人名称", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "应还款日期", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "实际还款日期", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "应还利息", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "应还本金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "实还利息", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "实还本金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "提前还款违约金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "还款状态", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "回款状态", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "当前期数", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "总期数", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "罚金", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "逾期天数", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "催收费", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "平台垫付金额", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "平台垫付时间", wcf_key));
startColNum++;
//将行数加1,列数重置为0
startRowNum++;
startColNum = 0;
//添加记录
for(TBorrowRepay ar :list){
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getBorrowId()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getLoginName()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, DateTimeUtils.DateToString(ar.getRepaymentTime(), "YYYY/MM/dd") ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getRepaymentActualTime()==null?"":DateTimeUtils.DateToString(ar.getRepaymentActualTime(), "YYYY/MM/dd") ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getInterest().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getCapital().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getInterestActual().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getCapitalActual().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getPenaltyMoney().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(SqAccountConstants.getNameByRepayStatusCode(ar.getRepayStatus())) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(SqAccountConstants.getNameByRepayReceiveStatusCode(ar.getReceiveStatus())) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getSortOrder()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getTotal()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getExpiredMoney().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getExpiredDays()) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getCallFee().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getSubstituteMoney().setScale(2, BigDecimal.ROUND_HALF_UP)) ,wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getSubstituteTime() ==null?"":DateTimeUtils.DateToString(ar.getSubstituteTime(),"YYYY/MM/dd") ,wcf_key));
startColNum++;
//将行数加1,列数重置为0
startRowNum++;
startColNum = 0;
}
wb.write();
//生成Excel工作簿
wb.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
相关文章推荐
- java 移位运算符
- interface Java 中接口
- Java web.xml session-config 属性配置
- Eclipse swt开发环境搭建
- JVM的相关知识整理和学习
- Java基础增强3-反射,内省,beanutils,泛型
- java内存分配和String类型的深度解析
- Spring Boot应用之数据加密以及字段过滤
- 深度优先实现拓扑排序--java
- 在Servlet中获取Spring的指定bean
- java keytool证书工具使用小结
- SpringMVC+MyBatis+EasyUI 实现分页查询
- eclipse项目没错但有红叉
- JAVA初始化过程
- 完成这个例子,说出java中针对异常的处理机制。
- java高精度开平方
- Java基本数据类型
- java开源微博系统weibo4j分享
- 使用JavaMail碰到的几个问题
- eclipse中AndroidA工程依赖B工程设置