


Java exports an Excel report based on a template and copies the template to generate multiple Sheet pages. Sample code sharing (picture)
本文主要介绍了Java根据模板导出Excel报表并复制模板生成多个Sheet页的方法,具有很好的参考价值。下面跟着小编一起来看下吧
因为最近用报表导出比较多,所有就提成了一个工具类,本工具类使用的场景为 根据提供的模板来导出Excel报表
并且可根据提供的模板Sheet页进行复制 从而实现多个Sheet页的需求,
使用本工具类时,如果需求是每个Sheet页中的数据都不一致,但是表格样式和模板都一样
那么只需要在实际情况中根据 sql 来查询要添加的数据源 (只需更改数据源即可)
采用的技术为 POI 导出,因为类的缘故,目前只支持2003版本的Excel.
使用前请先下载相应jar包!
后期有时间的话会进行进一步完善,初次写工具类,若有不完善的地方还请包涵!
先看看模板样式和运行结果,然后直接上代码
这是Excel的模板样式
这是导出结果
具体实现看demo
package com.sgcc.xyz.util; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.Region; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import com.sgcc.uap.persistence.impl.HibernateDaoImpl; /** * 报表导出工具类 * * @author JYLiu @巴黎的雨季 本工具是根据POI对Excel2003进行报表导出 本工具类可根据模板进行Excel的导出 * 并且可根据提供的模板Sheet页进行复制 从而实现多个Sheet页的需求 * 使用本工具类时,如果需求是每个Sheet页中的数据都不一致,但是表格样式和模板都一样 * 那么只需要在实际情况中根据 sql 来查询要添加的数据源 (只需更改数据源即可) */ public class ExcelUtil { /** * 根据模板导出报表,可导出多个Sheet页 * * @param 导出的Excel文件名 * @param 模板路径 (全路径) * @param 数据源 * @param 返回请求 * @param 生成的Sheet页的名称集合 * @param 数据源中Map集合的key值 (key值对应的value值顺序要列名顺序一致) * @param 开始 循环写入数据 的行数(从第几行开始写入数据) */ public static void ExcelByModel(String ExcelName, String ModelURl, List<Map<String, String>> dataSource, HttpServletResponse response, String[] sheetNames, String[] keyNames, int rowNum) throws Exception { // 设置导出Excel报表的导出形式 response.setContentType("application/vnd.ms-excel"); // 设置导出Excel报表的响应文件名 String fileName = new String(ExcelName.getBytes("utf-8"), "ISO-8859-1"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls"); // 创建一个输出流 OutputStream fileOut = response.getOutputStream(); // 读取模板文件路径 File file = new File(ModelURl); FileInputStream fins = new FileInputStream(file); POIFSFileSystem fs = new POIFSFileSystem(fins); // 读取Excel模板 HSSFWorkbook wb = new HSSFWorkbook(fs); // 设置边框样式 HSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 设置边框样式的颜色 style.setBottomBorderColor(HSSFColor.BLACK.index); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setRightBorderColor(HSSFColor.BLACK.index); style.setTopBorderColor(HSSFColor.BLACK.index); // 模板页 HSSFSheet sheetModel = null; // 新建的Sheet页 HSSFSheet newSheet = null; // 创建行 HSSFRow row = null; // 创建列 HSSFCell cell = null; // 循环建立Sheet页 for (int i = 0; i < sheetNames.length; i++) { // 读取模板中模板Sheet页中的内容 sheetModel = wb.getSheetAt(0); // 设置新建Sheet的页名 newSheet = wb.createSheet(sheetNames[i]); // 将模板中的内容复制到新建的Sheet页中 copySheet(wb, sheetModel, newSheet, sheetModel.getFirstRowNum(), sheetModel.getLastRowNum()); //获取到新建Sheet页中的第一行为其中的列赋值 row=newSheet.getRow(0); row.getCell(1).setCellValue("这是为表代码赋的值"); //注意 合并的单元格也要按照合并前的格数来算 row.getCell(6).setCellValue("这是为外部代码赋的值"); //获取模板中的第二列,并赋值 row=newSheet.getRow(1); row.getCell(1).setCellValue("表名称赋值"); //注意 合并的单元格也要按照合并前的格数来算 row.getCell(6).setCellValue("这是为是否系统表赋的值"); // 遍历数据源 开始写入数据(因为Excel中是从0开始,所以减一) int num = rowNum - 1; for (Map<String, String> item : dataSource) { // 循环遍历,新建行 row = newSheet.createRow((short) num); //判断有多少列数据 for (int j = 0; j < keyNames.length; j++) { // 设置每列的数据 设置每列的样式 设置每列的值 cell = row.createCell(j); cell.setCellStyle(style); cell.setCellValue(item.get(keyNames[j])); } num++; } // break 加break可以测试只添加一个Sheet页的情况 } // 写入流 wb.write(fileOut); // 关闭流 fileOut.close(); } /** * * @param Excel工作簿对象 * @param 模板Sheet页 * @param 新建Sheet页 * @param 模板页的第一行 * @param 模板页的最后一行 */ private static void copySheet(HSSFWorkbook wb, HSSFSheet fromsheet, HSSFSheet newSheet, int firstrow, int lasttrow) { // 复制一个单元格样式到新建单元格 if ((firstrow == -1) || (lasttrow == -1) || lasttrow < firstrow) { return; } // 复制合并的单元格 Region region = null; for (int i = 0; i < fromsheet.getNumMergedRegions(); i++) { region = fromsheet.getMergedRegionAt(i); if ((region.getRowFrom() >= firstrow) && (region.getRowTo() <= lasttrow)) { newSheet.addMergedRegion(region); } } HSSFRow fromRow = null; HSSFRow newRow = null; HSSFCell newCell = null; HSSFCell fromCell = null; // 设置列宽 for (int i = firstrow; i < lasttrow; i++) { fromRow = fromsheet.getRow(i); if (fromRow != null) { for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) { int colnum = fromsheet.getColumnWidth((short) j); if (colnum > 100) { newSheet.setColumnWidth((short) j, (short) colnum); } if (colnum == 0) { newSheet.setColumnHidden((short) j, true); } else { newSheet.setColumnHidden((short) j, false); } } break; } } // 复制行并填充数据 for (int i = 0; i < lasttrow; i++) { fromRow = fromsheet.getRow(i); if (fromRow == null) { continue; } newRow = newSheet.createRow(i - firstrow); newRow.setHeight(fromRow.getHeight()); for (int j = fromRow.getFirstCellNum(); j < fromRow.getPhysicalNumberOfCells(); j++) { fromCell = fromRow.getCell((short) j); if (fromCell == null) { continue; } newCell = newRow.createCell((short) j); newCell.setCellStyle(fromCell.getCellStyle()); int cType = fromCell.getCellType(); newCell.setCellType(cType); switch (cType) { case HSSFCell.CELL_TYPE_STRING: newCell.setCellValue(fromCell.getRichStringCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: newCell.setCellValue(fromCell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: newCell.setCellValue(fromCell.getCellFormula()); break; case HSSFCell.CELL_TYPE_BOOLEAN: newCell.setCellValue(fromCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: newCell.setCellValue(fromCell.getErrorCellValue()); break; default: newCell.setCellValue(fromCell.getRichStringCellValue()); break; } } } } }
以上便是整个工具类的核心代码了
测试数据如下
/** * 测试多Sheet页导出数据表格方法 */ public static void ExcelTest(HttpServletResponse response){ //构建数据源 List<Map<String, String>> dataSourceList=new ArrayList<Map<String,String>>(){ { add(new HashMap<String, String>(){{ put("字段编号", "1"); put("字段代码", "BUSINESS_ID"); put("字段含义", "业务id"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", "是"); put("主码", ""); }}); add(new HashMap<String, String>(){{ put("字段编号", "2"); put("字段代码", "PROC_INST_ID"); put("字段含义", "流程实例编号"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", ""); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "3"); put("字段代码", "PROC_STATE"); put("字段含义", "流程状态"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "4"); put("字段代码", "APPLICANT"); put("字段含义", "申请人"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "5"); put("字段代码", "LEAVE_TYPE"); put("字段含义", "请假类型"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "6"); put("字段代码", "REASON"); put("字段含义", "请假事因"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "7"); put("字段代码", "BEGIN_TIME"); put("字段含义", "起始时间"); put("数据类型", "TIMESTAMP"); put("长度", ""); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "8"); put("字段代码", "END_TIME"); put("字段含义", "结束时间"); put("数据类型", "TIMESTAMP"); put("长度", ""); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "9"); put("字段代码", "INSERT_PERSON"); put("字段含义", "登记人"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", " "); put("主码", " "); }}); add(new HashMap<String, String>(){{ put("字段编号", "10"); put("字段代码", "APPROVEDBY"); put("字段含义", "批准人"); put("数据类型", "VARCHAR"); put("长度", "64"); put("主键", " "); put("主码", " "); }}); } }; //构建数据源中的key值 String[] keysStrings={"字段编号","字段代码","字段含义","数据类型","长度","主键","主码"}; //每页的名称 String [] sheetNameStrings={"Sheet1","Sheet2","Sheet3","Sheet4","Sheet5","Sheet6"}; String modelURLString="D:\\model\\model.xls"; try { ExcelUtil.ExcelByModel("测试模板导出", modelURLString, dataSourceList, response, sheetNameStrings, keysStrings, 6); } catch (Exception e) { e.printStackTrace(); } }
The above is the detailed content of Java exports an Excel report based on a template and copies the template to generate multiple Sheet pages. Sample code sharing (picture). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
