项目中很多模块需要将数据导出到Excel中,简单的如导出所有的用户信息到Excel,对于导出这种数据,可以通过POI/JXL直接生成Excel即可;当面临复杂格式的报表怎么办呢?下面这张图是项目中某张需要导出的Excel模版中部分截图,需求是根据查询条件生成N张同类型的这种单子到Excel中的不同Sheet中,后期要打印成纸质单据!需要明确有几个重点是:需要在A4纸中打印,所有打印出来的格式必须一样,可能后期这张表还会进行调整,必须满足其高扩展和变化性;
事实上我们依然可以通过程序将Excel写出来,事实上开始我确实也是这么做了,但是在做的过程中发现,要手写代码生成这种几十行的Excel实在是一个很大的工作量,尤其是对样式的控制更是繁琐;而且用程序生拼单元格生成Sheet不能正常打印到A4纸中,最后果断放弃,事实证明这个选择是正确的!
好吧,说了这么多总算进入正题,最后我选择了通过程序Copy整个Sheet的方式,采用通过这种实现方式,我只需要相关人员给我提供设置好模版,在批量生成的时候先通过Copy的方式生成新的Sheet,然后在向指定的Sheet中填写相关数据即可,而且当模版发生变化时只需调整Sheet中写值的代码即可!
重点是两个方法:copySheet和copyRow,主要代码如下:
package org.pihai.utils.excel.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
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.Region;
/**
* POI操作Excel
* @since: 1.0
* @version:1.0
* @createTime:2011年05月21日 16:12:15
* @updateTime:2011年07月21日 10:47:20
* @author niejy niejingyu@gmail.com
*/
public class PoiExcelSheetCopy {
/**
* 根据源Sheet样式copy新Sheet
* @param fromsheetname
* @param newsheetname
* @param targetFile
*/
public void copySheet(String fromsheetname, String newsheetname,String targetFile) {
HSSFWorkbook wb = null;
try {
FileInputStream fis = new FileInputStream(targetFile);
wb = new HSSFWorkbook(fis);
HSSFSheet fromsheet = wb.getSheet(fromsheetname);
if (fromsheet != null && wb.getSheet(newsheetname) == null) {
HSSFSheet newsheet = wb.createSheet(newsheetname);
//设置打印参数
newsheet.setMargin(HSSFSheet.TopMargin,fromsheet.getMargin(HSSFSheet.TopMargin));// 页边距(上)
newsheet.setMargin(HSSFSheet.BottomMargin,fromsheet.getMargin(HSSFSheet.BottomMargin));// 页边距(下)
newsheet.setMargin(HSSFSheet.LeftMargin,fromsheet.getMargin(HSSFSheet.LeftMargin) );// 页边距(左)
newsheet.setMargin(HSSFSheet.RightMargin,fromsheet.getMargin(HSSFSheet.RightMargin));// 页边距(右
HSSFPrintSetup ps = newsheet.getPrintSetup();
ps.setLandscape(false); // 打印方向,true:横向,false:纵向(默认)
ps.setVResolution((short)600);
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
File file = new File(targetFile);
if (file.exists() && (file.renameTo(file))) {
copyRows(wb, fromsheet, newsheet, fromsheet.getFirstRowNum(), fromsheet.getLastRowNum());
FileOutputStream fileOut = new FileOutputStream(targetFile);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} else {
System.out.println(“文件不存在或者正在使用,请确认…”);
}
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拷贝Excel行
* @param wb
* @param fromsheet
* @param newsheet
* @param firstrow
* @param lastrow
*/
public void copyRows(HSSFWorkbook wb, HSSFSheet fromsheet,HSSFSheet newsheet, int firstrow, int lastrow) {
if ((firstrow == -1) || (lastrow == -1) || lastrow < firstrow) {
return;
}
// 拷贝合并的单元格
Region region = null;
for (int i = 0; i < fromsheet.getNumMergedRegions(); i++) {
region = fromsheet.getMergedRegionAt(i);
if ((region.getRowFrom() >= firstrow)&& (region.getRowTo() <= lastrow)) {
newsheet.addMergedRegion(region);
}
}
HSSFRow fromRow = null;
HSSFRow newRow = null;
HSSFCell newCell = null;
HSSFCell fromCell = null;
// 设置列宽
for (int i = firstrow; i <= lastrow; 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 <= lastrow; 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.setCellFormula(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;
}
}
}
}
public static void main(String[] args) {
PoiExcelSheetCopy ew = new PoiExcelSheetCopy();
ew.copySheet(“template”, “test2″, “d:/template.xls”);
}
}
转自:http://www.pihai.me/archives/882.html#header
分享到:
相关推荐
变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图恒温烘房电气控制柜原理图120KW
使用R语言的jiebaR包的情感分析_jiebaR_emotion
springboot基于双因素身份认证的学生社团管理系统论文
鳄梨酱guacamole:1.5.5和guacd:1.5.5容器
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。 替换数据可以直接使用,注释清楚,适合新手
小程序&小工具类&生活圈(源码+截图+源码导入教程和视频).zip
记录r语言单细胞学习_Single-cell
本项目是基于Javascript开发的微信小程序电影资讯平台,包含44个文件,其中包括10个JavaScript脚本文件、9个JSON配置文件、8个WXSS样式表文件、6个WXML页面文件、4个PNG图像文件、1个EOT字体文件、1个SVG矢量图文件、1个TTF字体文件、1个WOFF字体文件和1个WOFF2字体文件。该项目旨在为用户提供一个便捷、高效的电影资讯浏览和搜索平台,支持电影信息展示、分类查询、用户评价等功能,以提升用户体验和互动性。
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。 替换数据可以直接使用,注释清楚,适合新手
变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图控制室平面布置图
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。 替换数据可以直接使用,注释清楚,适合新手
本项目是基于Java开发的飞滴出行网约车2022,包含81个文件,其中包括52个Java源代码文件、11个XML配置文件、8个gitignore文件、5个YAML配置文件、3个HTML页面文件和2个Markdown文档文件。该项目旨在为用户提供一个便捷、高效的网约车服务平台,支持乘客下单、司机接单、行程跟踪等功能,以提升用户体验和运营效率。
NBA决策树R语言实践_NBA-decision-tree
解决oracle退格键乱码问题
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。 替换数据可以直接使用,注释清楚,适合新手
一个R语言的数据分析及可视化_R_Language_analysis_visualization
STM32F4使用通用定时器生成PWM和脉冲(只有干货、注释详细、附程序包)
包含 1)系统的文档教程 2)实例讲义 3)模型源文件 DesignModeler(简称DM)是ANSYS Workbench中的一个重要模块,它主要用于几何模型的创建、修复、简化和前处理。以下是对DesignModeler的详细介绍: 一、主要功能 全参数化实体建模:DesignModeler支持全参数化建模,用户可以通过设置参数来控制模型的尺寸和形状,便于后续的优化分析和数据传递。 CAD模型修复与简化:从CAD软件导入的模型可能存在细节缺失或错误,DesignModeler提供了多种工具来修复这些问题,并可以对模型进行简化,以提高计算效率。 概念建模:DesignModeler还具备概念建模功能,如创建梁模型或面(壳体)概念模型,为后续的有限元分析提供基础。 前处理功能:包括对几何模型的修改、分割、中面抽取等,为后续的网格划分和仿真分析做好准备。
华为杯《统计学习方法-李航》200页PDF手推公式细节讲解,包含详细的目录以及R语言代码实现,可结合《统_statistic-learning-R-note.zip