java导出excel模板数据

Wesley13
• 阅读 667

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);
}

}

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
PHP导入导出EXCELl,CSV
PHP导入导出Excel,CSVHTML<formaction"{:U('Admin/Unit/importcsv')}"method"post"name"myform"id"myform"enctype"multipart/formdata"<input
Stella981 Stella981
3年前
Python Challenge Level 18
初学Python,挑战一下流行的PythonChallenge,很不幸,卡在了18关~~被字符字节码之间的转换搞得焦头烂额,不过终于搞定了还是很happy的~~~主要的问题就是16进制形式的字符如何转成字节码(注意:不是encoding)如:\'89','50','4e','47','0d','0a','1a','0a','00
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这