public class ExcelUtils {

    private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = "download\\template\\materialList.xlsx";
private static XSSFCellStyle cellstyle = null; public static void exportBom(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException {
//模板的路径,这个在自己的项目中很容易弄错,相对位置一定要写对啊
String psth = request.getRealPath("/") + INSPECTIONRECORD_SURFACE_TEMPLET_PATH;
Workbook webBook = readExcel(psth);
Sheet sheet = webBook.getSheetAt(0);
//调用样式方法
cellstyle = createCellStyle(webBook);
sheet.setColumnWidth(0, 3766);//列宽
sheet.setColumnWidth(1, 4766);//列宽
//开始操作模板,找到某行某列(某个cell),需要注意的是这里有个坑,行和列的计数都是从0开始的
//一次数据插入的位置不对,别灰心,多试几次就好啦,你要是能看懂我下面的代码,数据插在了什么位置,你就明白了
//打印时间
int rows = 0;
Row row = sheet.getRow(rows);
//调用方法,赋值并给定样式(写样式的时候这个工具类只能用XSSF而不能用HSSF,不然样式会没响应)
setCellStyleWithValue(row.createCell(0),(String) map.get("title")); rows = 2;
row = sheet.getRow(rows);
row.createCell(1).setCellValue((String) map.get("date"));
// setCellStyleWithValue(row.createCell(1),(String) map.get("date")); //负责人
rows = 3;
row = sheet.getRow(rows);
row.createCell(1).setCellValue((String) map.get("leader"));
// setCellStyleWithValue(row.createCell(1),(String) map.get("leader")); //审核人
rows = 4;
row = sheet.getRow(rows);
row.createCell(1).setCellValue((String) map.get("Auditleader"));
// setCellStyleWithValue(row.createCell(1),(String) map.get("Auditleader")); //在调用模板的时候,数据的插入不能直接应用模板行,不然数据会覆盖掉哪行
rows = 7;
row = sheet.createRow(rows);
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("名称");
row.createCell(2).setCellValue("规格");
row.createCell(3).setCellValue("数量");
row.createCell(4).setCellValue("益值");
row.setRowStyle(cellstyle); List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("resultList");
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(rows++);
/* row.createCell(0).setCellValue(StringUtils.objToStr(list.get(i).get("物料编号")));
row.createCell(1).setCellValue(StringUtils.objToStr(list.get(i).get("物料名称")));
row.createCell(2).setCellValue(StringUtils.objToStr(list.get(i).get("物料规格")));
row.createCell(3).setCellValue(Double.parseDouble(StringUtils.objToStr(list.get(i).get("物料消耗数量"))));
row.createCell(4).setCellValue(Float.parseFloat(StringUtils.objToStr(list.get(i).get("物料消耗损益值"))));*/ setCellStyleWithValue(row.createCell(0),StringUtils.objToStr(list.get(i).get("物料编号")));
setCellStyleWithValue(row.createCell(1),StringUtils.objToStr(list.get(i).get("物料名称")));
setCellStyleWithValue(row.createCell(2),StringUtils.objToStr(list.get(i).get("物料规格")));
setCellStyleWithValue(row.createCell(3),Double.parseDouble(StringUtils.objToStr(list.get(i).get("物料消耗数量"))));
setCellStyleWithValue(row.createCell(4),Float.parseFloat(StringUtils.objToStr(list.get(i).get("物料消耗损益值")))); }
writeExcel(response, webBook, (String) map.get("title"));
} private static XSSFWorkbook readExcel(String filePath) {
InputStream in = null;
XSSFWorkbook work = null;
try {
in = new FileInputStream(filePath);
work = new XSSFWorkbook(in);
} catch (FileNotFoundException e) {
System.out.println("文件路径错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件输入流错误");
e.printStackTrace();
}
return work;
} private static void writeExcel(HttpServletResponse response, Workbook work, String fileName) throws IOException {
OutputStream out = null;
try {
out = response.getOutputStream();
response.setContentType("application/ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName + ".xls", "UTF-8"))));
work.write(out);
} catch (IOException e) {
System.out.println("输出流错误");
e.printStackTrace();
} finally {
out.close();
}
} private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, String value) {
cell.setCellStyle(style);
cell.setCellValue(value);
return cell;
} private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, Double value) {
cell.setCellStyle(style);
cell.setCellValue(value);
return cell;
} private static Cell setCellStyleWithValue(Cell cell, String value) {
cell.setCellStyle(cellstyle);
cell.setCellValue(value);
return cell;
} private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, RichTextString value) {
cell.setCellStyle(style);
cell.setCellValue(value);
return cell;
} private static Cell setCellStyleWithValue(Cell cell, int value) {
cell.setCellStyle(cellstyle);
cell.setCellValue(value);
return cell;
} private static Cell setCellStyleWithValue(Cell cell, double value) {
cell.setCellStyle(cellstyle);
cell.setCellValue(value);
return cell;
} private static XSSFCellStyle createCellStyle(Workbook wb) {
cellstyle = (XSSFCellStyle) wb.createCellStyle();
cellstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// cellstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
// cellstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
// cellstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
// cellstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellstyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
return cellstyle;
}
} 我自己的空模板,有数据的图就不贴出来了!

