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项目tinyhttpd(500行代码)

    编译命令 gcc -W -Wall -lpthread -o httpd httpd.c 源码 #include <stdio.h> #include <sys/socket.h&g ...

  2. git 本地重命名文件夹大小写并提交到远程分支

    git branch 查看本地分支 git branch -a 查看本地 本地分支可直接切换:git checkout name 进入正题: 1.文件夹备份 2.git config core.ign ...

  3. 文本对齐方式(text-align)

    text-align 设置元素内文本的水平对齐方式. 属性值:left.right.center.justify 注:该属性对块级元素设置有效.

  4. springboot笔记1(转载于puresmile)

    构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  5. day02python 整型 布尔

    今日内容 int bool 详细内容 1.整型(int) Py2 32位电脑 64位电脑 超出范围后python将自动转换成long(长整型) /运算不能显示小数-> (整形除法只能保留整数位) ...

  6. CSS3 实现圆形、椭圆形、三角形等各种形状样式

    CSS3 圆形 #css3-circle{ width: 150px; height: 150px; border-radius: 50%; background-color: #232323;} C ...

  7. Javascript 将一个句子中的单词首字母转成大写

    Javascript 将一个句子中的单词首字母转成大写 先上代码 function titleCase(str) { str = str.toLowerCase().split(" &quo ...

  8. Vue-项目之免费课和购物车实现

    调整首页细节 固定头部 App.vue中代码 <style> body{ padding: 0; margin:0; margin-top: 80px; } </style> ...

  9. Vue 表单校验 vee-validate

    gitHub 地址:https://github.com/baianat/vee-validate 官网API 地址:https://baianat.github.io/vee-validate/ap ...

  10. python 文件读写模式r,r+,w,w+,a,a+的区别(附代码示例)

    如下表   模式 可做操作 若文件不存在 是否覆盖 r 只能读 报错 - r+ 可读可写 报错 是 w 只能写 创建 是 w+ 可读可写 创建 是 a 只能写 创建 否,追加写 a+ 可读可写 创建 ...