java 导出excel(复杂案例)
import java.io.FileOutputStream;
import java.io.IOException;
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.Region;
public class ExcelTest {
/**
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);
sheet.addMergedRegion(new Region(0, (short) 0, 2, (short) 0));
HSSFCell ce = row.createCell((short) 0);
// ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文处理
ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据
ce.setCellStyle(style); // 样式,居中
int num = 0;
for (int i = 0; i < 9; i++) { // 循环9次,每一次都要跨单元格显示
// 计算从那个单元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 单元格合并
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨单元格显示的数据
cell.setCellStyle(style); // 样式
// 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
// cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
// cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
num++;
}
// 在后面加上合计百分比
// 合计 在最后加上,还要跨一个单元格
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
// cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
// cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
// cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
// cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
// cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
======================================================
用到的jar包
poi-3.8.jar
poi-examples-3.8.jar
poi-excelant-3.8.jar
poi-ooxml-3.8.jar
poi-ooxml-schemas-3.8.jar
poi-scratchpad-3.8.jar
======================================================
效果

java 导出excel(复杂案例)的更多相关文章
- java 导出excel(简单案例)
public class Student { private int id; private String name; private int age; private Date birth; pub ...
- java导出excel报错:getOutputStream() has already been called for this response
对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...
- java导出excel表格
java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...
- java导出excel报表
1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...
- Java导出Excel和CSV(简单Demo)
Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- java导出excel模板数据
Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...
- java导出excel工具类
java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...
- [转载]Java导出Excel
一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...
随机推荐
- 【LeetCode】31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【锋利的Jquery】读书笔记十一
项目进度太赶,天天公司加班12小时,没时间看书充电.2016年再更新一篇吧.现在凌晨2点36分. 2017加油哦 jquery合适的选择器 $("#id") 无疑是最佳提高性能的方 ...
- 读书笔记之《The Art of Readable Code》
<The Art of Readable Code>- Dustin Boswell, Trevor Foucher O'Reilly出版 整体认知:这本书写得很好,每一章后面的总结 ...
- cookie会话技术
会话技术 B/S请求是无状态无记忆的,脚本与脚本之间是没有联系的,导致不能进行连续的业务逻辑 Cookie技术:将会话数据保存在浏览器端 原理:服务器向浏览器发送指令,用来管理存储在浏览器端的cook ...
- xml动态修改 dom4j修改
xml的动态修改需要传入的参数 xml的位置(tomcat中的发布位置).修改后的xml需要保存的位置(因为动态修改,所以建议和xml未修改前的位置相同).添加的节点的信息.或者修改的节点的信息 SA ...
- JVM调优实战
JVM调优实战 文档修订记录 版本 日期 撰写人 审核人 批准人 变更摘要 & 修订位置 ...
- HTTP 错误 403.14 - Forbidden的解决办法
错误: HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 原因: 出现这个错误,是因为默认文档中没有增加index.aspx导致的. 解决方法: 打开 ...
- ES 6 : 变量的解构赋值
1. 数组的解构赋值 [ 基本用法 ] 按照一定的模式从数组或者对象中取值,对变量进行赋值的过程称为解构. 以前,为变量赋值只能直接指定值: 而ES 6 允许写成下面这样: 上面的代码表示,可以从数组 ...
- document对象相关的几个常用的方法
方法 描述 var newP=createElement("p"); 创建了一个p标签,p也可替换为div,span等 var oldTxt=createTextNode(&qu ...
- [DP优化方法之虚树]
首先我们看一篇文章 转自xyz: 给出一棵树. 每次询问选择一些点,求一些东西.这些东西的特点是,许多未选择的点可以通过某种方式剔除而不影响最终结果. 于是就有了建虚树这个技巧..... 我们可以用l ...