使用jxl导出数据到excel中
2017-05-27 16:15
387 查看
1.
function exportDataToExcel(){
var formData = $("#user_auth_form").formSerialize();
location.href="${ctx}/userAuth/ExportDataToExcel.do?"+formData
}
2.
@RequestMapping("/ExportDataToExcel.do")
public void ExportDataToExcel(UserQuery queryObject,OutputStream outputStream,HttpServletResponse response) throws Exception{
Map<String,Object> model = new HashMap<String, Object>();
try{
queryObject.setUsePage(false);
List<UserVO> userAuthedList = userAuthService.queryUserPassRecs(queryObject);
//清空输出流
response.reset();
//设置对中文的处理
response.setCharacterEncoding("UTF-8");
//设置文件名称
String date = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
String fileName = URLEncoder.encode("已认证用户统计"+date,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("UTF-8"),"UTF-8")+".xls");
//定义输出类型
response.setContentType("application/msexcel");
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
//创建工作表
WritableSheet sheet = workbook.createSheet("已认证用户统计",0);
sheet.setRowView(0, 400);//设置第一行的高度,值越小,高度越小
sheet.setColumnView(0,15);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(1,30);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(2,15);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(3,30);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(4,30);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(5,20);//设置第一列的宽度,值越大,宽度越小
//设置标题单元格样式
WritableFont titleCellStyle = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
//字体、字体大小、无下划线、粗体、字体颜色
WritableFont titleCellStyle2 = new WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); //字体、字体大小、无下划线、粗体、字体颜色
//单元格定义
WritableCellFormat titleCellFormat = new WritableCellFormat(titleCellStyle);
WritableCellFormat titleCellFormat2 = new WritableCellFormat(titleCellStyle2);
//设置对齐方式
titleCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
titleCellFormat2.setAlignment(jxl.format.Alignment.CENTRE);
//定义内容单元格
WritableCellFormat contentCellFormat = new WritableCellFormat();
//设置对齐方式
contentCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
//创建excel标题
String[] dataTitle = {"认证账户","会员名称","认证类型","认证时间","所在地"};
for(int i=0;i<userAuthedList.size();i++){
if(i<1){
for(int j=0;j<5;j++){
Label label = new Label(j,0,dataTitle[j],titleCellFormat);
sheet.addCell(label);
}
}
//获取DB数据
UserVO userVO = use
4000
rAuthedList.get(i);
//认证账户
Label accountName = new Label(0,i+1,userVO.getAccountName(),contentCellFormat);
sheet.addCell(accountName);
//会员名称
if(userVO.getUserType()==DataConstants.DRIVER || userVO.getUserType()== DataConstants.P_GOODS_OWNER){
Label nickName = new Label(1,i+1,userVO.getNickName());
sheet.addCell(nickName);
}else{
if(userVO.getNickName() !=null && userVO.getEnterpriseName() !=null){
Label nickName = new Label(1,i+1,userVO.getNickName()+"("+userVO.getEnterpriseName()+")");
sheet.addCell(nickName);
}else if(userVO.getNickName() !=null && userVO.getEnterpriseName() ==null){
Label nickName = new Label(1,i+1,userVO.getNickName());
sheet.addCell(nickName);
}else{
Label nickName = new Label(1,i+1,userVO.getEnterpriseName());
sheet.addCell(nickName);
}
}
//认证类型
if(userVO.getUserType()==DataConstants.DRIVER){
Label userType = new Label(2,i+1,"司机认证",titleCellFormat2);
sheet.addCell(userType);
}else if(userVO.getUserType()==DataConstants.P_GOODS_OWNER){
Label userType = new Label(2,i+1,"个人货主认证",contentCellFormat);
sheet.addCell(userType);
}else{
Label userType = new Label(2,i+1,"企业认证",contentCellFormat);
sheet.addCell(userType);
}
//认证时间
Label authTime = new Label(3,i+1,new SimpleDateFormat("yyyy-MM-dd HH: mm").format(userVO.getAuthTime()),contentCellFormat);
//所在地
Label address = new Label(4,i+1,userVO.getAddress(),titleCellFormat2);
sheet.addCell(authTime);
sheet.addCell(address);
}
workbook.write();
workbook.close();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
throw new Exception("导出数据出错!");
}
}
function exportDataToExcel(){
var formData = $("#user_auth_form").formSerialize();
location.href="${ctx}/userAuth/ExportDataToExcel.do?"+formData
}
2.
@RequestMapping("/ExportDataToExcel.do")
public void ExportDataToExcel(UserQuery queryObject,OutputStream outputStream,HttpServletResponse response) throws Exception{
Map<String,Object> model = new HashMap<String, Object>();
try{
queryObject.setUsePage(false);
List<UserVO> userAuthedList = userAuthService.queryUserPassRecs(queryObject);
//清空输出流
response.reset();
//设置对中文的处理
response.setCharacterEncoding("UTF-8");
//设置文件名称
String date = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
String fileName = URLEncoder.encode("已认证用户统计"+date,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("UTF-8"),"UTF-8")+".xls");
//定义输出类型
response.setContentType("application/msexcel");
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
//创建工作表
WritableSheet sheet = workbook.createSheet("已认证用户统计",0);
sheet.setRowView(0, 400);//设置第一行的高度,值越小,高度越小
sheet.setColumnView(0,15);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(1,30);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(2,15);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(3,30);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(4,30);//设置第一列的宽度,值越大,宽度越小
sheet.setColumnView(5,20);//设置第一列的宽度,值越大,宽度越小
//设置标题单元格样式
WritableFont titleCellStyle = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
//字体、字体大小、无下划线、粗体、字体颜色
WritableFont titleCellStyle2 = new WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); //字体、字体大小、无下划线、粗体、字体颜色
//单元格定义
WritableCellFormat titleCellFormat = new WritableCellFormat(titleCellStyle);
WritableCellFormat titleCellFormat2 = new WritableCellFormat(titleCellStyle2);
//设置对齐方式
titleCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
titleCellFormat2.setAlignment(jxl.format.Alignment.CENTRE);
//定义内容单元格
WritableCellFormat contentCellFormat = new WritableCellFormat();
//设置对齐方式
contentCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
//创建excel标题
String[] dataTitle = {"认证账户","会员名称","认证类型","认证时间","所在地"};
for(int i=0;i<userAuthedList.size();i++){
if(i<1){
for(int j=0;j<5;j++){
Label label = new Label(j,0,dataTitle[j],titleCellFormat);
sheet.addCell(label);
}
}
//获取DB数据
UserVO userVO = use
4000
rAuthedList.get(i);
//认证账户
Label accountName = new Label(0,i+1,userVO.getAccountName(),contentCellFormat);
sheet.addCell(accountName);
//会员名称
if(userVO.getUserType()==DataConstants.DRIVER || userVO.getUserType()== DataConstants.P_GOODS_OWNER){
Label nickName = new Label(1,i+1,userVO.getNickName());
sheet.addCell(nickName);
}else{
if(userVO.getNickName() !=null && userVO.getEnterpriseName() !=null){
Label nickName = new Label(1,i+1,userVO.getNickName()+"("+userVO.getEnterpriseName()+")");
sheet.addCell(nickName);
}else if(userVO.getNickName() !=null && userVO.getEnterpriseName() ==null){
Label nickName = new Label(1,i+1,userVO.getNickName());
sheet.addCell(nickName);
}else{
Label nickName = new Label(1,i+1,userVO.getEnterpriseName());
sheet.addCell(nickName);
}
}
//认证类型
if(userVO.getUserType()==DataConstants.DRIVER){
Label userType = new Label(2,i+1,"司机认证",titleCellFormat2);
sheet.addCell(userType);
}else if(userVO.getUserType()==DataConstants.P_GOODS_OWNER){
Label userType = new Label(2,i+1,"个人货主认证",contentCellFormat);
sheet.addCell(userType);
}else{
Label userType = new Label(2,i+1,"企业认证",contentCellFormat);
sheet.addCell(userType);
}
//认证时间
Label authTime = new Label(3,i+1,new SimpleDateFormat("yyyy-MM-dd HH: mm").format(userVO.getAuthTime()),contentCellFormat);
//所在地
Label address = new Label(4,i+1,userVO.getAddress(),titleCellFormat2);
sheet.addCell(authTime);
sheet.addCell(address);
}
workbook.write();
workbook.close();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
throw new Exception("导出数据出错!");
}
}
相关文章推荐
- Java 使用jxl.jar 导入导出Excel数据
- ZK框架简介,以及在ZK下使用JXL读取Excel模板,导出数据模型到Excel的方法
- 使用jxl读取模板文件,写入数据并导出excel
- java中使用JXL对Excel文件进行数据的写入、导出操作
- 使用jxl导出大数据量EXCEL时内存溢出的解决办法
- 使用jxl导出大数据量EXCEL时内存溢出的解决办法
- java中使用JXL对Excel文件进行数据的写入、导出操作
- sl3中的DataGrid中的数据导出到Excel中 使用csv格式 解决中文是乱码的问题
- SQL SERVER使用OpenRowset,、OpenDataSource函数导入、导出数据到Excel 的几种方法(整理)
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 用Jxl实现将统计数据导出到excel表中
- Java解释Excel数据(jxl.jar包的使用)
- java中jxl导出数据到excel的例子
- DataGrid使用----导出到Excel数据表
- 使用GridView控件导出数据源的数据为Excel、Word或Text
- java中jxl导出数据到excel的例子
- 使用jxl导出excel时怎么创建多个sheet
- 使用XML&XSL替代OWC进行数据导出为EXCEL
- 使用HTML,CSS快速导出数据到Excel(转)
- 使用CLR存储过程方便快捷导出数据到Excel 【转】