java excel导出(表头合并,多行表头)
@RequestMapping(value="orderExcelList2")
public void orderExcelList2forJava(Order order,HttpServletResponse response){
Map<String,Object> map = toOrderExcelJSONList2(order);
List<Bottle> bottleList = (List<Bottle>) map.get("bottleList");
List<Order> orderList = (List<Order>) map.get("orderList");
List<Order> backOrderList = (List<Order>) map.get("backOrderList");
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet();
String[] head0 = new String[]{"订单号","收货人","电话","固话","地址","下单时间","完成时间","所属店",
"出货品牌数/18.9L","出货品牌数/18.9L","出货品牌数/18.9L","出货品牌数/18.9L","出货品牌数/18.9L",
"出货品牌数/18.9L","出货品牌数/18.9L","出货品牌数/18.9L","出货品牌数/18.9L","出货品牌数/18.9L",
"价格","类型","协议号",
"付款","付款","付款","付款","付款","送气工","送气工","租金","用户类型","优惠券","红包","红包","红包","备注"};
String[] headnum0 = new String[] { "1,3,0,0", "1,3,1,1", "1,3,2,2","1,3,3,3","1,3,4,4","1,3,5,5","1,3,6,6","1,3,7,7",
"1,1,8,17", "1,3,18,18", "1,3,19,19","1,3,20,20","1,1,21,25","1,1,26,27","1,3,28,28","1,3,29,29","1,3,30,30","1,1,31,33","1,3,34,34" }; //对应excel中的行和列,下表从0开始{"开始行,结束行,开始列,结束列"}
String[] head1 = new String[]{"10KG/瓶数","单价","15KG/瓶数","单价","2KG/瓶数","单价","5KG/瓶数","单价","50KG/瓶数","单价"};
String[] headnum1 = new String[] { "2,3,8,8", "2,3,9,9", "2,3,10,10","2,3,11,11","2,3,12,12","2,3,13,13","2,3,14,14","2,3,15,15","2,3,16,16","2,3,17,17"};
String[] head1_2 = new String[]{"收现","收现","收现","收现","欠款","姓名","楼层费"};
String[] headnum1_2 = new String[] { "2,2,21,24","2,2,25,25","2,3,26,26","2,3,27,27"};
String[] head1_3 = new String[]{"微支付","现金","押金","其它","账期"};
String[] headnum1_3 = new String[] { "3,3,21,21","3,3,22,22","3,3,23,23","3,3,24,24","3,3,25,25"};
String[] head1_4 = new String[]{"金额","接收人","状态"};
String[] headnum1_4 = new String[] { "2,3,31,31","2,3,32,32","2,3,33,33"};
// 表头标题样式
HSSFFont headfont = book.createFont();
headfont.setFontName("宋体");
headfont.setFontHeightInPoints((short) 22);// 字体大小
HSSFCellStyle headstyle = book.createCellStyle();
headstyle.setFont(headfont);
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle.setLocked(true);
// 表头标题样式2
HSSFFont headfont2 = book.createFont();
headfont.setFontName("宋体");
headfont.setFontHeightInPoints((short) 15);// 字体大小
HSSFCellStyle headstyle2 = book.createCellStyle();
headstyle2.setFont(headfont2);
headstyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle2.setLocked(true);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, head0.length-1));
HSSFRow row = sheet.createRow(0); //第一行
row.setHeight((short) 0x200);
HSSFCell cell = row.createCell(0);
cell.setCellValue("订单详情");
cell.setCellStyle(headstyle);
row = sheet.createRow(1); //第二行
for(int i =0;i < head0.length;i++){
cell = row.createCell(i);
cell.setCellStyle(headstyle2);
cell.setCellValue(head0[i]);
}
row = sheet.createRow(2); //第三行
for (int i = 0; i < head0.length; i++) {
if (i > 1 && i < 18) {
for (int j = 0; j < head1.length; j++) {
cell = row.createCell(j + 8); //在8起步所以+8
cell.setCellValue(head1[j]);
cell.setCellStyle(headstyle2);
}
}
if(i > 21 && i < 27){ //大于起步,小于止步
for (int j = 0; j < head1_2.length; j++) {
cell = row.createCell(j + 21); ///在22起步所以+21
cell.setCellValue(head1_2[j]);
cell.setCellStyle(headstyle2);
}
}
if(i > 28 && i < 34){ //大于起步,小于止步
for (int j = 0; j < head1_4.length; j++) {
cell = row.createCell(j + 31);
cell.setCellValue(head1_4[j]);
cell.setCellStyle(headstyle2);
}
}
}
row = sheet.createRow(3); //第四行
for (int i = 0; i < head0.length; i++) {
if(i > 21 && i < 25){ //大于起步,小于止步
for (int j = 0; j < head1_3.length; j++) {
cell = row.createCell(j + 21); ///在21起步所以+21
cell.setCellValue(head1_3[j]);
cell.setCellStyle(headstyle2);
}
}
}
//动态合并单元格
for (int i = 0; i < headnum0.length; i++) {
String[] temp = headnum0[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//动态合并单元格
for (int i = 0; i < headnum1.length; i++) {
String[] temp = headnum1[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//动态合并单元格
for (int i = 0; i < headnum1_2.length; i++) {
String[] temp = headnum1_2[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//动态合并单元格
for (int i = 0; i < headnum1_3.length; i++) {
String[] temp = headnum1_3[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//动态合并单元格
for (int i = 0; i < headnum1_4.length; i++) {
String[] temp = headnum1_4[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int count = 4; //从第五行开始绑数据
for (Order order1 : orderList) {
row = sheet.createRow(count);
row.createCell(0).setCellValue(order1.getOrderNo());
row.createCell(1).setCellValue(order1.getName());
row.createCell(2).setCellValue(order1.getPhone());
row.createCell(3).setCellValue(order1.getFixedline());
row.createCell(4).setCellValue(order1.getAddress());
if(order1.getCreateTime()!=null)
row.createCell(5).setCellValue(sdf.format(order1.getCreateTime()));
if(order1.getEndTime()!=null)
row.createCell(6).setCellValue(sdf.format(order1.getEndTime()));
row.createCell(7).setCellValue(order1.getStoreName());
List<OrderGas> gasOrderList = order1.getGasOrderList();
int j = 8;
for(int i=0;i<gasOrderList.size();i++){
if (gasOrderList.get(i).getNumber() != 0) {
row.createCell(j).setCellValue(gasOrderList.get(i).getNumber());
}
if (gasOrderList.get(i).getPrice() != 0) {
// double d = gasOrderList.get(i).getPrice() * 0.01;
// int te = Integer.parseInt(String.valueOf(d));
row.createCell(j+1).setCellValue(gasOrderList.get(i).getPrice() * 0.01 + "元/瓶");
}
j+=2;
}
row.createCell(7).setCellValue(order1.getSpec());
row.createCell(8).setCellValue(order1.getStoreName());
row.createCell(9).setCellValue(order1.getStoreName());
row.createCell(10).setCellValue(order1.getStoreName());
row.createCell(11).setCellValue(order1.getStoreName());
row.createCell(12).setCellValue(order1.getStoreName());
row.createCell(13).setCellValue(order1.getStoreName());
row.createCell(14).setCellValue(order1.getStoreName());
row.createCell(15).setCellValue(order1.getStoreName());
row.createCell(16).setCellValue(order1.getStoreName());
row.createCell(17).setCellValue(order1.getStoreName());
row.createCell(18).setCellValue(order1.getStoreName());
row.createCell(19).setCellValue(order1.getStoreName());
row.createCell(20).setCellValue(order1.getStoreName());
row.createCell(21).setCellValue(order1.getStoreName());
row.createCell(22).setCellValue(order1.getStoreName());
row.createCell(23).setCellValue(order1.getStoreName());
count++;
}
private static void out(HSSFWorkbook book, HttpServletResponse response) {
try {
response.addHeader("Content-Disposition", "attachment;filename=" + new String((new Date().getTime() +".xls").getBytes()));
response.setContentType("application/octet-stream");
book.write(response.getOutputStream());
} catch (Exception e) {
logger.error("导出出错", e);
}
}
java excel导出(表头合并,多行表头)的更多相关文章
- C# 使用Epplus导出Excel [4]:合并指定行
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- DevExpress GridControl复合表头(多行表头)设置
关于DevExpress.XtraGrid的复合表头或多行表头的示例,界面如下图所示 1.首先要把DevExpress的GridControl转换为BandedGridView 2.设置显示列及绑定的 ...
- java excel导出(基于注解)
小白,做日志只是为了方便自己查看,能帮到别人当然更好,不喜勿喷. 上代码 依赖: <dependency> <groupId>org.apache.poi</groupI ...
- WPF DataGrid 复合表头 (实现表头合并,自定义表头)
功能说明: 将 DataGrid嵌套在本控件内,使用Label自定义表头,如果需要上下左右滚动 需要在控件外围添加 ScrollViewer 并且设置 ScrollVisibility 为Auto ...
- 三步轻松搞定delphi中CXGRID手动添加复表头(多行表头,报表头)
网上有代码动态生成cxgrid多行表头的源码,地址为:http://mycreature.blog.163.com/blog/static/556317200772524226400/ 如果要手动设计 ...
- asp.net GridView实现多表头类 多行表头实现方法
以上列表中运用的都是基本的东东: 1.多表头: 2.按值改变行颜色: 3.分页码 代码: AndyGridViewTHeaderHepler.cs //------------------------ ...
- 自己写的java excel导出工具类
最近项目要用到excel导出功能,之前也写过类似的代码.因为这次项目中多次用到excel导出.这次长了记性整理了一下 分享给大伙 欢迎一起讨论 生成excel的主工具类: public class E ...
- php 之 excel导出导入合并
<?php class Excel extends Controller { //直属高校 public function __construct() { parent::Controller( ...
随机推荐
- Nexus修改admin密码及其添加用户
Nexus之所以修改密码,是为了安全起见,个人学习的话,本地windows或者虚拟机即可,外网服务器建议将密码修改复杂点,而且强烈建议端口不要8081,最好将其改为其他的.同样也是为了安全起见. 添加 ...
- codeforces472D
Design Tutorial: Inverse the Problem CodeForces - 472D 给你了一个 n × n最短距离矩阵.(即矩阵中dis[u][v]为u点到v点的最短距离), ...
- Luogu_2774 方格取数问题
Luogu_2774 方格取数问题 二分图最小割 第一次做这种题,对于某些强烈暗示性的条件并没有理解到. 也就是每一立刻理解到是这个图是二分图. 为什么? 横纵坐标为奇数的只会和横纵坐标为偶数的相连. ...
- [LuoguP1438]无聊的数列(差分+线段树/树状数组)
\(Link\) \(\color{red}{\mathcal{Description}}\) 给你一个数列,要求支持单点查询\(and\)区间加等差数列. \(\color{red}{\mathca ...
- Selenium自动化测试之数据驱动及用例管理
Selenium自动化测试之数据驱动及用例管理 一.TestNg注解介绍 @Test:表示一个测试方法,在运行测试用例过程中,会自动运行@Test注解的方法. 例:
- EF Core 2.1 支持数据库一对一关系
在使用EF Core和设计数据库的时候,通常一对多.多对多关系使用得比较多,但是一对一关系使用得就比较少了.最近我发现实际上EF Core很好地支持了数据库的一对一关系. 数据库 我们先来看看SQL ...
- 1<=portNo<=4竟然在keil4.71里面不报错
1.if( 1<=portNo<=4 ) { CardIn2_CS_L; //pull low CardIn1_CS_H; CardOut1_CS_H; CardOut2_CS_H ...
- python2.7下使用logging模块记录日志到终端显示乱码问题解决
刚才翻了翻2年以前用python2.7写的一个爬虫程序,主要功能就是把各地市知识产权局/专利局网站的专利相关项目.课题通知,定期爬取和分析,辅助企业进行项目申请. 这里要谈的不是爬虫功能的实现,而是今 ...
- SFTP 服务搭建
1. 介绍 sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP ...
- Xcode 提交APP时遇到 “has one iOS Distribution certificate but its private key is not installed”
解决办法:登录Apple开发证书后台,把发布版证书.cer文件下载到本地,双击安装即可.若还没有设置发布证书文件,则创建一个后下载. Ref: https://blog.csdn.net/dingqk ...