//jxl.jar导入导出Excel表格
//一.在SSH中导入生Excel表格的依赖包Jxl.jar
//二.在service层创建接口IExcelService
package com.crms.service;
import java.io.InputStream;
import java.util.List;
/**
* struts2导出Excel
* @author Administrator
*
*/
public interface IExcelService {
public InputStream getExcelInputStream(List<Object[]> objList);
}
//三.创建接口的实现类
package com.crms.service.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import com.crms.service.IExcelService;
/**
* 写入Excel文件
*
* 用來生成一个Excel文件
*
* @author Administrator
*
*/
public class ExcelServiceImpl implements IExcelService {
// 定义request ,response.
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
/**
* 将OutputStream转化为InputStream
*/
public InputStream getExcelInputStream(List<Object[]> dataList) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
putDataOnOutputStream(out,dataList);
return new ByteArrayInputStream(out.toByteArray());
}
/**
* 生成数据表(Excel)
*/
private void putDataOnOutputStream(OutputStream os,List<Object[]> dataList) {
//构造Workbook(工作薄)对象
WritableWorkbook workbook=null;
//创建工作表
WritableSheet ws=null;
try {
//首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
workbook = Workbook.createWorkbook(os);
/*
* 创建一个可写入的工作表
* Workbook的createSheet方法有两个参数,
* 第一个是工作表的名称,第二个是工作表在工作薄中的位置
* 生成名为"第一页"的工作表,参数0表示这是第一页
*/
ws= workbook.createSheet("offerUp", 0);
//创建列名
// 定义格式, 字体, 下划线, 斜体, 粗体, 颜色
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
// 创建格式化对象实例
WritableCellFormat totalx2Format = new WritableCellFormat(wf);
// 垂直居中
totalx2Format.setVerticalAlignment(VerticalAlignment.CENTRE);
// 水平居中
totalx2Format.setAlignment(Alignment.CENTRE);
// 设置标题.ws.addCell(new jxl.write.Label(列(从0开始), 行(从1开始), 内容.));
ws.addCell(new Label(0, 0, "编号",totalx2Format));
ws.addCell(new Label(1, 0, "客户名称",totalx2Format));
ws.addCell(new Label(2, 0, "订单金额(元)",totalx2Format));
// 合并单元格,参数格式(开始列,开始行,结束列,结束行)
ws.mergeCells(0, 0, 0, 0);
ws.mergeCells(1, 0, 1, 0);
ws.mergeCells(2, 0, 2, 0);
// 设置单元格的宽度
ws.setColumnView(0, 10);
ws.setColumnView(1, 40);
ws.setColumnView(2, 20);
//添加数据
if (null!=workbook) {
//下面开始添加单元格数据
for(int i=0;i<dataList.size();i++){
for(int j=0;j<dataList.get(0).length;j++){
//这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
Label labelC = new Label(j, i+1,dataList.get(i)[j].toString(),totalx2Format);
try {
//将生成的单元格添加到工作表中
ws.addCell(labelC);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
}
//从内存中写入文件中
workbook.write();
//关闭资源,释放内存
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
四.Action层将数据库的数据传给Excel,并返回结果
package com.crms.web.action;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.crms.biz.StatisticsBiz;
import com.crms.pojo.Customerinfo;
import com.crms.pojo.msms.Orderdetail;
import com.crms.pojo.msms.Orders;
import com.crms.service.IExcelService;
import com.crms.service.impl.ExcelServiceImpl;
/**
* struts2导出Excel
* @author Administrator
*
*/
public class ExcelAction {
private StatisticsBiz statisticsBiz;
private List<Orderdetail> orderdetailList;
private Customerinfo customerinfo;
private ExcelServiceImpl excelServiceImpl=new ExcelServiceImpl();
public StatisticsBiz getStatisticsBiz() {
return statisticsBiz;
}
public void setStatisticsBiz(StatisticsBiz statisticsBiz) {
this.statisticsBiz = statisticsBiz;
}
public List<Orderdetail> getOrderdetailList() {
return orderdetailList;
}
public void setOrderdetailList(List<Orderdetail> orderdetailList) {
this.orderdetailList = orderdetailList;
}
public IExcelService getExcelService() {
return excelService;
} public void setExcelService(IExcelService excelService) {
this.excelService = excelService;
}
public ExcelServiceImpl getExcelServiceImpl() {
return excelServiceImpl;
}
public void setExcelServiceImpl(ExcelServiceImpl excelServiceImpl) {
this.excelServiceImpl = excelServiceImpl;
}
//+++++++++++++++++++++++++++
private InputStream excelStream;
private IExcelService excelService = new ExcelServiceImpl();
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
public String execute() {
String customerId = null;
List<Object[]> objectList = new ArrayList<Object[]>();
try {
orderdetailList = statisticsBiz.getOrderdetailService().queryAll();
Integer i = 0;
for (Orderdetail orderdetail_2 : orderdetailList) {
customerId = ((Orders) (orderdetail_2.getOrders())).getCustomer_id();
customerinfo = statisticsBiz.getCustomerinfoService().queryById(customerId);
((Orders) (orderdetailList.get(i).getOrders())).setCustomer_id(customerinfo.getCustomerName());
Object [] object={i+1,customerinfo.getCustomerName(),orderdetail_2.getSub_price()};
objectList.add(object);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
excelStream = excelService.getExcelInputStream(objectList);
return "excel";
}
}
//五.Struts2配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts2导出Excel -->
<struts>
<package name="excel" namespace="/export2excel" extends="struts-default">
<action name="excel" class="com.crms.web.action.ExcelAction">
<result name="excel_*" type="stream">
<param name="contentType">
application/vnd.ms-excel
</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">
filename="{1}.xls"
</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
分享到:
相关推荐
jxl.jar是通过java操作excel表格的工具类库,开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的。
jxl.jar是通过java操作excel表格的工具类库,是由java语言开发而成的。通过jxl可以把数据导入到excel。例子在另外一个资源里面
通过java来操作Excel表格的工具类库。 支持字体、数字、日期操作,能够修饰单元格属性,能支持图像和图表,可以正确的处理Excel文件。 附使用方法。
源程序不需做任何修改,只要将jar包导入到项目中就可以运行了。
Android数据转化为Excel表格导入导出,利用jxl.jar [注:本内容来自网络,在此分享仅为帮助有需要的网友,如果侵犯了您的权利,麻烦联系我,我会第一时间删除,谢谢您。]
Android数据转化为Excel表格导入导出,利用jxl.jar
Android数据转化Excel表格导入导出,利用jxl.jar,当然还能转化为word什么的
利用jxl来实现excel表格的导入和导出,包含源码和所需要的jar包
jxl jxl包 poi技术导出数据至excel中 poi-bin-2.5.1包 jxl对excel表格 代码操作 poi技术 java代码从数据库取数据导入至Excel表中 poi-bin-2.5.1-final-20040804.jar
本文实例讲述了Android开发实现的导出数据库到Excel表格功能。分享给大家供大家参考,具体如下: 之前一直在电脑上用Excel表格记录家庭帐单,不久前重装系统不小心干掉了,伤心了好久,那可是我记了五年的帐单呀!这...
使用jxl需要在Android工程中导入jxl.jar包,jxl可以完成Excel的基本读写操作,其支持与不支持的情况如下: 1、jxl只支持Excel2003格式,不支持Excel2007格式。即支持xls文件,不支持xlsx文件。 2、jxl不支持直接...