java 通用导出一个excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 通用导出一个excel相关的知识,希望对你有一定的参考价值。
可以自定义标题,可以传bean.并且自定义输入bean里面的字段(也就是不要全部输出)..网上看了很多.. 就是不知道怎么自定义输出bean的字段.. 网上都是全部输入
java导出Excel通用方法,只需要一个list 集合。package oa.common.utils;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import java.lang.reflect.Field;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/***
* @author lsf
*/
public class ExportExcel
/***************************************************************************
* @param fileName EXCEL文件名称
* @param listTitle EXCEL文件第一行列标题集合
* @param listContent EXCEL文件正文数据集合
* @return
*/
public final static String exportExcel(String fileName,String[] Title, List<Object> listContent)
String result="系统提示:Excel文件导出成功!";
// 以下开始输出到EXCEL
try
//定义输出流,以便打开保存对话框______________________begin
HttpServletResponse response=ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="+ new String(fileName.getBytes("GB2312"),"ISO8859-1"));
// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
//定义输出流,以便打开保存对话框_______________________end
/** **********创建工作簿************ */
WritableWorkbook workbook = Workbook.createWorkbook(os);
/** **********创建工作表************ */
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
/** **********设置纵横打印(默认为纵打)、打印纸***************** */
jxl.SheetSettings sheetset = sheet.getSettings();
sheetset.setProtected(false);
/** ************设置单元格字体************** */
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD);
/** ************以下设置三种单元格样式,灵活备用************ */
// 用于标题居中
WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
wcf_center.setWrap(false); // 文字是否换行
// 用于正文居左
WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐
wcf_left.setWrap(false); // 文字是否换行
/** ***************以下是EXCEL开头大标题,暂时省略********************* */
//sheet.mergeCells(0, 0, colWidth, 0);
//sheet.addCell(new Label(0, 0, "XX报表", wcf_center));
/** ***************以下是EXCEL第一行列标题********************* */
for (int i = 0; i < Title.length; i++)
sheet.addCell(new Label(i, 0,Title[i],wcf_center));
/** ***************以下是EXCEL正文数据********************* */
Field[] fields=null;
int i=1;
for(Object obj:listContent)
fields=obj.getClass().getDeclaredFields();
int j=0;
for(Field v:fields)
v.setAccessible(true);
Object va=v.get(obj);
if(va==null)
va="";
sheet.addCell(new Label(j, i,va.toString(),wcf_left));
j++;
i++;
/** **********将以上缓存中的内容写到EXCEL文件中******** */
workbook.write();
/** *********关闭文件************* */
workbook.close();
catch (Exception e)
result="系统提示:Excel文件导出失败,原因:"+ e.toString();
System.out.println(result);
e.printStackTrace();
return result;
测试:
/**
* 导出excel
* @return
*/
public String excelPage()
ExportExcel excel=new ExportExcel();
String str="";
try
str = new String(getHTTP.getRequest().getParameter("wineOrg.orgName").getBytes("iso8859-1"),"UTF-8");
catch (UnsupportedEncodingException e)
// TODO Auto-generated catch block
e.printStackTrace();
wineOrg.setOrgName(str);
List<Object> li=service.exportExcel(wineOrg);
String[] Title="机构ID","会员编号","类别","名称","省ID","省名称","城市ID","城市名称","详细地址","联系人","性别","联系手机","联系电话","传真","邮箱","QQ","生日","积分","客户等级","现金账户余额","结算方式","客户类型","购买次数","购买支数","创建人ID","创建人姓名","create_time","del","STS","备注","负责人ID","负责人姓名","审核标识","审核人ID ","审核人姓名","审核日期","分配人ID","分配人姓名","分配日期","修改人ID","修改人姓名 ","修改时间";
excel.exportExcel("客户资料信息.xls",Title, li);
return SUCCESS;
参考技术A package com.tools.bean; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.sql.ResultSet; import java.util.*; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class printattendance private ArrayList list=new ArrayList(); private ArrayList list2=null; private String f1=null; private String f2=null; private String f3=null; public void WriteCell(ArrayList list) HSSFWorkbook wb=new HSSFWorkbook(); HSSFSheet sheet=wb.createSheet("考勤管理"); sheet.setColumnWidth((short)0,(short)4000); sheet.setColumnWidth((short)1,(short)4000); sheet.setColumnWidth((short)2,(short)4000); HSSFRow row=sheet.createRow(0); HSSFCell cell[]=new HSSFCell[3]; for(short i=0;i<3;i++) cell[i]=row.createCell(i); cell[i].setEncoding(HSSFCell.ENCODING_UTF_16); cell[0].setCellValue("员工工号"); cell[1].setCellValue("次数"); cell[2].setCellValue("总金额"); this.list=list; if(list.size()>0&&list!=null) for(int i=0;i<list.size();i++) list2=(ArrayList)list.get(i); HSSFRow datarow = sheet.createRow(i + 1); HSSFCell data[] = new HSSFCell[8]; for (short j = 0; j < 3; j++) data[j] = datarow.createCell(j); // 将单元格定义成UTF_16编码,这样才能使输出数据不会乱码 data[j].setEncoding(HSSFCell.ENCODING_UTF_16); f2=""+list2.get(1); f3=""+list2.get(2); data[0].setCellValue((String)list2.get(0)); data[1].setCellValue(f2); data[2].setCellValue(f3); File file = new File("e:\\text\\attendancebook.xls"); try FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); catch (IOException e) e.printStackTrace(); 本回答被提问者采纳
JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦
如今导出excel基本上都是用poi了,当报表格式非常负责的时候 开发难度会加大
假设报表有格式有变化 那就更复杂了,先发现一个非常老的技术。能够解决格式复杂的报表。
实例代码例如以下:
<%@ page contentType="application/vnd.ms-excel;charset=GBK" %> <%@page import="java.net.URLEncoder"%> <% String filedisplay = "文件.xls"; filedisplay = URLEncoder.encode(filedisplay, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay); %> <!doctype html> <html> <head> <title>导出Excel实例</title> <style>tr td{border:1px red double;}</style> </head> <body> <table style="border:1px"> <tr> <td>ID</td> <td>名称</td> <td>性别</td> <td>年龄</td> </tr> <tr> <td>1</td> <td>张三</td> <td>男</td> <td>18</td> </tr> <tr> <td>2</td> <td>张三</td> <td>男</td> <td>18</td> </tr><tr> <td>3</td> <td>张三</td> <td>男</td> <td>18</td> </tr><tr> <td>4</td> <td>张三</td> <td>男</td> <td>18</td> </tr> </table> </body> </html>
以上是关于java 通用导出一个excel的主要内容,如果未能解决你的问题,请参考以下文章
[DevExpress]DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表