java导出Excel通用方法的实例详解
作者:lqh 时间:2022-06-25 14:28:25
java导出Excel通用方法的实例详解
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;
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/johnstrive/article/details/8109919
标签:java,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java注解的类型知识点总结
2022-11-04 00:26:02
java实现简单石头剪刀布游戏
2023-07-20 05:31:37
![](https://img.aspxhome.com/file/2023/8/57628_0s.jpg)
谈谈RxJava2中的异常及处理方法
2023-05-12 09:18:11
Unity实现VR中在黑板上写字效果
2021-08-04 20:11:11
![](https://img.aspxhome.com/file/2023/0/126070_0s.gif)
Android Volley图片加载功能详解
2023-11-19 08:01:41
一款非常简单酷炫的LoadingView动画效果
2021-11-09 11:49:08
![](https://img.aspxhome.com/file/2023/7/137347_0s.gif)
java io读取文件操作代码实例
2023-04-12 08:53:57
浅谈C#设计模式之工厂模式
2021-12-17 16:06:48
Recyclerview添加头布局和尾布局、item点击事件详解
2022-04-19 12:38:12
![](https://img.aspxhome.com/file/2023/8/139038_0s.gif)
C#基于Socket实现多人聊天功能
2021-12-06 12:00:47
![](https://img.aspxhome.com/file/2023/9/104359_0s.jpg)
C++ lambda函数详解
2023-06-20 07:49:43
Unity实现10天签到系统
2022-07-11 23:40:01
![](https://img.aspxhome.com/file/2023/0/89820_0s.jpg)
Android实现的数字格式化用法示例
2023-09-30 09:03:28
浅谈Maven镜像更换为阿里云中央仓库(精)
2022-08-06 04:48:17
C#实现围棋游戏
2022-04-20 11:08:57
![](https://img.aspxhome.com/file/2023/0/96400_0s.jpg)
Java使用DOM4j实现读写XML文件的属性和元素
2021-10-19 00:21:52
Java自定义实现equals()方法过程解析
2021-10-12 11:21:48
Android使用CardView作为RecyclerView的Item并实现拖拽和左滑删除
2022-03-20 00:33:28
![](https://img.aspxhome.com/file/2023/0/138690_0s.png)
SpringBoot 配置文件加密的步骤
2023-10-23 02:55:55
Android本地实现搜索历史记录
2023-08-15 22:25:17
![](https://img.aspxhome.com/file/2023/3/99803_0s.jpg)