首先需要引入的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. Vue中的v-model与my97日期选择插件冲突

    Vue中的v-model指令只是一个语法糖,其具体实现是:监听input框的input事件,然后将用户输入的值赋值给input框的value属性 <input type="text&q ...

  2. Flex元素布局规则总结,以及布局和容器

    一.Flex中的元素分类从功能层面可以把Flex中的元素分为组件(Components)和容器(Containers)两大类:组件 - 是指那类具有明确交互或数据展示功能的元素,例如Button.Ch ...

  3. vc++如何创建窗口

    创建一个mfc工程,AppWizard可以用来增加和选择一些特性 可以发现一共有五个类,每一个类都有一个基类,都是mfc基础类库中的类 例如将一个类拷贝一下,将CFrameWnd到msdn中查找一下, ...

  4. PHP SplObjectStorage使用实例

    SplObjectStorage是SPL标准库中的数据结构对象容器,用来存储一组对象,特别是当你需要唯一标识对象的时候,需要的朋友可以参考下: PHP SPL SplObjectStorage类实现了 ...

  5. mac 安装卸载python

    第 1 步,删除框架: sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.x第 2步,删除应用目录: sudo rm -rf &q ...

  6. Python笔记17---------魔法方法

    魔法方法也为特殊方法,即用两个下划线形成的(__方法__).自己定义的方法最好不要采用这种方式,因为这些方法会在一些特殊的情况下直接被调用. 1.第一个魔法方法:类中常用的__init__()方法:相 ...

  7. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  8. PHP算法之判断是否是质数

    质数的定义 质数又称素数.一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数:否则称为合数. 实现思路 循环所有可能的备选数字,然后和中间数以下且大于等于2的整数进行整除比较,如果能 ...

  9. redis_2 数据类型

    1.key Redis keys 命令 下表给出了与 Redis 键相关的基本命令: 序号 命令及描述 1 DEL key该命令用于在 key 存在时删除 key. 2 DUMP key 序列化给定 ...

  10. Makefile错误总结

    自己在做嵌入式驱动时,编写makefile文件是犯的错及解决办法 问题1:makefile 3 missing separator.stop: 问题2:Nothing to be done for ' ...