首先需要引入的jar包:

正式代码了。

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.CellRangeAddress;

import org.apache.poi.hssf.util.HSSFColor;

@SuppressWarnings( { "deprecation" })

public class Test46 {

public static void main(String[] args) throws Exception {

String sheetName = "用车统计表单";

String titleName = "用车申请数据统计表";

String fileName = "用车申请统计表单";

int columnNumber = 3;

int[] columnWidth = { 10, 20, 30 };

String[][] dataList = { { "001", "2015-01-01", "IT" },

{ "002", "2015-01-02", "市场部" }, { "003", "2015-01-03", "测试" } };

String[] columnName = { "单号", "申请时间", "申请部门" };

new Test46().ExportNoResponse(sheetName, titleName, fileName,

columnNumber, columnWidth, columnName, dataList);

}

public void ExportWithResponse(String sheetName, String titleName,

String fileName, int columnNumber, int[] columnWidth,

String[] columnName, String[][] dataList,

HttpServletResponse response) throws Exception {

if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {

// 第一步,创建一个webbook,对应一个Excel文件

HSSFWorkbook wb = new HSSFWorkbook();

// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

HSSFSheet sheet = wb.createSheet(sheetName);

// sheet.setDefaultColumnWidth(15); //统一设置列宽

for (int i = 0; i < columnNumber; i++)

{

for (int j = 0; j <= i; j++)

{

if (i == j)

{

sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽

}

}

}

// 创建第0行 也就是标题

HSSFRow row1 = sheet.createRow((int) 0);

row1.setHeightInPoints(50);// 设备标题的高度

// 第三步创建标题的单元格样式style2以及字体样式headerFont1

HSSFCellStyle style2 = wb.createCellStyle();

style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);

style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式

headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗

headerFont1.setFontName("黑体"); // 设置字体类型

headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小

style2.setFont(headerFont1); // 为标题样式设置字体样式

HSSFCell cell1 = row1.createCell(0);// 创建标题第一列

sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,

columnNumber - 1)); // 合并列标题

cell1.setCellValue(titleName); // 设置值标题

cell1.setCellStyle(style2); // 设置标题样式

// 创建第1行 也就是表头

HSSFRow row = sheet.createRow((int) 1);

row.setHeightInPoints(37);// 设置表头高度

// 第四步,创建表头单元格样式 以及表头的字体样式

HSSFCellStyle style = wb.createCellStyle();

style.setWrapText(true);// 设置自动换行

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式

headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗

headerFont.setFontName("黑体"); // 设置字体类型

headerFont.setFontHeightInPoints((short) 10); // 设置字体大小

style.setFont(headerFont); // 为标题样式设置字体样式

// 第四.一步,创建表头的列

for (int i = 0; i < columnNumber; i++)

{

HSSFCell cell = row.createCell(i);

cell.setCellValue(columnName[i]);

cell.setCellStyle(style);

}

// 第五步,创建单元格,并设置值

for (int i = 0; i < dataList.length; i++)

{

row = sheet.createRow((int) i + 2);

// 为数据内容设置特点新单元格样式1 自动换行 上下居中

HSSFCellStyle zidonghuanhang = wb.createCellStyle();

zidonghuanhang.setWrapText(true);// 设置自动换行

zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

// 设置边框

zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);

zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);

zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);

zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);

zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);

// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中

HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();

zidonghuanhang2.setWrapText(true);// 设置自动换行

zidonghuanhang2

.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式

zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

// 设置边框

zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);

zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);

zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);

HSSFCell datacell = null;

for (int j = 0; j < columnNumber; j++)

{

datacell = row.createCell(j);

datacell.setCellValue(dataList[i][j]);

datacell.setCellStyle(zidonghuanhang2);

}

}

// 第六步,将文件存到浏览器设置的下载位置

String filename = fileName + ".xls";

response.setContentType("application/ms-excel;charset=UTF-8");

response.setHeader("Content-Disposition", "attachment;filename="

.concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));

OutputStream out = response.getOutputStream();

try {

wb.write(out);// 将数据写出去

String str = "导出" + fileName + "成功!";

System.out.println(str);

} catch (Exception e) {

e.printStackTrace();

String str1 = "导出" + fileName + "失败!";

System.out.println(str1);

} finally {

out.close();

}

} else {

System.out.println("列数目长度名称三个数组长度要一致");

}

}

public void ExportNoResponse(String sheetName, String titleName,

String fileName, int columnNumber, int[] columnWidth,

String[] columnName, String[][] dataList) throws Exception {

if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {

// 第一步,创建一个webbook,对应一个Excel文件

HSSFWorkbook wb = new HSSFWorkbook();

// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

HSSFSheet sheet = wb.createSheet(sheetName);

// sheet.setDefaultColumnWidth(15); //统一设置列宽

for (int i = 0; i < columnNumber; i++)

{

for (int j = 0; j <= i; j++)

{

if (i == j)

{

sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽

}

}

}

// 创建第0行 也就是标题

HSSFRow row1 = sheet.createRow((int) 0);

row1.setHeightInPoints(50);// 设备标题的高度

// 第三步创建标题的单元格样式style2以及字体样式headerFont1

HSSFCellStyle style2 = wb.createCellStyle();

style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);

style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式

headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗

headerFont1.setFontName("黑体"); // 设置字体类型

headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小

style2.setFont(headerFont1); // 为标题样式设置字体样式

HSSFCell cell1 = row1.createCell(0);// 创建标题第一列

sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,

columnNumber - 1)); // 合并第0到第17列

