qqqq

package com.xmmy.intelligence.service;

import java.io.File;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xmmy.core.base.service.DefaultEntityManager;
import com.xmmy.core.util.PropertiesRead;

@SuppressWarnings("unchecked")
@Service
@Transactional
public class ExportExcelManager extends DefaultEntityManager {
    /**
     * 根据记录集合和字段生成excel文件并直接写入到输出流
     * @param valueList
     * @param attributes
     * @param os
     * @author:陈涌
     */
    public void exportData(List valueList, String attributes, OutputStream os) {
        String[] attrArr = attributes.split(",");
        String[] headNames = new String[attrArr.length];
        String[] attrNames = new String[attrArr.length];
        for (int i = 0; i < attrArr.length; i++) {
            headNames[i] = attrArr[i].substring(0, attrArr[i].indexOf(":"));
            attrNames[i] = attrArr[i].substring(attrArr[i].indexOf(":") + 1);
        }
        try {
            
            WritableWorkbook book = Workbook.createWorkbook(os);
            // 生成名为“第一页”的工作表,参数0表示这是第一页
            WritableSheet sheet = book.createSheet(" 第一页 ", 0);
            // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
            // 以及单元格内容为test
            for (int i = 0; i < headNames.length; i++) {
                Label label = new Label(i, 0, headNames[i]);
                sheet.addCell(label);
            }
            for (int i = 0; i < valueList.size(); i++) {
                Object obj = valueList.get(i);
                for (int j = 0; j < attrNames.length; j++) {
                    String value = BeanUtils.getProperty(obj, attrNames[j]);
                    Label label = new Label(j, i + 1, value);
                    sheet.addCell(label);

                }
            }

            // 写入数据并关闭文件
            book.write();
            book.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
    public File exportData(List valueList, String attributes) {
        String[] attrArr = attributes.split(",");
        String[] headNames = new String[attrArr.length];
        String[] attrNames = new String[attrArr.length];
        File f = null;
        for (int i = 0; i < attrArr.length; i++) {
            headNames[i] = attrArr[i].substring(0, attrArr[i].indexOf(":"));
            attrNames[i] = attrArr[i].substring(attrArr[i].indexOf(":") + 1);
        }
        try {
            String savePath = PropertiesRead.getProperty("save_dir_qb");
            SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            SimpleDateFormat sdfDir = new SimpleDateFormat("yyyy_MM_dd");
            Date date = new Date();
            String fileName = sdfFileName.format(date);
            String dir = sdfDir.format(date);
            fileName = savePath + "/export/" + dir + "/" + fileName + ".xls";
            f = new File(fileName);
            if (!f.exists()) {
                if(!f.getParentFile().exists())
                    f.getParentFile().mkdirs();
                f.createNewFile();
            }
            WritableWorkbook book = Workbook.createWorkbook(f);
            // 生成名为“第一页”的工作表,参数0表示这是第一页
            WritableSheet sheet = book.createSheet(" 第一页 ", 0);
            // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
            // 以及单元格内容为test
            for (int i = 0; i < headNames.length; i++) {
                Label label = new Label(i, 0, headNames[i]);
                sheet.addCell(label);
            }
            for (int i = 0; i < valueList.size(); i++) {
                Object obj = valueList.get(i);
                for (int j = 0; j < attrNames.length; j++) {
                    String value = BeanUtils.getProperty(obj, attrNames[j]);
                    Label label = new Label(j, i + 1, value);
                    sheet.addCell(label);
                    
                }
            }
            
            // 写入数据并关闭文件
            book.write();
            book.close();
            
        } catch (Exception e) {
            System.out.println(e);
        }
        return f;
    }

}




@SuppressWarnings("unchecked")
    private String export(List list, String attributes) throws Exception {
        SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String fileName = sdfFileName.format(new Date());
        fileName = "export_" + fileName + ".xls";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment; filename="
                + fileName);
        this.exportExcelManager.exportData(list, attributes, response
                .getOutputStream());
        return null;
    }




if (export)
            return export(
                    page.getResult(),
                    "信息编号:msgId,信息形式:harmfulMsgMsgStyle.name,信息来源:msgSrc,网站名称:webSiteName,栏目名称:partName,信息标题:msgTitle,URL:url,网站域名:webSiteDomain,网站IP:webSiteIp,网站接入地:webSiteAddress,开办者所在地:founderAddress,搜索词:searchWord,信息类别:msgType,传播方式:spreadMode,网上部位:webPosition,工信备案:isGxRecord,公安备案:isGaRecord,发现单位:foundDept,发现人员:foundPerson,发现时间:foundTime,通报时间:noticeTime,处置单位:dealDept,处置人员:dealPerson,处置结果:dealResult,处置时间:dealTime,处置期限:dealTimeLimit");


你可能感兴趣的:(java多线程,hibernate)