java导出excel模板数据
Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤:
String[] colName=new String[]{"期间","科目代码","科目名称","币别","期初余额(借方)","期初余额(贷方)","本期发生额(借方)","本期发生额(贷方)","本年累计发生额(借方)","本年累计发生额(贷方)","期末余额(借方)","期末余额(贷方)"};
DataExcelExportUtil dataExcelExportUtil =new DataExcelExportUtil();
list为List<Object[]>list类型:
XSSFWorkbook wb=dataExcelExportUtil.exportExcelWorkbook("科目余额表数据导出", colName, list, true,querySubMessage);
String fileName = new String("科目余额表数据导出.xlsx".getBytes("GBK"),"ISO8859-1");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
wb.write(response.getOutputStream());
response.getOutputStream().flush();
result.put("success", "导出科目余额表excle数据成功");
下面这个就是一个Java类,上面创建的对象:
public class DataExcelExportUtil {
private Map<String, XSSFCellStyle> styles;
protected int rowIndex = 0;
public XSSFWorkbook exportExcelWorkbook(String sheetName, String[] columnHeaders, List<Object[]> contentList,
boolean flag, Map<String, Object> querySubMessage) throws Exception {
rowIndex = 0;
XSSFSheet sheet = null;
XSSFWorkbook wb = null;
wb = new XSSFWorkbook();
createStyles(wb);
if (sheetName == null || sheetName.length() == 0) {
sheetName = "Sheet第一页";
}
sheet = wb.createSheet(sheetName);
sheet.createFreezePane(0, 1, 0, 1);
if (flag == true) {
generateHeader(sheet, columnHeaders, 0);
}
for (int i = 0; i < contentList.size(); i++) {
Object[] subject = contentList.get(i);
generateContent(sheet, columnHeaders, subject, i + 1);
}
generateContentLast(sheet, columnHeaders, querySubMessage, contentList.size() + 1);
return wb;
}
public void generateContent(XSSFSheet sheet, String[] columnHeaders, Object[] subject, int rowIndex) {
XSSFRow row = sheet.createRow(rowIndex++);
Object[] objArr = subject;
for (int coloum = 0; coloum < columnHeaders.length; coloum++) {
XSSFCell cell = row.createCell(coloum);
cell.setCellStyle(styles.get("fontfinally"));
cell.setCellValue(objArr[coloum] != null ? objArr[coloum].toString() : "");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
}
}
public void generateContentLast(XSSFSheet sheet, String[] columnHeaders, Map<String, Object> querySubMessage,
int rowIndex) {
if(querySubMessage==null){
return ;
}
XSSFRow row = sheet.createRow(rowIndex++);
Map<String, Object> subject = querySubMessage;
for (int coloum = 0; coloum < columnHeaders.length; coloum++) {
XSSFCell cell = row.createCell(coloum);
if (coloum == 0) {
cell.setCellValue(0);
}
if (coloum == 1 || coloum == 3) {
cell.setCellValue("");
}
if (coloum == 2) {
cell.setCellValue("合计");
}
if (coloum == 4) {
cell.setCellValue(Double.parseDouble(subject.get("initDebitBalanceTotal").toString()));
}
if (coloum == 5) {
cell.setCellValue(Double.parseDouble(subject.get("initCreditBalanceTotal").toString()));
}
if (coloum == 6) {
cell.setCellValue(Double.parseDouble(subject.get("currentAmountDebitTotal").toString()));
}
if (coloum == 7) {
cell.setCellValue(Double.parseDouble(subject.get("currentAmountCreditTotal").toString()));
}
if (coloum == 8) {
cell.setCellValue(Double.parseDouble(subject.get("yearAmountDebitTotal").toString()));
}
if (coloum == 9) {
cell.setCellValue(Double.parseDouble(subject.get("yearAmountCreditTotal").toString()));
}
if (coloum == 10) {
cell.setCellValue(Double.parseDouble(subject.get("endingBalanceDebitTotal").toString()));
}
if (coloum == 11) {
cell.setCellValue(Double.parseDouble(subject.get("endingBalanceCreditTotal").toString()));
}
}
}
public Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb) {
styles = new HashMap<String, XSSFCellStyle>();
XSSFDataFormat df = wb.createDataFormat();
XSSFFont normalFont = wb.createFont();
normalFont.setFontHeightInPoints((short) 10);
XSSFFont boldFont = wb.createFont();
boldFont.setFontHeightInPoints((short) 10);
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
XSSFFont blueBoldFont = wb.createFont();
blueBoldFont.setFontHeightInPoints((short) 10);
blueBoldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
blueBoldFont.setColor(HSSFColor.BLUE.index);
XSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFont(boldFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styles.put("header", headerStyle);
XSSFCellStyle dateCellStyle = wb.createCellStyle();
dateCellStyle.setFont(normalFont);
dateCellStyle.setDataFormat(df.getFormat("yyyy-MM-dd"));
setBorder(dateCellStyle);
styles.put("dateCell", dateCellStyle);
XSSFCellStyle numberCellStyle = wb.createCellStyle();
numberCellStyle.setFont(normalFont);
numberCellStyle.setDataFormat(df.getFormat("#,##0.00000000"));
setBorder(numberCellStyle);
styles.put("numberCell", numberCellStyle);
XSSFCellStyle numberCellStyle1 = wb.createCellStyle();
numberCellStyle1.setFont(boldFont);
numberCellStyle1.setDataFormat(df.getFormat("#,##0.00000000"));
setBorder(numberCellStyle1);
styles.put("numberCell1", numberCellStyle1);
XSSFCellStyle numberCellStyle2 = wb.createCellStyle();
numberCellStyle2.setFont(blueBoldFont);
numberCellStyle2.setDataFormat(df.getFormat("#,##0.00000000"));
setBorder(numberCellStyle2);
styles.put("numberCell2", numberCellStyle2);
XSSFCellStyle totalStyle = wb.createCellStyle();
totalStyle.setFont(blueBoldFont);
totalStyle.setWrapText(true);
totalStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
setBorder(totalStyle);
styles.put("total", totalStyle);
XSSFFont titleFont = wb.createFont();
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleFont.setFontHeightInPoints((short) 20);
XSSFFont middleFont = wb.createFont();
middleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
middleFont.setFontHeightInPoints((short) 12);
XSSFCellStyle titleCellStyle = wb.createCellStyle();
titleCellStyle.setFont(titleFont);
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styles.put("titleCell", titleCellStyle);
XSSFCellStyle middleCellStyle = wb.createCellStyle();
middleCellStyle.setFont(middleFont);
middleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styles.put("middleCell", middleCellStyle);
XSSFCellStyle leftCellStyle = wb.createCellStyle();
leftCellStyle.setFont(middleFont);
leftCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
styles.put("leftCell", leftCellStyle);
XSSFCellStyle dateCellStyle1 = wb.createCellStyle();
dateCellStyle1.setFont(middleFont);
dateCellStyle1.setDataFormat(df.getFormat("yyyy-MM-dd"));
dateCellStyle1.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
setBorder(dateCellStyle);
styles.put("dateCell1", dateCellStyle1);
XSSFCellStyle contentStyle = wb.createCellStyle();
contentStyle.setFont(normalFont);
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// contentStyle.setWrapText(true);
styles.put("contentCell", contentStyle);
XSSFCellStyle numberCellStyle3 = wb.createCellStyle();
numberCellStyle3.setFont(middleFont);
numberCellStyle3.setDataFormat(df.getFormat("#,##0.00000000"));
setBorder(numberCellStyle3);
styles.put("numberCell3", numberCellStyle3);
XSSFCellStyle numberCellStyle4 = wb.createCellStyle();
numberCellStyle4.setFont(normalFont);
numberCellStyle4.setDataFormat(df.getFormat("#,##0.00000000"));
setBorder(numberCellStyle4);
styles.put("numberCell4", numberCellStyle4);
XSSFCellStyle fontstyle = wb.createCellStyle();
fontstyle.setDataFormat(df.getFormat("@"));
styles.put("fontfinally", fontstyle);
return styles;
}
public void generateHeader(XSSFSheet sheet, String[] columnHeaders, int rowIndex) {
rowIndex = 0;
XSSFRow r = sheet.createRow(rowIndex++);
for (int i = 0; i < columnHeaders.length; i++) {
XSSFCell cell = r.createCell(i);
sheet.autoSizeColumn((short) i);
cell.setCellValue(columnHeaders[i]);
cell.setCellStyle(styles.get("header"));
}
}
public void setBorder(XSSFCellStyle style) {
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
}
}
java导出excel模板数据的更多相关文章
- java 导出Excel 大数据量,自己经验总结!
出处: http://lyjilu.iteye.com/ 分析导出实现代码,XLSX支持: /** * 生成<span style="white-space: normal; back ...
- java 导出Excel 大数据量,自己经验总结!(二)
在上一次的基础上加上了样式,以及中文列名 package com.tommy.fundation.util; import java.io.OutputStream; import java.util ...
- java后台读取excel模板数据
/** * 读取EXCEL模板数据 * * @param excelFilePath excel文件路径 * @param dataRowNum 开始读取数据的行数 * @param keyRowNu ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- [转载]Java导出Excel
一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...
- 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导出excel工具类
java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...
随机推荐
- js调用app启动页
第一步:添加js $(function () { var ua = window.navigator.userAgent.toLowerCase(); //微信 if(ua.match(/MicroM ...
- CodeForces - 13E
Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...
- 【工具】switchhost
1.前提 主要功能切换host 2.下载路径 https://oldj.github.io/SwitchHosts/ 3.使用略(太简单)
- torchvision里densenet代码分析
#densenet原文地址 https://arxiv.org/abs/1608.06993 #densenet介绍 https://blog.csdn.net/zchang81/article/de ...
- 斐波那契数列中获取第n个数据值
class Fibonacci { /** * Description:迭代方法获取fibonacci第n项数值 * * @param int $n * @return int */ public s ...
- kafka-producer kerberos 原理和配置
kerberos简单介绍 kerberos这一名词来源于希腊神话“三个头的狗---地狱之门守护者”后来沿用作为安全认证的概念,该系统设计上 采用客户端/服务器结构与DES(Data Encryptio ...
- python(二)——list、字典、字符串操作
列表——list 假设一种场景,想要存班里所有同学的名字,那应该拿什么存呢? 我们可以拿一个字符串去存,例如: stus = "s1,s2,s3,s4,s5……sn" 那我们要从里 ...
- 聊聊Java反射
反射是Java最重要的特性.通过Java反射可以在运行时知道一个类的所有成员和方法,知道一个对象的类类型.成员和方法的所有信息,进而调用对象的方法或生成对象的代理或包装类. Java是面向对象语言,除 ...
- expect脚本实现ssh自动登录
1:简单的实现ssh登录 #!/usr/bin/expect set ip "10.0.0.142" set user "root" set password ...
- laravel框架容器管理
来自http://www.cnblogs.com/chy1000/p/7072936.html 本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章 ...