cell1.setCellValue(titleName); // 设置值标题

cell1.setCellStyle(style2); // 设置标题样式

// 创建第1行 也就是表头

HSSFRow row = sheet.createRow((int) 1);

row.setHeightInPoints(37);// 设置表头高度

// 第四步,创建表头单元格样式 以及表头的字体样式

HSSFCellStyle style = wb.createCellStyle();

style.setWrapText(true);// 设置自动换行

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式

headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗

headerFont.setFontName("黑体"); // 设置字体类型

headerFont.setFontHeightInPoints((short) 10); // 设置字体大小

style.setFont(headerFont); // 为标题样式设置字体样式

// 第四.一步,创建表头的列

for (int i = 0; i < columnNumber; i++)

{

HSSFCell cell = row.createCell(i);

cell.setCellValue(columnName[i]);

cell.setCellStyle(style);

}

// 第五步,创建单元格,并设置值

for (int i = 0; i < dataList.length; i++)

{

row = sheet.createRow((int) i + 2);

// 为数据内容设置特点新单元格样式1 自动换行 上下居中

HSSFCellStyle zidonghuanhang = wb.createCellStyle();

zidonghuanhang.setWrapText(true);// 设置自动换行

zidonghuanhang

.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

// 设置边框

zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);

zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);

zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);

zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);

zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);

// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中

HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();

zidonghuanhang2.setWrapText(true);// 设置自动换行

zidonghuanhang2

.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式

zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

// 设置边框

zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);

zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);

zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);

HSSFCell datacell = null;

for (int j = 0; j < columnNumber; j++)

{

datacell = row.createCell(j);

datacell.setCellValue(dataList[i][j]);

datacell.setCellStyle(zidonghuanhang2);

}

}

// 第六步,将文件存到指定位置

try {

FileOutputStream fout = new FileOutputStream("D:students.xls");

wb.write(fout);

String str = "导出" + fileName + "成功!";

System.out.println(str);

fout.close();

} catch (Exception e) {

e.printStackTrace();

String str1 = "导出" + fileName + "失败!";

System.out.println(str1);

}

} else {

System.out.println("列数目长度名称三个数组长度要一致");

}

}

}

为了本地测试效果,单独写了一个无response参数的ExportNoResponse方法,直接将文件保存到指定目录D盘。
两个方法的不同就在于第六步中,有response参数的方法可以将文件存到浏览器设置的下载位置。
下面是导出效果截图:

java导出excel通用方法的更多相关文章

  1. java根据xml配置文件导出excel通用方法

    java web项目中时常会用到导出功能,而导出excel几乎是每个项目必备的功能之一.针对形形色色的导出方法及个人平时的工作经验,特将导出excel方法整理成通用的方法,根据xml配置来实现特定的导 ...

  2. NOPI 导出excel 通用方法

    public static byte[] ExportExcel<T>(Dictionary<string, string> columnsHeader, List<T& ...

  3. NPOI 导出excel 通用方法

    public static byte[] ExportExcel<T>(Dictionary<string, string> columnsHeader, List<T& ...

  4. Java 导出EXCEL

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  5. Java学习之道:Java 导出EXCEL

    1.Apache POI简单介绍  Apache POI是Apache软件基金会的开放源代码函式库.POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开 ...

  6. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

  7. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  8. java导出excel表格

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

  9. java导出excel报表

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

随机推荐

  1. echart全局属性,自用,搜索比较快

    // 全图默认背景  // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...

  2. jQuery scrollFix滚动定位插件

    [插件功能] 当用户向上或向下滚动页面到一定位置时,目标元素开始固定定位(position:fixed),当回滚到原位置时目标元素恢复到原状态,可以定制触发滚动相对屏幕位置和触发滚动方向,兼容IE6 ...

  3. php 返回某个月的 每周几有几天

    不得不承认,这真是一个奇葩的需求,无奈写个类凑活用用. 输入日期格式或者 时间戳,返回当月有多少个周一.周二.周三.....周日; 思路就是 找到这个月有多少天,在便利判断. 稍微考虑下闰年的情况 前 ...

  4. c#设置button透明

    c#设置button透明 1.使用代码进行设置: this.button_welcom_login.FlatStyle = System.Windows.Forms.FlatStyle.Flat; t ...

  5. Java基础学习总结(63)——Java集合总结

    数据结构是以某种形式将数据组织在一起的集合,它不仅存储数据,还支持访问和处理数据的操作.Java提供了几个能有效地组织和操作数据的数据结构,这些数据结构通常称为Java集合框架.在平常的学习开发中,灵 ...

  6. spring md5 加密

    一.spring 自带的 DigestUtils 工具类可以进行 md5 加密 //导入包 import org.springframework.util.DigestUtils; //对密码进行 m ...

  7. @Bean 指定初始化和销毁方法

    bean 的生命周期 bean 的创建 --> 初始化 --> 销毁 ioc 容器管理 bean 的声明周期 可以自定义初始化和销毁方法 构造器( 对象创建 )被调用时机 单实例:在容器启 ...

  8. asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值

    asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...

  9. Weblogic常用监控指标

    http://blog.csdn.net/konglongaa/article/details/53897562

  10. 继续过Hard题目.0209

    http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Diffi ...