标签:
Java使用POI实现数据导出excel报表
在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式。不仅仅简单的读取office中的数据.尤其是在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作。而数据导出的格式一般是EXCEL或者PDF 。所以今天我们来简单看一下利用Apache POI实现数据库中数据导出excel报表。在java中有很多实现数据导出excel报表的第三方jar包。但在比较了一下感觉还是POI相对来说比较好用。如果大家想学习其他的导出方式可以自行研究一下。
首先来了解一下 Apache POI
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)。 下面我们来看一下Apache POI 中提供的几大部分的作用:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
本节我们所要学习的是POI对excel的操作。所以我们只需用到HSSF 这部分内容就可以了。其他的请大家自行研究吧。哈哈。
下面我们就一步一步的来看一下如何创建一个excel报表:
1. 创建新的Excel工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
在Excel工作簿中建一工作表,其名为缺省值。POI中还提供了其他的一些其他的workbook 构造方法。下面我们来看一下:
2.创建一个工作表
如要新建一名为"工资表"的工作表,其语句为:
HSSFSheet sheet = workbook.createSheet("工资表");
3.创建行
在索引0的位置创建行(最顶端的行)
HSSFRow row = sheet.createRow(0);
4.创建单元格
在索引0的位置创建单元格(左上端)
HSSFCell cell = row.createCell((short) 0);
定义单元格为字符串类型,这个字符串类型也可在创建单元格里面设置。
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
在单元格中输入一些内容
cell.setCellValue("增加值");
5.新建一输出文件流,把相应的Excel工作簿 输出到本地
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.flush();
操作结束,关闭文件
fOut.close();
OK,在给单元格设置下从数据库中读取的数据。这样我们就可以把数据库里面的内容导入到excel了。当然这里我们只是简单的介绍了一些POI的用法。并没有细讲,比如设置单元格里面数据的格式。单元格的一些属性设置。这些将会在我下面的例子中用到。也算是给大家一些示例吧。好了。最后看一个实例吧:
EXCEL报表工具类:ExportExcel.java
- package com.bzu.search.action;
-
-
import java.io.File;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
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.HSSFRichTextString;
-
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.HSSFColor;
-
import org.apache.poi.hssf.util.Region;
-
-
-
public class ExportExcel {
-
-
private HSSFWorkbook wb = null;
-
-
private HSSFSheet sheet = null;
-
-
-
public ExportExcel(HSSFWorkbook wb, HSSFSheet sheet) {
-
super();
-
this.wb = wb;
-
this.sheet = sheet;
- }
-
-
-
public HSSFSheet getSheet() {
-
return sheet;
- }
-
-
-
public void setSheet(HSSFSheet sheet) {
-
this.sheet = sheet;
- }
-
-
-
public HSSFWorkbook getWb() {
-
return wb;
- }
-
-
-
public void setWb(HSSFWorkbook wb) {
-
this.wb = wb;
- }
-
-
-
public void createNormalHead(String headString, int colSum) {
-
-
HSSFRow row = sheet.createRow(0);
-
-
-
HSSFCell cell = row.createCell(0);
-
row.setHeight((short) 400);
-
-
- cell.setCellType(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue(new HSSFRichTextString("南京城区各网点进件统计报表"));
-
-
-
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) colSum));
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
-
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
font.setFontName("宋体");
-
font.setFontHeight((short) 300);
- cellStyle.setFont(font);
-
- cell.setCellStyle(cellStyle);
- }
-
-
-
public void createNormalTwoRow(String[] params, int colSum) {
-
HSSFRow row1 = sheet.createRow(1);
-
row1.setHeight((short) 300);
-
-
HSSFCell cell2 = row1.createCell(0);
-
- cell2.setCellType(HSSFCell.ENCODING_UTF_16);
-
cell2.setCellValue(new HSSFRichTextString("统计时间:" + params[0] + "至"
-
+ params[1]));
-
-
-
sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) colSum));
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
font.setFontName("宋体");
-
font.setFontHeight((short) 250);
- cellStyle.setFont(font);
-
- cell2.setCellStyle(cellStyle);
-
- }
-
-
-
public void createColumHeader(String[] columHeader) {
-
-
-
HSSFRow row2 = sheet.createRow(2);
-
-
-
row2.setHeight((short) 600);
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
font.setFontName("宋体");
-
font.setFontHeight((short) 250);
- cellStyle.setFont(font);
-
-
-
-
- cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
-
-
HSSFCell cell3 = null;
-
-
for (int i = 0; i < columHeader.length; i++) {
- cell3 = row2.createCell(i);
- cell3.setCellType(HSSFCell.ENCODING_UTF_16);
- cell3.setCellStyle(cellStyle);
-
cell3.setCellValue(new HSSFRichTextString(columHeader[i]));
- }
-
- }
-
-
-
public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col, short align,
- String val) {
- HSSFCell cell = row.createCell(col);
- cell.setCellType(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue(new HSSFRichTextString(val));
- HSSFCellStyle cellstyle = wb.createCellStyle();
- cellstyle.setAlignment(align);
- cell.setCellStyle(cellstyle);
- }
-
-
-
public void createLastSumRow(int colSum, String[] cellValue) {
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
font.setFontName("宋体");
-
font.setFontHeight((short) 250);
- cellStyle.setFont(font);
-
-
HSSFRow lastRow = sheet.createRow((short) (sheet.getLastRowNum() + 1));
-
HSSFCell sumCell = lastRow.createCell(0);
-
-
sumCell.setCellValue(new HSSFRichTextString("合计"));
- sumCell.setCellStyle(cellStyle);
-
sheet.addMergedRegion(new Region(sheet.getLastRowNum(), (short) 0,
-
sheet.getLastRowNum(), (short) colSum));
-
-
for (int i = 2; i < (cellValue.length + 2); i++) {
- sumCell = lastRow.createCell(i);
- sumCell.setCellStyle(cellStyle);
-
sumCell.setCellValue(new HSSFRichTextString(cellValue[i - 2]));
-
- }
-
- }
-
-
-
public void outputExcel(String fileName) {
-
FileOutputStream fos = null;
-
try {
-
fos = new FileOutputStream(new File(fileName));
- wb.write(fos);
- fos.close();
-
} catch (FileNotFoundException e) {
- e.printStackTrace();
-
} catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
报表生成类:ComplexExportExcelClient.java
- package com.bzu.search.action;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
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.HSSFRichTextString;
-
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.Region;
-
-
-
public class ComplexExportExcelClient {
-
-
private static HSSFWorkbook wb = new HSSFWorkbook();
-
-
private static HSSFSheet sheet = wb.createSheet();
-
-
@SuppressWarnings({ "unchecked", "deprecation" })
-
public static void main(String[] args) {
-
-
ExportExcel exportExcel = new ExportExcel(wb, sheet);
-
-
-
List fialList = new ArrayList();
-
-
fialList.add("申请人未提供任何联系方式");
-
fialList.add("无工作单位信息且未提供收入来源信息");
-
fialList.add("有工作单位但未提供单位地址或电话");
-
fialList.add("家庭地址缺失");
-
fialList.add("客户身份证明资料缺");
-
fialList.add("签名缺失或签名不符合要求");
-
fialList.add("其它");
-
-
List errorList = new ArrayList();
-
-
errorList.add("客户主动取消");
-
errorList.add("个人征信不良");
-
errorList.add("欺诈申请");
-
errorList.add("申请人基本条件不符");
-
errorList.add("申请材料不合规");
-
errorList.add("无法正常完成征信");
-
errorList.add("重复申请");
-
errorList.add("其他");
-
-
-
int number = 2 + fialList.size() * 2 + errorList.size() * 2;
-
-
-
for (int i = 0; i < number; i++) {
-
sheet.setColumnWidth(i, 3000);
- }
-
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
-
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
-
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
-
-
cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
font.setFontName("宋体");
-
font.setFontHeight((short) 200);
- cellStyle.setFont(font);
-
-
-
exportExcel.createNormalHead("南京地区申请资料拒件分析统计", number);
-
-
-
String[] params = new String[] { " 年 月 日", " 年 月 日" };
- exportExcel.createNormalTwoRow(params, number);
-
-
-
HSSFRow row2 = sheet.createRow(2);
-
-
HSSFCell cell0 = row2.createCell(0);
- cell0.setCellStyle(cellStyle);
-
cell0.setCellValue(new HSSFRichTextString("机构代码"));
-
-
HSSFCell cell1 = row2.createCell(1);
- cell1.setCellStyle(cellStyle);
-
cell1.setCellValue(new HSSFRichTextString("支行名称"));
-
-
HSSFCell cell2 = row2.createCell(2);
- cell2.setCellStyle(cellStyle);
-
cell2.setCellValue(new HSSFRichTextString("无效件"));
-
-
HSSFCell cell3 = row2.createCell(2 * fialList.size() + 2);
- cell3.setCellStyle(cellStyle);
-
cell3.setCellValue(new HSSFRichTextString("拒绝件"));
-
-
HSSFRow row3 = sheet.createRow(3);
-
-
-
row3.setHeight((short) 800);
-
-
HSSFCell row3Cell = null;
-
int m = 0;
-
int n = 0;
-
-
-
for (int i = 2; i < number; i = i + 2) {
-
-
if (i < 2 * fialList.size() + 2) {
- row3Cell = row3.createCell(i);
- row3Cell.setCellStyle(cellStyle);
-
row3Cell.setCellValue(new HSSFRichTextString(fialList.get(m)
- .toString()));
- m++;
-
} else {
- row3Cell = row3.createCell(i);
- row3Cell.setCellStyle(cellStyle);
-
row3Cell.setCellValue(new HSSFRichTextString(errorList.get(n)
- .toString()));
- n++;
- }
-
- }
-
-
- row3Cell = row3.createCell(number);
- row3Cell.setCellStyle(cellStyle);
-
row3Cell.setCellValue(new HSSFRichTextString("合计"));
-
-
-
HSSFRow row4 = sheet.createRow(4);
-
-
-
sheet.addMergedRegion(new Region(2, (short) 0, 4, (short) 0));
-
-
-
sheet.addMergedRegion(new Region(2, (short) 1, 4, (short) 1));
-
-
-
int aa = 2 * fialList.size() + 1;
-
sheet.addMergedRegion(new Region(2, (short) 2, 2, (short) aa));
-
-
int start = aa + 1;
-
-
sheet.addMergedRegion(new Region(2, (short) start, 2,
-
(short) (number - 1)));
-
-
-
for (int i = 2; i < number; i = i + 2) {
-
sheet.addMergedRegion(new Region(3, (short) i, 3, (short) (i + 1)));
-
- }
-
-
-
for (int i = 2; i < number; i++) {
-
if (i < 2 * fialList.size() + 2) {
-
-
if (i % 2 == 0) {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
-
cell.setCellValue(new HSSFRichTextString("无效量"));
-
} else {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
-
cell.setCellValue(new HSSFRichTextString("占比"));
- }
-
} else {
-
if (i % 2 == 0) {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
-
cell.setCellValue(new HSSFRichTextString("拒绝量"));
-
} else {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
-
cell.setCellValue(new HSSFRichTextString("占比"));
- }
- }
-
- }
-
-
-
for (int i = 5; i < number; i++) {
-
HSSFRow row = sheet.createRow((short) i);
-
for (int j = 0; j <= number; j++) {
- exportExcel
-
.cteateCell(wb, row, (short) j,
- HSSFCellStyle.ALIGN_CENTER_SELECTION, String
- .valueOf(j));
- }
-
- }
-
-
-
String[] cellValue = new String[number - 1];
-
for (int i = 0; i < number - 1; i++) {
- cellValue[i] = String.valueOf(i);
-
- }
-
exportExcel.createLastSumRow(1, cellValue);
-
-
exportExcel.outputExcel("c:\\拒绝件统计.xls");
-
- }
- }
运行上述两段代码你就会在c盘的根目录下看到一个拒绝件统计.xls文件
Java使用POI实现数据导出excel报表
标签:
原文地址:http://www.cnblogs.com/widow/p/4303357.html