封装一个导出文件工具

来源:互联网 发布:计算器编程代码 编辑:程序博客网 时间:2024/05/29 18:59

参数讲解:

1.第一个参数:第一行显示的信息(如 姓名  年龄  升高  体重 等等)

2.第二个参数:为要替换进去的数据(比如  name age hight  weight ..)

3.第三个参数:为从数据库中查出的数据

4.第四个参数:为文件名可以随便起


public static<T> void exportExcel(List<String> headAttribute,List<String> bodyAttribute,List<T> bodyData,String fileName){

   
    MutiStyleExcelWrite mew = new MutiStyleExcelWrite();
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletResponse response = attributes.getResponse();
    response.reset();
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;fileName="+fileName+System.currentTimeMillis()+".xls");
response.setContentType("application/shlnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
    try{
    mew.createExcel(response.getOutputStream(),bodyData, bodyAttribute, headAttribute,fileName, fileName);
    }catch(Exception e){
    throw new RuntimeException(e);
    }

    }




import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
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;


public class MutiStyleExcelWrite  {

    public <T> void  createExcel(OutputStream os,List<T> objList,
List<String> propOrderList, List<String> tableHeadList,
String excelName, String sheetName) throws WriteException,IOException {
        //创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);      
        
        //构造表头   
        createExcelHead(sheet,tableHeadList);
        WritableFont boldNumber = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑体
        WritableCellFormat boldNumberFormate = new WritableCellFormat(boldNumber);
        
        if(objList!=null&&!"".equals(objList)){
for (int objNum = 0; objNum < objList.size(); objNum++) {
for (int cellNum = 0; cellNum < propOrderList.size(); cellNum++) {
// 将属性的首字母大写
String propertyName = propOrderList.get(cellNum).replaceFirst(
propOrderList.get(cellNum).substring(0, 1),
propOrderList.get(cellNum).substring(0, 1)
.toUpperCase());
//设置单元格宽度
sheet.setColumnView(cellNum+1,15);
try {
// 如果type是类类型,则前面包含"class ",后面跟类名
Method m = objList.get(objNum).getClass()
.getMethod("get" + propertyName);
// 调用getter方法获取属性值
Object property = m.invoke(objList.get(objNum));
//整形数据
if (property instanceof Integer ) {
Number ints = new Number(cellNum,1+objNum,(Integer)property);
sheet.addCell(ints);
}else if(property instanceof Float){
Number ints = new Number(cellNum,1+objNum,(Float)property);
sheet.addCell(ints);
}else if(property instanceof Double){
Number ints = new Number(cellNum,1+objNum,(Double)property);
sheet.addCell(ints);
}else if(property instanceof Long){
Number ints = new Number(cellNum,1+objNum,(Long)property);
sheet.addCell(ints);
}else  if (property instanceof Date) {
String strdate  = TimeHelper.dateTimeToStr2((Date) property,
"yyyy-MM-dd HH:mm:ss") ;
Label example = new Label(cellNum,1+objNum,strdate);
sheet.addCell(example);
}
else{
Label example = new Label(cellNum,1+objNum,(String)property,boldNumberFormate);
sheet.addCell(example);
}
} catch (Exception e) {
log.error("生成excel解析数据出错:"+ e);
}
}
}
        }
        //把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        os.close(); 
    }



    public  void createExcelHead( WritableSheet sheet,
List<String> tableHeadList) throws RowsExceededException, WriteException {
for (int cellNum = 0; cellNum < tableHeadList.size(); cellNum++) {
Label floats = new Label(cellNum,0,tableHeadList.get(cellNum));
sheet.addCell(floats);
}
}
    
}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族手机gps信号弱怎么办 魅族手机突然没有信号怎么办 魅族手机流量信号不好怎么办 魅族手机wifi信号差怎么办 魅族5s信号不好怎么办 魅族mx5的双击不亮屏怎么办 魅族mx5返回键失灵怎么办 电信苹果3g网速慢怎么办 魅蓝6开不了机怎么办 手机应用被锁了怎么办 魅族电池不耐用怎么办 魅族mx6现在很卡怎么办 魅族e2手机屏幕背景黑色怎么办 魅族x6手机锁了怎么办 360n5返回键失灵怎么办 360n5返回键不好用怎么办 魅族手机锁屏怎么办 魅族手机锁住了怎么办 手机己锁定怎么办魅族 魅蓝u10触屏没反应怎么办 魅蓝e2手机锁定怎么办 苹果手机声音键坏了怎么办 一加6的屏幕问题怎么办 魅蓝note6卡顿怎么办 苹果4s内屏坏了怎么办 魅族mx6一直重启怎么办 魅族充电慢了怎么办啊 笔记本玩游戏掉帧怎么办 psv玩游戏掉帧怎么办 ipad玩游戏掉帧怎么办 手机玩游戏严重掉帧怎么办 苹果7p掉电快怎么办 努比亚z11手机总是开关机怎么办 努比亚手机不能保存图片怎么办 小米8拍照和努比亚怎么办 oppor7手机开不了机怎么办 vivox5m手机开不开机怎么办 步步高x7图案解锁忘记怎么办 手机机锁定怎么办魅族 衣服买完降价了怎么办 在淘宝上不发货怎么办