public void exportExcel(String title, Collection<T> dataset,
OutputStream out) {
// 声明一个工作薄
try {
//首先检查数据看是否是正确的
Iterator<T> its = dataset.iterator();
if(dataset==null||!its.hasNext()||title==null||out==null)
{
throw new Exception("传入的数据不对!");
}
//取得实际泛型类
T ts = (T) its.next();
Class tCls = ts.getClass();
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置标题样式
style = ExcelStyle.setHeadStyle(workbook, style);
// 得到所有字段
Field filed[] = ts.getClass().getDeclaredFields();
// 标题
List<String> exportfieldtile = new ArrayList<String>();
// 导出的字段的get方法
List<Method> methodObj = new ArrayList<Method>();
// 遍历整个filed
for (int i = 0; i < filed.length; i++) {
Field f = filed[i];
ExcelAnnotation exa = f.getAnnotation(ExcelAnnotation.class);
// 如果设置了annottion
if (exa != null) {
String exprot = exa.exportName();
// 添加到标题
exportfieldtile.add(exprot);
// 添加到需要导出的字段的方法
String fieldname = f.getName();
String getMethodName = "get"
+ fieldname.substring(0, 1).toUpperCase()
+ fieldname.substring(1);
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
methodObj.add(getMethod);
}
}
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < exportfieldtile.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(
exportfieldtile.get(i));
cell.setCellValue(text);
}
int index = 0;
// 循环整个集合
while (its.hasNext()) {
//从第二行开始写,第一行是标题
index++;
row = sheet.createRow(index);
T t = (T) its.next();
for (int k = 0; k < methodObj.size(); k++) {
HSSFCell cell = row.createCell(k);
Method getMethod=methodObj.get(k);
Object value = getMethod.invoke(t, new Object[] {});
String textValue = getValue(value);
cell.setCellValue(textValue);
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
数据库数据:
1 kk a [email protected] M 2010-10-24 16:30:28
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59导出数据:
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59
OutputStream out) {
// 声明一个工作薄
try {
//首先检查数据看是否是正确的
Iterator<T> its = dataset.iterator();
if(dataset==null||!its.hasNext()||title==null||out==null)
{
throw new Exception("传入的数据不对!");
}
//取得实际泛型类
T ts = (T) its.next();
Class tCls = ts.getClass();
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置标题样式
style = ExcelStyle.setHeadStyle(workbook, style);
// 得到所有字段
Field filed[] = ts.getClass().getDeclaredFields();
// 标题
List<String> exportfieldtile = new ArrayList<String>();
// 导出的字段的get方法
List<Method> methodObj = new ArrayList<Method>();
// 遍历整个filed
for (int i = 0; i < filed.length; i++) {
Field f = filed[i];
ExcelAnnotation exa = f.getAnnotation(ExcelAnnotation.class);
// 如果设置了annottion
if (exa != null) {
String exprot = exa.exportName();
// 添加到标题
exportfieldtile.add(exprot);
// 添加到需要导出的字段的方法
String fieldname = f.getName();
String getMethodName = "get"
+ fieldname.substring(0, 1).toUpperCase()
+ fieldname.substring(1);
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
methodObj.add(getMethod);
}
}
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < exportfieldtile.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(
exportfieldtile.get(i));
cell.setCellValue(text);
}
int index = 0;
// 循环整个集合
while (its.hasNext()) {
//从第二行开始写,第一行是标题
index++;
row = sheet.createRow(index);
T t = (T) its.next();
for (int k = 0; k < methodObj.size(); k++) {
HSSFCell cell = row.createCell(k);
Method getMethod=methodObj.get(k);
Object value = getMethod.invoke(t, new Object[] {});
String textValue = getValue(value);
cell.setCellValue(textValue);
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
数据库数据:
1 kk a [email protected] M 2010-10-24 16:30:28
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59导出数据:
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59
解决方案 »
- 请教:java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost/tes
- struts和sprint整合的简单问题
- javascript 中的frame 同步问题
- 如何通过js得到文件的扩展名
- HQL问题,应该非常简单,解决马上给分
- 请问如何通过java,将已有的word文件转成pdf文档。。。
- 求助:如何通过jsp实现服务器端的文件及目录远程浏览?
- 大侠们谁知道亿阳信通这个公司怎么样啊
- 当一个文本框为空时,post出一个什么值?<NULL> OR ""
- 【求助】关于<s:select/>
- @Aspect 的异常,求助
- 怎么把js里面的多个值传个servlet
private void exportExcel(ActionContext ctx) throws Exception{
HttpServletRequest request=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
String[] titles=(String[])request.getAttribute("titles");
List dataList=(List)request.getAttribute("shopList");
workbook=new HSSFWorkbook();
HSSFSheet sheet=workbook.createSheet("sheet1");
if(dataList!=null){
HSSFRow titleRow=sheet.createRow(0);
for(int i=0;i<titles.length;i++){
HSSFCell cell=titleRow.createCell((short)i);
cell.setCellValue(titles[i]);
}
//填充表格
for(int i=0;i<dataList.size();i++){
HSSFRow dataRow=sheet.createRow(i+1);
Object obj=dataList.get(i);
Method[] methods=obj.getClass().getMethods();
int j=0;
for(Method method:methods){
if(method.getName().startsWith("get")&&!method.getName().equals("getClass")){
HSSFCell cell=dataRow.createCell((short)j++);
Object value=method.invoke(obj);
cell.setCellValue(value.toString());
}
}
}
}
}
@Override
protected void doExecute(String arg0, ActionInvocation invocation)
throws Exception {
//获取到ActionContext的实例
ActionContext ctx=invocation.getInvocationContext();
//通过ActionContext对象获取到response
HttpServletResponse response=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
//获取到outputStream实例,用于将excel输出到流中
OutputStream out=response.getOutputStream();
//设置响应头与内容
response.setHeader("Content-disposition", "attachment;filename=SCCE.xls");
response.setContentType("application/msexcel;charset=gbk");
exportExcel(ctx);
workbook.write(out);
out.flush();
out.close();
}