Excel导出到文件工具

来源:互联网 发布:口红保质期 知乎 编辑:程序博客网 时间:2024/05/16 09:19

经常遇到需要将数据库的数据导入到表中的需求.封装以下代码.只为实现基本功能,并没有做到完善.

package com.jiechengkeji.manager.util;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Collection;import java.util.UUID;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public abstract class ExcelBuilder<T> {protected CellStyle titleStyle;protected CellStyle contentStyle; private int pointer =1;private Workbook workbook ;public ExcelBuilder(){workbook= new XSSFWorkbook();workbook.createSheet();initStyle();}public ExcelBuilder<T> name(String sheetName){workbook.setSheetName(0, sheetName);return this;}public ExcelBuilder<T> title(String[] title){Sheet sheet = workbook.getSheetAt(0);Row row =sheet.createRow(0);for(int i =0;i<title.length;i++){Cell c = row.createCell(i,Cell.CELL_TYPE_STRING);c.setCellValue(title[i]); c.setCellStyle(titleStyle);}return this;}abstract protected String[] mapEntity(T t);public ExcelBuilder<T> append(Collection<T> content){Sheet sheet = workbook.getSheetAt(0);for(T t: content){Row row =sheet.createRow(pointer++);String[] strs = this.mapEntity(t);for(int i =0;i<strs.length;i++){Cell c = row.createCell(i,Cell.CELL_TYPE_STRING);c.setCellValue(strs[i]);}row.setRowStyle(contentStyle);}return this;}public File build(){File file = new File(UUID.randomUUID().toString());FileOutputStream fos =  null;try {file.createNewFile();fos = new FileOutputStream(file);workbook.write(fos);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {fos.flush();fos.close();} catch (IOException e) {e.printStackTrace();}}return file;}protected void initStyle() {Font font = workbook.createFont();font.setFontName("宋体");        font.setFontHeightInPoints((short) 14);// 设置字体大小titleStyle=workbook.createCellStyle();titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); titleStyle.setFillForegroundColor(IndexedColors.GREEN.index);//titleStyle.setFont(font);contentStyle=workbook.createCellStyle();//contentStyle.setFont(font);}}


使用演示:


public class ExcelBuilderTest extends BaseTest {@Testpublic void test() throws IOException {List<User> users = ctx.getBean(IUserDao.class).getByPeriod(0, 100, DateConstants.ABSOLUTE_START,DateConstants.ABSOLUTE_END);ExcelBuilder<User> ueb = new ExcelBuilder<User>() {@Overrideprotected String[] mapEntity(User user) {return new String[] { Integer.toString(user.getId()), user.getUsername(), user.getCreateTime().toString() };}};File excel = ueb.name("用户清单").title(new String[] { "ID", "姓名", "创建时间" }).append(users).append(users)//可多次填充内容.build();}}



1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 夫妻离婚后孩子怎么办 离婚小孩的户口怎么办 离婚孩子落户口怎么办 父母离婚孩子出生证怎么办 离婚怎么办孩子的户口 离婚了儿子户口怎么办 性格不合离婚 孩子怎么办 前夫不迁走户口怎么办 我想离婚老婆不同意怎么办 要离婚没身份证怎么办 老婆怀孕想离婚怎么办 我老婆起诉离婚怎么办 父母冷战我该怎么办 经常跟父母亲吵架怎么办 14岁父母离婚怎么办 夫妻为钱吵架怎么办 离婚后担心孩子怎么办 不爱对方有孩子怎么办 爸和大伯吵架怎么办 孩子和家长吵架怎么办 老公吵架摔东西怎么办 楼上吵架摔东西怎么办 宿友没素质的人怎么办 父母人品差自私怎么办 家里穷压力大怎么办 偏执型人格障碍怎么办 遇到低素质保安怎么办 和老婆性格不合怎么办 当心理阴暗时怎么办 孩子胆子小怎么办呢? 父母伤亡的孩子怎么办 小孩上课老是走神怎么办 小孩上课老走神怎么办 小孩上课爱走神怎么办 孩子对学习成绩无所谓怎么办 孩子脾气特别倔强怎么办 没有规矩的孩子怎么办 孩子情商太低怎么办 4岁宝宝缺乏自信怎么办 孩子害羞不自信怎么办 说话脸红的人怎么办