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模板数据的更多相关文章

  1. java 导出Excel 大数据量,自己经验总结!

    出处: http://lyjilu.iteye.com/ 分析导出实现代码,XLSX支持: /** * 生成<span style="white-space: normal; back ...

  2. java 导出Excel 大数据量,自己经验总结!(二)

    在上一次的基础上加上了样式,以及中文列名 package com.tommy.fundation.util; import java.io.OutputStream; import java.util ...

  3. java后台读取excel模板数据

    /** * 读取EXCEL模板数据 * * @param excelFilePath excel文件路径 * @param dataRowNum 开始读取数据的行数 * @param keyRowNu ...

  4. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  5. [转载]Java导出Excel

    一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...

  6. java导出excel表格

    java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  7. java导出excel报表

    1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...

  8. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

  9. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

随机推荐

  1. c程序的期望

    对于C语言,我认为是我们计算机专业必须掌握的,如果C语言都不能掌握好,我认为在以后的学习中也不会学的有多好,所以,我们要把C语言尽量掌握好,也是在今后的学习中打好基础,多看书,多写一些程序运行,做一些 ...

  2. Python2和Python3安装注意事项

    1. 到官网 https://www.python.org/downloads/windows/ 下载 Windows x86-64 executable installer版本: 2. python ...

  3. VSCode 常用的快捷键

    R键:点击后热加载,直接查看预览结果 P键: 在虚拟机中显示网格,常用 O 键:切换iOS 和Android Q键 :退出调试 ctr +~  打开 终端

  4. ubuntu 16.04 安装 网易云

    现在网易云官网上下载对应版本 文件名:netease-cloud-music_1.0.0-2_amd64_ubuntu16.04.deb 进入下载目录: 正常安装会出现错误 解决的办法是换源 换源教程 ...

  5. spring boot 1.4 整合 mybatis druid

    http://www.jianshu.com/p/cef49ad91ba9spring boot 1.4 整合 mybatis druid

  6. poj 3264 倍增 ST表

    #include<iostream> #include<cmath> using namespace std; ; int a[maxn]; ]; ]; int quick(i ...

  7. hdu5001 Walk 概率DP

    I used to think I could be anything, but now I know that I couldn't do anything. So I started travel ...

  8. lua luv分析

    地址 https://github.com/richardhundt/luv

  9. django 路由分发

    对于一个大的工程,可能会有很多应用,比如cmbd,moniter,openstack等等,我们就要用到路由分发 1,首先在跟工程同名的文件夹下的urls中写分发表: from django.conf. ...

  10. FTP文件传输服务

    FTP文件传输服务 一 .FTP 连接及传输的模式 l  控制连接:TCP21,用于发送FTP命令信息. l  数据连接:TCP 20, 用于上传下载数据. · 数据连接建立的类型: ·主动模式: 服 ...