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); } }}
阅读全文
0 0
- struts2导出excel java 导出excel
- struts2导出excel java 导出excel
- java导出EXCEL
- JAVA 导入导出Excel
- Java jxl导出excel
- java导出Excel
- java 导出 Excel 报表
- java excel导入导出
- java excel导入导出
- JAVA导出EXCEL心得
- 【Java】导出excel表格
- java poi 导出excel
- java怎么导出excel
- java 导出Excel
- JAVA导出EXCEL
- JAVA导出EXCEL心得 .
- java导出Excel例子
- java 导出 excel
- macOS装回java8
- js常用方法总结
- dubbo实战---No provider available for the service
- CXF实现wsdl文件转换为javaBean
- CSDN中搜索用户
- Java Excel导出
- vs2008设置选中 高亮
- Ubuntu 16.04 安装chrome+postman
- 在阿里云海外windows主机上开启virtualPN
- 第三次作业
- leetcode 485. Max Consecutive Ones
- 3224: Tyvj 1728 普通平衡树(Splay裸题)
- Tensorflow(r1.4)API--tf.truncated_normal()
- 《OpenCV视频中分割圆形区域》