首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
编程语言
> 详细
spring mvc poi excel
时间:
2015-08-15 14:51:58
阅读:
199
评论:
0
收藏:
0
[点我收藏+]
标签:
Java代码
Util类
package com.common.util;
public
class ExportUtil
{
private XSSFWorkbook wb =
null;
private XSSFSheet sheet =
null;
/**
* @param wb
* @param sheet
*/
public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet)
{
this.wb = wb;
this.sheet = sheet;
}
/**
* 合并单元格后给合并后的单元格加边框
*
* @param region
* @param cs
*/
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);
}
}
}
/**
* 设置表头的单元格样式
*
* @return
*/
public XSSFCellStyle getHeadStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格的背景颜色为淡蓝色
cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
// 设置单元格居中对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
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.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}
/**
* 设置表体的单元格样式
*
* @return
*/
public XSSFCellStyle getBodyStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格居中对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
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.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}
}
service类
public
interface ITestExportExcelService
{
public
void exportExcel(String hql,String [] titles,ServletOutputStream outputStream);
}
@Service
public
class TestExportExcelServiceImpl
implements ITestExportExcelService
{
@Resource
private ITestExportExcelDao dao;
public
void exportExcel(String hql, String[] titles, ServletOutputStream outputStream)
{
List<Goods> list = dao.exportExcel(hql);
// 创建一个workbook 对应一个excel应用文件
XSSFWorkbook workBook =
new XSSFWorkbook();
// 在workbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = workBook.createSheet(
"导出excel例子");
ExportUtil exportUtil =
new ExportUtil(workBook, sheet);
XSSFCellStyle headStyle = exportUtil.getHeadStyle();
XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();
// 构建表头
XSSFRow headRow = sheet.createRow(
0);
XSSFCell cell =
null;
for (
int i =
0; i < titles.length; i++)
{
cell = headRow.createCell(i);
cell.setCellStyle(headStyle);
cell.setCellValue(titles[i]);
}
// 构建表体数据
if (list !=
null && list.size() >
0)
{
for (
int j =
0; j < list.size(); j++)
{
XSSFRow bodyRow = sheet.createRow(j +
1);
Goods goods = list.get(j);
cell = bodyRow.createCell(
0);
cell.setCellStyle(bodyStyle);
cell.setCellValue(goods.getGoodsName());
cell = bodyRow.createCell(
1);
cell.setCellStyle(bodyStyle);
cell.setCellValue(goods.getGoodsCost());
cell = bodyRow.createCell(
2);
cell.setCellStyle(bodyStyle);
cell.setCellValue(goods.getGoodsUnit());
}
}
try
{
workBook.write(outputStream);
outputStream.flush();
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
dao类
public
interface ITestExportExcelDao
{
public List<Goods> exportExcel(String hql);
}
@Repository
public
class TestExportExcelDaoImpl
implements ITestExportExcelDao
{
@Resource
private SessionFactory factory;
/**
* 以goods表为例导出测试
*/
@SuppressWarnings(
"unchecked")
public List<Goods> exportExcel(String hql)
{
Session session = factory.getCurrentSession();
List<Goods> list = session.createQuery(hql).list();
return list;
}
}
控制层Controller
@Controller
@RequestMapping(
"/testexportexcel/*")
public
class TestExportExcelController
{
@Resource
private ITestExportExcelService service;
@RequestMapping
public String exportExcel(HttpServletResponse response)
{
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 hql =
"from Goods";
String[] titles = {
"商品名",
"商品单价",
"商品单位" };
service.exportExcel(hql, titles, outputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
return
null;
}
@RequestMapping
public String upload(HttpServletRequest request, HttpServletResponse response)
{
MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
MultipartFile file = mulRequest.getFile(
"excel");
String filename = file.getOriginalFilename();
if (filename ==
null ||
"".equals(filename))
{
return
null;
}
try
{
InputStream input = file.getInputStream();
XSSFWorkbook workBook =
new XSSFWorkbook(input);
XSSFSheet sheet = workBook.getSheetAt(
0);
if (sheet !=
null)
{
for (
int i =
1; i < sheet.getPhysicalNumberOfRows(); i++)
{
XSSFRow row = sheet.getRow(i);
for (
int j =
0; j < row.getPhysicalNumberOfCells(); j++)
{
XSSFCell cell = row.getCell(j);
String cellStr = cell.toString();
System.out.print(
"【"+cellStr+
"】 ");
}
System.out.println();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return
"/test/uploadExcel.jsp";
}
}
spring mvc poi excel
标签:
原文地址:http://www.cnblogs.com/likeju/p/4732401.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
Spring Cloud 从入门到精通(一)Nacos 服务中心初探
2021-07-29
基础的排序算法
2021-07-29
SpringBoot|常用配置介绍
2021-07-29
关于 .NET 与 JAVA 在 JIT 编译上的一些差异
2021-07-29
C语言常用函数-toupper()将字符转换为大写英文字母函数
2021-07-29
《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)
2021-07-28
4-1 YAML配置文件 注入 JavaBean中
2021-07-28
【python】 用来将对象持久化的 pickle 模块
2021-07-28
马拉车算法
2021-07-28
用Python进行冒泡排序
2021-07-28
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!