Java Excel导出

来源:互联网 发布:ise12.3软件下载 编辑:程序博客网 时间:2024/06/16 18:32

Maven配置

<dependency>            <groupId>org.apache.poi</groupId>            <artifactId>poi</artifactId>            <version>3.7</version>        </dependency>


jar下载地址http://poi.apache.org/download.html


Java代码

package com.mk.util;import org.apache.poi.hssf.usermodel.*;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.hssf.util.Region;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.List;import java.util.Set;public class ExcelUtil {      /**     * 导出Excel     *     * @param list      * @return excel的字节数组     */    public static byte[] exportLogs(List<Log> list) {        if (list == null) {            return null;        }        try {            HSSFWorkbook wb = new HSSFWorkbook();            int s=0;            for (int i = 0; s < list.size();i++,s = i * MAX_ROWS ) {                createOnePage(wb, list, s, i);            }            //先写到字节数组,在从字节数组读出            ByteArrayOutputStream os = new ByteArrayOutputStream();            try {                wb.write(os);            } catch (IOException e) {                           }            byte[] bytes= os.toByteArray();            try {                os.close();            } catch (IOException e) {                           }            return bytes;        } catch (Exception e) {                  }        return null;    }    private static final int MAX_ROWS=65530;    private static void createOnePage(HSSFWorkbook wb, List<Log> list, int start, int index) {        HSSFSheet sheet = wb.createSheet("sheet" + index);        // 样式对象        HSSFCellStyle cs0 = wb.createCellStyle();        cs0.setAlignment(HSSFCellStyle.ALIGN_CENTER);//        cs0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);        cs0.setBorderBottom(HSSFCellStyle.BORDER_THIN);//        cs0.setBorderLeft(HSSFCellStyle.BORDER_THIN);//        cs0.setBorderRight(HSSFCellStyle.BORDER_THIN);//        cs0.setBorderTop(HSSFCellStyle.BORDER_THIN);//        cs0.setWrapText(true);        // 创建字体        HSSFFont ff0 = wb.createFont();        ff0.setFontHeightInPoints((short) 14);// 字体大小        ff0.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   //字体加粗        cs0.setFont(ff0);//放入样式中        HSSFRow row0 = sheet.createRow(0);        row0.setHeight((short) 500);        HSSFCell cell0 = row0.createCell(0);        cell0.setCellValue("导出日志报表");        cell0.setCellStyle(cs0);        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));//指定合并区域        // 样式对象        HSSFCellStyle cs = wb.createCellStyle();        cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);//        cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);        cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);//        cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);//        cs.setBorderRight(HSSFCellStyle.BORDER_THIN);//        cs.setBorderTop(HSSFCellStyle.BORDER_THIN);//        cs.setWrapText(true);        // 创建字体        HSSFFont ff = wb.createFont();        ff.setFontHeightInPoints((short) 10);// 字体大小        ff.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   //字体加粗        cs.setFont(ff);//放入样式中        // 样式对象        HSSFCellStyle cs2 = wb.createCellStyle();        cs2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//        cs2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);        cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);//        cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//        cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);//        cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);//        cs2.setWrapText(true);        // 创建字体        HSSFFont ff2 = wb.createFont();        ff2.setFontHeightInPoints((short) 10);// 字体大小        cs2.setFont(ff2);//放入样式中        HSSFRow row1 = sheet.createRow(1);        sheet.setColumnWidth(0, 32 * 200);        sheet.setColumnWidth(1, 32 * 150);        sheet.setColumnWidth(2, 32 * 900);        HSSFCell cell = row1.createCell(0);        cell.setCellValue("TIME");        cell.setCellStyle(cs);        cell = row1.createCell(1);        cell.setCellValue("IP");        cell.setCellStyle(cs);        cell = row1.createCell(2);        cell.setCellValue("DATA");        cell.setCellStyle(cs);        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        int len = start + MAX_ROWS <= list.size() ? MAX_ROWS : list.size() - start;        for (int i = 0; i < len; i++) {            Log log = list.get(start++);            HSSFRow row = sheet.createRow(i+2);            cell = row.createCell(0);            cell.setCellValue(df.format(log.getTime()));            cell.setCellStyle(cs2);            cell = row.createCell(1);            cell.setCellValue(log.getIP());            cell.setCellStyle(cs2);            cell = row.createCell(2);            cell.setCellValue(T.toJsonString(log.getText()));            cell.setCellStyle(cs2);        }    }}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一周多的宝宝拉肚子怎么办 出生半个月的宝宝拉肚子怎么办 刚出生几天的宝宝拉肚子怎么办 刚出生的宝宝拉肚子怎么办 出生八天的宝宝拉肚子怎么办 刚出生婴儿拉水怎么办 三岁宝宝一直吐怎么办 3岁宝宝一直吐怎么办 5个月宝宝一直吐怎么办 新生儿睡反了觉怎么办 吃母乳奶流量大怎么办 两个月宝宝有痰怎么办 婴儿瞌睡睡倒了怎么办 侧切伤口崩开了怎么办 脐带30天未脱落怎么办 洗眉结痂不掉怎么办 新生宝宝生殖器下面破皮怎么办 脐带未脱落渗血怎么办 车停店门口贴发单怎么办 有人要打我我该怎么办 很笨怎么办工作做不好 脸被电焊烤了疼怎么办 脸过敏痒的不行怎么办 领导想让你辞职怎么办 领导强吻我了怎么办 赫曼夏天温度高怎么办 近视看3d电影怎么办 有声挂图撕坏了怎么办 主机没有放光盘的设备怎么办 装显卡是没光驱怎么办 电脑机箱光盘总是出来怎么办 看书网作者密码忘记怎么办 电脑光驱访问出现出错怎么办 电脑不读dvd光盘怎么办 dvd光盘读不出来怎么办 光盘插在电脑里出不来怎么办 cad画斜线不光滑怎么办 ps没有魔棒工具怎么办 洗完鞋子发黄了怎么办 牛拜单车不退押金怎么办 总裁太爱我怎么办小说