/**
**单元格值对象
**/
public class Cells { /***
* 行
*/
private int row; /**
* 列
*/
private int column; /**
* 单元格的值
*/
private Object val; public int getRow() {
return row;
} public void setRow(int row) {
this.row = row;
} public int getColumn() {
return column;
} public void setColumn(int column) {
this.column = column;
} public Object getVal() {
return val;
} public void setVal(Object val) {
this.val = val;
} public Cells(int row, int column, Object val) {
this.row = row;
this.column = column;
this.val = val;
} public Cells() {} }
/**
**表空间对象
**/
public class Sheet { private String sheetName; public String getSheetName() {
return sheetName;
} public void setSheetName(String sheetName) {
this.sheetName = sheetName;
} public List<Cells> getCells() {
return cells;
} public void setCells(List<Cells> cells) {
this.cells = cells;
} private List<Cells> cells=Lists.newArrayList(); public Sheet(String sheetName, List<Cells> cells) {
this.sheetName = sheetName;
this.cells = cells;
} public Sheet() {} }
 public final class ExcelUntil {

     /**
* 根据模板导出excel
* @param templatePath 模板路径
* @param sheets 设置sheet 表空间的单元格具体的值对象
* @param exportPath 导出路径
* @throws Exception
*/
@SuppressWarnings("resource")
public static void exportExcelByTemplate(String templatePath, List<Sheet> sheets, String exportPath)
throws Exception {
if (Strings.isStringEmpty(templatePath) || CollectionUtils.isEmpty(sheets) || Strings.isStringEmpty(exportPath)) {
return;
}
InputStream in = ExcelUntil.class.getResourceAsStream(templatePath);
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);
for (int i = 0; i < sheets.size(); i++) {
HSSFSheet sheet = workbook.getSheetAt(i);
sheet.setForceFormulaRecalculation(true);
List<Cells> cells = sheets.get(i).getCells();
cellSetValue(sheet, cells);
}
FileOutputStream out = new FileOutputStream(exportPath);
workbook.write(out);
out.close();
} public static HSSFWorkbook exportExcel(String templatePath, List<Sheet> sheets, String exportPath)
throws Exception {
InputStream in = ExcelUntil.class.getResourceAsStream(templatePath);
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);
for (int i = 0; i < sheets.size(); i++) {
HSSFSheet sheet = workbook.getSheetAt(i);
sheet.setForceFormulaRecalculation(true);
List<Cells> cells = sheets.get(i).getCells();
cellSetValue(sheet, cells);
}
return workbook; } /**
* 设置具体单元格的值
* @param sheet 具体表空间
* @param cells 设置的具体的单元格
*/
private static void cellSetValue(HSSFSheet sheet, List<Cells> cells) {
// 设置数据行列并设置值
Cells c = null;
Object val = null;
HSSFCell cell = null;
for (int i = 0; i < cells.size(); i++) {
c = cells.get(i);
if(c==null) {
continue;
}
val = c.getVal();
cell = sheet.getRow(c.getRow() - 1).getCell(c.getColumn() - 1);
if (val instanceof Integer) {
cell.setCellValue((Integer) val);
} else if (val instanceof Double) {
cell.setCellValue((Double) val);
} else if (val instanceof Date) {
cell.setCellValue((Date) val);
} else if (val instanceof Short) {
cell.setCellValue((short) val);
} else {
cell.setCellValue(val + "");
}
}
}
}

poi根据excel模板导出Excel的更多相关文章

  1. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  2. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  3. 14. java基于excel模板导出excel=>使用jxls最新版(注意点)

    注意点:如下: jxls官网:http://jxls.sourceforge.net/getting_started.html

  4. NPOI复制模板导出Excel

    本人菜鸟实习生一枚,公司给我安排了一个excel导出功能.要求如下:1.导出excel文件有样式要求:2.导出excel包含一个或多个工作表:3.功能做活(我的理解就是excel样式以后可能会变方便维 ...

  5. kettle 使用excel模板导出数据

    通过excel进行高速开发报表: 建设思路: 1.首先制订相关的execl模板. 2.通过etl工具(kettle)能够高速的 将数据库中的数据按excel模板导出成新的excel就可以. 当中ket ...

  6. POI通过模板导出EXCEL文件

    一般的EXCEL导出使用POI先创建一个HSSFWorkbook,然后通过不断创建HSSFRow,HSSFCell后设置单元格内容便可以完成导出. 这次在项目中需要用到模板,导出的内容包括(1.模板中 ...

  7. apache poi根据模板导出excel

    需要预先新建编辑好一个excel文件,设置好样式. 编辑好输出的数据,根据excel坐标一一对应. 支持列表数据输出,列表中列合并. 代码如下: package com.icourt.util; im ...

  8. java实现excel模板导出

    一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...

  9. Java无模板导出Excel,Apache-POI插件实现

    开发环境 jdk 1.8 Maven 3.6 Tomcat 8.5 SpringBoot 2.1.4.RELEASE Apache-POI 3.6 Idea 注意: 我是在现有的基于SpringBoo ...

随机推荐

  1. iOS7的十个更“佳”:简洁直观更受青睐

    转自:http://www.25pp.com/news/news_27792.html iOS7自发布以来一直是褒贬不一,虽然苹果还只是发布了第二个测试版,但普通用户早已经在纠结到底该不该升级iOS7 ...

  2. django 开发Broken pipe from ('127.0.0.1', 58078)问题解决

    最近写的一个项目,前端使用了表单submit提交,后端接收POST数据存储.实际上的逻辑并不复杂, django接收到的时候会产生Broken pipe from ('127.0.0.1', 5807 ...

  3. android Gradle下载慢,使用阿里镜像

    在Project下的 build.gradle添加阿里镜像 buildscript { repositories { maven{ url 'http://maven.aliyun.com/nexus ...

  4. 使用gunicorn部署python web

    gunicorn 是一款支持wsgi的web服务器, 支持gevent 首先安装setuptools.  wget https://bootstrap.pypa.io/ez_setup.py $pyt ...

  5. properties文件属性值通过xml文件为 java entity属性赋值

    一.通过xml配置文件进行赋值: 举个栗子,一目了然: 1.1 properties文件: 1.2 xml配置文件,将properties属性与java entity属性相关联:(这是一个新建的xml ...

  6. php静态化介绍

    1.动态URL地址设置成静态形式http://state.com/index.php?c=play&a=index&id=16267 ------>http://state.co ...

  7. CEF 3.2623使用flash插件的方法

    PPAPI Flash插件是Chrome浏览器内置的Flash插件,是Google和Adobe合作的产物,于Chrome21(Win)或者Chrome20(Linux)加入,具有沙箱.GPU加速.多进 ...

  8. 18-C#笔记-继承

    1. 子类可以使用父类的成员和函数. 和C++不同,使用的是一个冒号 2. 不支持多重继承 但是可以通过接口(interface)这种结构实现.后续讲解. using System; namespac ...

  9. python scapy中sniffer的用法以及过滤器

    Sniff方法定义: sniff(filter="",iface="any", prn=function, count=N) 1.filter的规则使用 Ber ...

  10. crystalreport使用方法

    使用: 打开CrystalReport官网下载页 目前最新版本为13.0.4 选择“SAP Crystal Reports, version for Visual Studio 2010 - Stan ...