java后台poi根据模板导出excel的更多相关文章

  1. apache poi根据模板导出excel

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

  2. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

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

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

  4. Java利用POI实现导入导出Excel表格示例代码

    转自:https://www.jb51.net/article/95526.htm 介绍 Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其 ...

  5. JAVA 通过POI 模版,导出excel

    如有不足,欢迎指正,谢谢 ! 1.Maven引入  POI jar包.模版和结果文件.rar下载 <dependency> <groupId>org.apache.poi< ...

  6. java使用freemarker作为模板导出Excel表格

    1:首先新建一个excel表格自己弄好格式如下图 2:把excel 表格另存为xml格式文件如下图 3:这个时候的文件就是xml 格式的文件了,在myeclipse里面项目工程里面新建一个文件后缀为. ...

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

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

  8. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  9. 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的路由方案,与原来的方案在使用上差别不 ...

随机推荐

  1. css实现高度垂直居中

    1:单行文字垂直居中: 如果一个容器中只有一行文字的话,定义height(高度)和 line-height(行高)相等即可. 如:<div style="height:25px;lin ...

  2. 虚拟机扩容(/dev/mapper/centos-root 空间不足)

    1:.首先查看我们的根分区大小是多少 df -h 文件系统                类型      容量  已用  可用 已用% 挂载点 /dev/mapper/centos-root xfs  ...

  3. [Android]对字符串进行MD5加密

    /** * 将字符串转成MD5值 * * @param string * @return */ public static String stringToMD5(String string) { by ...

  4. [PHP]memcache安装

    1.memcached 安装sudo apt-get install memcached memcached 参数说明memcached -d -m 50 -p 11211 -u root-m 指定使 ...

  5. 比特币--私钥->公钥->钱包地址

    脑钱包-字符串SHA256散列成256bit当做私钥-彩虹表 脑钱包是开源的程序-->用户输入字符串当做密码-->经过SHA256散列算法-->256长bit位 (即是私钥)--&g ...

  6. WPF的组成架构

    Windows呈现基础(Windows Presentation foundation,WPF)是微软新一代图形系统,运行,NET Framework3.0架构下,为用户界面.2D/3D图形.文档和媒 ...

  7. iis部署网站打不开

    事件查看器 1.crypt32报错 2.ASP.NET 2.0错误 解决办法: 1.crpt32报错,根本原因是计算机无法连接更新根证书所致,可能造成死循环. [1]必须连接到 Internet 或关 ...

  8. Props 和 IActorRef 3

    在Actor模式中我们使用IActorRef通过ActorSystem来发送消息数据.这么做有两个理由 1.ActorSystem会在消息中封装一些元数据(metadata),这些数据我们可以通过上下 ...

  9. iOS开发实践之多线程(单例模式)

    单例模式的作用:可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问,从而方便地控制了实例个数,并节约系统资源. 单例模式的使用场合:在这个应用程序中,共享一份资源(这份资源只需要创建 ...

  10. 关于hibernate字段映射@colunm出现的问题以及jpa驼峰大写转_小写的问题探究

    关于hibernate字段映射@colunm出现的问题以及jpa驼峰大写转_小写的问题探究2018年04月24日 15:47:26 守望dfdfdf 阅读数:735 标签: @colunmhibern ...