Java Excel导出
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java Excel导出
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                一、Excel依賴包POI
(1)Maven配置
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.7</version></dependency>(2)jar下載地址http://poi.apache.org/download.html
二、Java代碼實(shí)例
Excel工具代碼
package com.mk.util;import com.mk.bean.LogInfo; import org.apache.poi.hssf.usermodel.*;import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.poi.ss.util.CellRangeAddress;public class Excels {public static byte[] export(String sheetName, String title, List<LogInfo> list, int start, int end) {if (list == null||start>end) {return null;}HSSFWorkbook workbook = new HSSFWorkbook(); //工作簿initWorkbook(workbook, sheetName, title, list,start,end);//先寫到字節(jié)數(shù)組,在從字節(jié)數(shù)組讀出 ByteArrayOutputStream os = new ByteArrayOutputStream();try {workbook.write(os);byte[] bytes = os.toByteArray();return bytes;} catch (IOException ex) {Logger.getLogger(Excels.class.getName()).log(Level.SEVERE, null, ex);} finally {try {os.close();} catch (IOException ex) {Logger.getLogger(Excels.class.getName()).log(Level.SEVERE, null, ex);}}return null;}private static final int MAX_ROWS = 65535;/*** 初始化工作簿* @param workbook 工作簿* @param sheetName 工作表名* @param title 標(biāo)題* @param list 數(shù)據(jù)* @param start 起始索引,起始值為0* @param end 截止索引,為末尾索引+1*/private static void initWorkbook(HSSFWorkbook workbook, String sheetName, String title,List<LogInfo> list, int start, int end) {assert end - start <= MAX_ROWS - 2;HSSFSheet sheet = workbook.createSheet(sheetName); //工作表 initTitle(workbook, sheet, title, 0, 16, 800, 0, 0, 0, 2);// 屬性樣式 HSSFCellStyle attrCellStyle = createStyle(workbook, 10, true, HSSFFont.BOLDWEIGHT_BOLD);// 默認(rèn)樣式HSSFCellStyle defaultCellStyle = createStyle(workbook, 10, true, HSSFFont.BOLDWEIGHT_NORMAL);//屬性行HSSFRow attrRow = sheet.createRow(1);final int baseFont=10;sheet.setColumnWidth(0, baseFont*400);sheet.setColumnWidth(1, baseFont*800);sheet.setColumnWidth(2, baseFont*1200);HSSFCell cell = attrRow.createCell(0);cell.setCellValue("索引");cell.setCellStyle(attrCellStyle);cell = attrRow.createCell(1);cell.setCellValue("時(shí)間");cell.setCellStyle(attrCellStyle);cell = attrRow.createCell(2);cell.setCellValue("信息");cell.setCellStyle(attrCellStyle);DateFormat dateFomater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");for (int i = 2; start < end; i++) {LogInfo logInfo = list.get(start++);HSSFRow row = sheet.createRow(i);//除開標(biāo)題和屬性行//索引cell = row.createCell(0);cell.setCellValue(logInfo.getIndex());cell.setCellStyle(defaultCellStyle);//時(shí)間cell = row.createCell(1);cell.setCellValue(dateFomater.format(logInfo.getTime()));cell.setCellStyle(defaultCellStyle);//信息cell = row.createCell(2);cell.setCellValue(logInfo.getInfo());cell.setCellStyle(defaultCellStyle);}}/*** 設(shè)置工作表標(biāo)題** @param workbook 工作簿* @param sheet 工作表* @param title 標(biāo)題* @param rowIndex 當(dāng)前行* @param fontSize 字體* @param height 高度* @param firstRow 合并區(qū)域起始行* @param lastRow 合并區(qū)域末尾行* @param firstCol 合并區(qū)域起始列* @param lastCol 合并區(qū)域末尾列*/private static void initTitle(HSSFWorkbook workbook, HSSFSheet sheet,String title, int rowIndex, int fontSize, int height, int firstRow,int lastRow, int firstCol, int lastCol) {HSSFCellStyle titleCellStyle = createStyle(workbook, fontSize, true, HSSFFont.BOLDWEIGHT_BOLD);//標(biāo)題行HSSFRow titleRow = sheet.createRow(0);titleRow.setHeight((short) height); //標(biāo)題行高度HSSFCell titleCell = titleRow.createCell(rowIndex); //標(biāo)題單元titleCell.setCellValue(title); //設(shè)置標(biāo)題titleCell.setCellStyle(titleCellStyle);sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));//指定合并區(qū)域 }private static HSSFCellStyle createStyle(HSSFWorkbook workbook, int fontSize, boolean wrapText, short boldweight) {// 標(biāo)題字體HSSFFont font = workbook.createFont();font.setFontHeightInPoints((short) fontSize);// 字體大小font.setBoldweight(boldweight); //字體加粗//標(biāo)題樣式HSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平中間對齊cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直中間對其cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); //頂邊cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//底邊cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左邊cellStyle.setWrapText(wrapText); //自動換行cellStyle.setFont(font);//樣式字體return cellStyle;} }日志類
package com.mk.bean;import java.util.Date;public class LogInfo {private int index;private Date time;private String info;public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public Date getTime() {return time;}public void setTime(Date time) {this.time = time;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;} }測試代碼
package com.mk.testmaven;import com.mk.bean.LogInfo; import com.mk.util.Excels; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List;public class Test {public static void main(String[] args) {List<LogInfo> list=new ArrayList<>();LogInfo logInfo=new LogInfo();logInfo.setIndex(0);logInfo.setInfo("你好");logInfo.setTime(new Date(2018,9,12));list.add(logInfo );logInfo=new LogInfo();logInfo.setIndex(1);logInfo.setInfo("問你");logInfo.setTime(new Date(2018,9,15));list.add(logInfo );logInfo=new LogInfo();logInfo.setIndex(2);logInfo.setInfo("世界");logInfo.setTime(new Date(2018,9,17));list.add(logInfo );byte[] bs=Excels.export("1", "日志", list,0,list.size());try (FileOutputStream outputStream=new FileOutputStream("D:/log.xls")){outputStream.write(bs);outputStream.flush();} catch (Exception e) {}} }總結(jié)
以上是生活随笔為你收集整理的Java Excel导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 电脑端微信如何多开如何电脑多开微信账号
 - 下一篇: 电影剪辑的12条进阶技巧电影的剪辑方法