关于JAVA 用POI 导出Excel
来源:互联网 发布:mac无法打开cad注册机 编辑:程序博客网 时间:2024/06/06 04:17
研究好几天终于把导出EXCEL 报表研究透彻了。
public void exportExcel(String[] titles, List
基本上写的非常详细吧。对于开发人员来说,这些东西应该很快都能看懂。
以下是我整理的一些资料。
框架:spring mvc
公共类导出EXCEL:(这个很重要,拿过来不用太多的修改,每个方法的属性都有注解)
package com.cmcc.pas.common;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportUtil
{
private XSSFWorkbook wb = null;
private XSSFSheet sheet = null;
public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet)
{
this.wb = wb;
this.sheet = sheet;
}
public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs)
{
int toprowNum = region.getFirstRow();
for (int i = toprowNum; i <= region.getLastRow(); i++)
{
XSSFRow row = sheet.getRow(i);
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++)
{
XSSFCell cell = row.getCell(j);// XSSFCellUtil.getCell(row,
// (short) j);
cell.setCellStyle(cs);
}
}
}
public XSSFCellStyle getHeadStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格的背景颜色为淡蓝色
cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
// 设置单元格居中对齐
// 设置单元格内容水平对其方式
// XSSFCellStyle.ALIGN_CENTER 居中对齐
// XSSFCellStyle.ALIGN_LEFT 左对齐
// XSSFCellStyle.ALIGN_RIGHT 右对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
// 设置单元格内容垂直对其方式
// XSSFCellStyle.VERTICAL_TOP 上对齐
// XSSFCellStyle.VERTICAL_CENTER 中对齐
// XSSFCellStyle.VERTICAL_BOTTOM 下对齐
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体样式
XSSFFont font = wb.createFont();
// 设置字体加粗
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
// 设置单元格边框为细线条
// 设置单元格边框样式
// CellStyle.BORDER_DOUBLE 双边线
// CellStyle.BORDER_THIN 细边线
// CellStyle.BORDER_MEDIUM 中等边线
// CellStyle.BORDER_DASHED 虚线边线
// CellStyle.BORDER_HAIR 小圆点虚线边线
// CellStyle.BORDER_THICK 粗边线
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}
public XSSFCellStyle getBodyStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格居中对齐
// 设置单元格内容水平对其方式
// XSSFCellStyle.ALIGN_CENTER 居中对齐
// XSSFCellStyle.ALIGN_LEFT 左对齐
// XSSFCellStyle.ALIGN_RIGHT 右对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
// 设置单元格内容垂直对其方式
// XSSFCellStyle.VERTICAL_TOP 上对齐
// XSSFCellStyle.VERTICAL_CENTER 中对齐
// XSSFCellStyle.VERTICAL_BOTTOM 下对齐
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体样式
XSSFFont font = wb.createFont();
// 设置字体加粗
font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
// 设置单元格边框为细线条
// 设置单元格边框样式
// CellStyle.BORDER_DOUBLE 双边线
// CellStyle.BORDER_THIN 细边线
// CellStyle.BORDER_MEDIUM 中等边线
// CellStyle.BORDER_DASHED 虚线边线
// CellStyle.BORDER_HAIR 小圆点虚线边线
// CellStyle.BORDER_THICK 粗边线
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}
public XSSFCellStyle getBigHeadStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格的背景颜色为淡蓝色
cellStyle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
// 设置单元格居中对齐
// 设置单元格内容水平对其方式
// XSSFCellStyle.ALIGN_CENTER 居中对齐
// XSSFCellStyle.ALIGN_LEFT 左对齐
// XSSFCellStyle.ALIGN_RIGHT 右对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
// 设置单元格内容垂直对其方式
// XSSFCellStyle.VERTICAL_TOP 上对齐
// XSSFCellStyle.VERTICAL_CENTER 中对齐
// XSSFCellStyle.VERTICAL_BOTTOM 下对齐
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体样式
XSSFFont font = wb.createFont();
// 设置字体加粗
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 400);
cellStyle.setFont(font);
// 设置单元格边框为细线条
// 设置单元格边框样式
// CellStyle.BORDER_DOUBLE 双边线
// CellStyle.BORDER_THIN 细边线
// CellStyle.BORDER_MEDIUM 中等边线
// CellStyle.BORDER_DASHED 虚线边线
// CellStyle.BORDER_HAIR 小圆点虚线边线
// CellStyle.BORDER_THICK 粗边线
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}
}
JSP前台页面:
由于ajax 不能回显出,excel下载保存打开窗口。所以必须要用form 表单的submit。
代码如下:
//导出
function onExport(){
var form1 = window.document.getElementByIdx_x("form");//获取form1对象
form1.action="${pageContext.request.contextPath}/deposit_DepositStatisticsController/export.json";
form1.submit();
}
Controller层:
@LogRecord
@RequestMapping(value = "/export")
@AccessAuthority(access = AccessType.AUTH, name = "备付金管理-导出EXCEL", id = "6203")
public Map export(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception
{
response.setContentType("application/binary;charset=ISO8859_1");
try
{
ServletOutputStream outputStream = response.getOutputStream();
String fileName = new String(("导出excel例子").getBytes(), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
String[] titles = { "ID", "客户编号", "客户名称", "金额" };
List
listData = excelService.queryExcelDate();
excelService.exportExcel(titles, listData, outputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
return model;
}
service层:
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cmcc.pas.common.ExportUtil;
import com.cmcc.pas.repository.deposit.ExcelDAO;
@Service
public class ExcelService
{
public void exportExcel(String[] titles, List
}
JAR包:(当然还有非常重要的JAR包)
这里面我就把需要的JAR包截图了,实在是找不到如何上传压缩文件。
基本上写的非常详细吧。对于开发人员来说,这些东西应该很快都能看懂。
0 0
- 关于JAVA 用POI 导出Excel
- 关于JAVA 用POI 通过EXCEL模板 导出Excel
- 关于JAVA 用POI 通过EXCEL模板 导出Excel
- 关于poi导出Excel
- java poi 导出excel
- Java POI导出excel
- Java POI 导出Excel
- java poi 导出excel
- java POI导出Excel
- JAVA POI 导出excel
- JAVA POI 导出excel
- java poi导出excel
- Java POI 导出EXCEL
- Java POI导出Excel
- java 用poi包导出excel
- 关于POI导入导出Excel
- 用poi导出Excel
- 关于java poi导入导出excel的包冲突问题
- HTML meta viewport属性详细说明
- 矩阵分解
- 详解Java GC的工作原理+Minor GC、FullGC
- Android之高仿雅虎天气
- Android Notification通知详解
- 关于JAVA 用POI 导出Excel
- ioctl函数
- C3P0的详细配置说明(com.mchange.v2.c3p0.ComboPooledDataSource)
- 学习嵌入式开发要学习哪些知识?
- Latex使用技巧
- node pm2进程管理器
- 关于微信网页授权
- Python学习十:生成器
- mysql排序规则