Excel导出到文件工具
2016-05-24 10:45
274 查看
经常遇到需要将数据库的数据导入到表中的需求.封装以下代码.只为实现基本功能,并没有做到完善.
使用演示:
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 { @Test public 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>() { @Override protected 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(); } }
相关文章推荐
- 使用Python生成Excel格式的图片
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- VBA将excel数据表生成JSON文件
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- SQL 导入导出Excel数据的语句
- 文本、Excel、Access数据导入SQL Server2000的方法
- C#实现Excel动态生成PivotTable
- C#导出数据到Excel文件的方法
- Vbscript生成Excel报表的常用操作总结
- C#实现导入CSV文件到Excel工作簿的方法
- C#基于NPOI生成具有精确列宽行高的Excel文件的方法
- 总提示[Microsoft][ODBC Excel Driver] 数值字段溢出官方解决方法
- C#将Sql数据保存到Excel文件中的方法
- VC6.0实现读取Excel数据的方法
- C#定制Excel界面并实现与数据库交互的方法
- 把excel表格里的数据导入sql数据库的两种方法
- access dbase excel foxpro 数据库浏览器V3.1版 下载
- C#实现把txt文本数据快速读取到excel中
- C#自定义导出数据到Excel的类实例