package com.csmn.eg.service;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import com.csmn.base.service.BaseService;
import com.csmn.base.utils.displaytag.PaginatedListHelper;
import com.csmn.test.common.po.Aircraft;
/**
* <p>
* ImExPortService類主要用於-.
* </p>
* <p>
* 創建時間 Jul 8, 2012 - 4:55:18 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* <p>
* copyright csmn 2010-2012, all rights reserved.
* </p>
* @author 車水碼農
* @version 1.0
*/
public interface ImExPortService extends BaseService<Aircraft, String> {
/**
* <p>
* getDisplayData方法-获取分页数据集合.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 5:04:58 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param aircraft 查询参数
* @param start 开始页
* @param limit 偏移量
* @return PaginatedListHelper 总页数/页面集合数据
*/
PaginatedListHelper getDisplayData(Aircraft aircraft, int start, int limit);
/**
* <p>
* getAircraftList方法-获取所有列表数据.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 5:30:59 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @return List<Aircraft>
*/
List<Aircraft> getAircraftList();
/**
* <p>
* importExceling方法-导入excel到数据库.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 5:35:12 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param file excel文件
* @return String 提示信息
*/
String importExceling(File excelFile);
/**
* <p>
* exportExceling方法-导出数据到excel中,poi3.8.jar.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 5:41:10 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param aircraft 查询参数
* @return InputStream 下载输出流
*/
InputStream exportExceling(Aircraft aircraft);
/**
* <p>
* exportWording方法-导出数据到word中,itext2.1.7.jar.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 10:14:44 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param aircraft
* @return
*/
InputStream exportWording(Aircraft aircraft);
/**
* <p>
* exportPdfing方法-导出数据到pdf中,itextpdf5.3.jar.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 10:16:19 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param aircraft
* @return
*/
InputStream exportPdfing(Aircraft aircraft);
}
package com.csmn.eg.service.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import com.csmn.base.service.impl.BaseServiceImpl;
import com.csmn.base.utils.displaytag.PaginatedListHelper;
import com.csmn.base.utils.poi.ExcelUtil;
import com.csmn.base.utils.poi.WordUtil;
import com.csmn.eg.service.ImExPortService;
import com.csmn.test.common.po.Aircraft;
/**
* <p>
* ImExportServiceImpl類主要用於-.
* </p>
* <p>
* 創建時間 Jul 8, 2012 - 5:44:54 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* <p>
* copyright csmn 2010-2012, all rights reserved.
* </p>
* @author 車水碼農
* @version 1.0
*/
public class ImExportServiceImpl extends BaseServiceImpl<Aircraft, String> implements
ImExPortService {
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
.getLog(ImExportServiceImpl.class);
String[] pattern = new String[] {
"yyyyMM",
"yyyy-MM",
"yyyy/MM",
"yyyyMMdd",
"yyyy-MM-dd",
"yyyy/MM/dd",
"yyyyMMddHHmmss",
"yyyy-MM-dd HH:mm:ss",
"yyyy/MM/dd HH:mm:ss"};
/**
* {@inheritDoc}
*/
@Override
public InputStream exportExceling(Aircraft aircraft) {
String[] titles = new String[] {"机型", "展翼(m)", "机长(m)", "机高(m)", "标准载客数(人)", "巡航速度(km/h)", "航程(km)", "空机重(t)", "商载(t)", "最大起飞重(t)", "最大燃油量(t)", "动力装置", "发动机", "推力(磅)", "首飞时间"};
Integer[] colWidths = new Integer[] {15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 30, 40, 15, 20};
ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream targetFile = null;
try {
// 处理excel内容页
org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = makeExcel(this.getAircraftList(), titles, colWidths);
workbook.write(output);
byte[] ba = output.toByteArray();
targetFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
log.error(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e));
}
return targetFile;
}
/**
* <p>
* makeExcel方法-将数据写入excel.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 6:28:06 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param aircraftList 数据
* @param titles 表头
* @param colWidths 列宽
* @return HSSFWorkbook
*/
private org.apache.poi.hssf.usermodel.HSSFWorkbook makeExcel(List<Aircraft> aircraftList,
String[] titles, Integer[] colWidths) {
org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = null;
// 这里的数据即时你要从后台取得的数据
// 创建工作簿实例
workbook = new org.apache.poi.hssf.usermodel.HSSFWorkbook();
// 创建工作表实例
org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.createSheet("部分航空器数据信息表");
// 设置列宽
// this.setSheetColumnWidth(sheet);
// 获取样式,如果你没有样式,可以将下面四个样式设置为null
org.apache.poi.hssf.usermodel.HSSFCellStyle titleStyle1 = ExcelUtil.createTitleStyle1(workbook);
org.apache.poi.hssf.usermodel.HSSFCellStyle titleStyle2 = ExcelUtil.createTitleStyle2(workbook);
org.apache.poi.hssf.usermodel.HSSFCellStyle tableStyle1 = ExcelUtil.createTableStyle1(workbook);
org.apache.poi.hssf.usermodel.HSSFCellStyle tableStyle2 = ExcelUtil.createTableStyle2(workbook);
if (aircraftList != null && aircraftList.size() > 0) {
short currentRow = 0;
// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
org.apache.poi.hssf.usermodel.HSSFRow row = sheet.createRow(currentRow++);// 建立新行
// row.setHeight((short) 560);//1:20
row.setHeightInPoints(28);// 像素高
ExcelUtil.createCell(row, 0, titleStyle1, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING, "航空器数据信息表");
// 合并单元格
sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, currentRow++, 0, titles.length-1));
row = sheet.createRow(currentRow++);// 建立新行
row.setHeightInPoints(28);
for (int i = 0; i < titles.length; i++) {
ExcelUtil.createCell(row, i, titleStyle2, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING, titles[i]);
sheet.setColumnWidth(i, colWidths[i]*256);//设置列宽,50个字符宽度。宽度参数为1/256,故乘以256
}
// 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
short col = 0;
for (Aircraft aircraft : aircraftList) {
// 建立新行
row = sheet.createRow(currentRow++);
row.setHeightInPoints(28);
col = 0;
// 你可以通过下面一行代码实现ExcelUtil.createCell方法
// row.createCell(col++).setCellValue(aircraft.getProduct());
ExcelUtil.createCell(row, col++, tableStyle1, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING, aircraft.getProduct());
// ...............................
ExcelUtil.createCell(row, col++, tableStyle1, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_NUMERIC, aircraft.getThrust());
if (aircraft.getFirstFlyDate() != null) {
ExcelUtil.createCell(row, col, tableStyle2, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING, DateFormatUtils.format(aircraft.getFirstFlyDate(), "yyyy-MM-dd"));
} else {
ExcelUtil.createCell(row, col, tableStyle2, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING, "");
}
}
} else {
ExcelUtil.createCell(sheet.createRow(0), 0, titleStyle1, org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING, "查无资料");
}
return workbook;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public List<Aircraft> getAircraftList() {
String hql = "FROM Aircraft a";
return (List<Aircraft>) super.listOnly(hql, null);
}
/**
* {@inheritDoc}
*/
@Override
public PaginatedListHelper getDisplayData(Aircraft aircraft, int start,
int limit) {
// TODO Auto-generated method stub
return null;
}
/**
* {@inheritDoc}
*/
@Override
public String importExceling(File excelFile) {
// 将传入的File构造为FileInputStream;
FileInputStream inputStream;
try {
inputStream = new FileInputStream(excelFile);
// 得到工作表
org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = new org.apache.poi.hssf.usermodel.HSSFWorkbook(inputStream);
// 得到第一页
org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.getSheetAt(0);
// 得到第一面的所有行
Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.rowIterator();
// 得到第一行,也就是标题行(一般导入时通常是略过该行)
org.apache.poi.ss.usermodel.Row titleRow = rowIterator.next();
// 得到正文
while (rowIterator.hasNext()) {
org.apache.poi.ss.usermodel.Row row = rowIterator.next();
Aircraft aircraft = this.creatObjectByRow(row);
super.save(aircraft);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e));
} catch (IOException e) {
e.printStackTrace();
log.error(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e));
} catch (ParseException e) {
e.printStackTrace();
log.error(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e));
}
return "导入数据成功!";
}
/**
* <p>
* creatObjectByRow方法-读取excel中的每一行数据构造成实体对象.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 5:54:17 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param row excel中的行
* @return Aircraft 实体对象
* @throws ParseException
*/
private Aircraft creatObjectByRow(org.apache.poi.ss.usermodel.Row row) throws ParseException {
Aircraft aircraft = new Aircraft();
int col = 0;
String cellValue = "";
cellValue = this.getCellValue(row.getCell(col++));
if (StringUtils.isNotBlank(cellValue)) {
aircraft.setProduct(cellValue);
}
// ........................
cellValue = this.getCellValue(row.getCell(col++));
if (StringUtils.isNotBlank(cellValue)) {
aircraft.setFirstFlyDate(DateUtils.parseDate(cellValue, pattern));
}
return aircraft;
}
/**
* <p>
* getCellValue方法-读取excel中的单元格数据.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 6:00:26 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param cell
* @return
*/
private String getCellValue(org.apache.poi.ss.usermodel.Cell cell) {
String cellValue = "";
DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
if (cell != null) {
switch (cell.getCellType()) {
case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING: // 字符
cellValue = StringUtils.trimToEmpty(cell.getStringCellValue());
break;
case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
cellValue = StringUtils.trimToNull(cell.getStringCellValue());
break;
case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_NUMERIC: // 数字
/*if (HSSFDateUtil.isCellDateFormatted(cell)) {// 是否为日期格式
object = String.valueOf(cell.getDateCellValue());
} else {
Double cellValue = cell.getNumericCellValue();// 读取cell内数据
if (String.valueOf(cellValue).length() > 11) { // 如果读取到的是手机号码,需要匹配数字格式
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
//format.applyPattern("00000000000");
object = format.format(cellValue);
} else { // 如果读取到的是比较短的数字,则去掉尾数(.0)后显示 TODO 未完
object = StringUtils.substring(String.valueOf(cellValue), 0);
}
}*/
// FIXME 这里还是有些小问题的,比如读取整数时
if("@".equals(cell.getCellStyle().getDataFormatString())) {
cellValue = df.format(cell.getNumericCellValue());
} else if("General".equals(cell.getCellStyle().getDataFormatString())) {
cellValue = nf.format(cell.getNumericCellValue());
} else {
cellValue = sdf.format(org.apache.poi.hssf.usermodel.HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
break;
case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_FORMULA: // 公式
cellValue = StringUtils.trimToEmpty(cell.getCellFormula());
break;
case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_BLANK: // 空
cellValue = StringUtils.trimToEmpty(cell.getStringCellValue());
break;
case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_ERROR: // 异常
cellValue = String.valueOf(cell.getErrorCellValue());
default:
break;
}
} else {
// TODO
}
return cellValue;
}
/**
* {@inheritDoc}
*/
@Override
public InputStream exportPdfing(Aircraft aircraft) {
// 首先定义个document
com.itextpdf.text.Document document = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4.rotate());
// 新建字节数组输出流
ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream targetFile = null;
try {
// 然后得到pdfWriter一般用不到,高级的用的到
com.itextpdf.text.pdf.PdfWriter.getInstance(document, output);
// 打开文档
document.open();
// 处理word
document = this.makePdf(document, this.getAircraftList());
// 关闭文档
document.close();
byte[] ba = output.toByteArray();
targetFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
} catch (com.itextpdf.text.DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return targetFile;
}
/**
* <p>
* makePdf方法-.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 11:09:45 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param document
* @param aircraftList
* @return
* @throws DocumentException
* @throws IOException
*/
private com.itextpdf.text.Document makePdf(com.itextpdf.text.Document document, List<Aircraft> aircraftList) throws com.itextpdf.text.DocumentException, IOException {
// 表头
String[] titles = new String[] {"机型", "展翼(m)", "机长(m)", "机高(m)", "标准载客数(人)", "巡航速度(km/h)", "航程(km)", "空机重(t)", "商载(t)", "最大起飞重(t)", "最大燃油量(t)", "动力装置", "发动机", "推力(磅)", "首飞时间"};
// 列宽百分比
// int[] colWidths = new int[] {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 6, 8};
// 下面这句要报错
// BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.itextpdf.text.pdf.BaseFont bfChinese = com.itextpdf.text.pdf.BaseFont.createFont("c://windows//fonts//simsun.ttc,1", com.itextpdf.text.pdf.BaseFont.IDENTITY_H, com.itextpdf.text.pdf.BaseFont.EMBEDDED);
com.itextpdf.text.Font f8 = new com.itextpdf.text.Font(bfChinese, 8, com.itextpdf.text.Font.NORMAL);
int tableCol = titles.length;
com.itextpdf.text.pdf.PdfPTable pdfpTable = new com.itextpdf.text.pdf.PdfPTable(tableCol);
pdfpTable.setWidthPercentage(100);
pdfpTable.setHorizontalAlignment(com.itextpdf.text.pdf.PdfPCell.ALIGN_LEFT);
// 设置table的title
com.itextpdf.text.pdf.PdfPCell titleCell_1 = new com.itextpdf.text.pdf.PdfPCell();
titleCell_1.setBackgroundColor(new com.itextpdf.text.BaseColor(213, 141, 69));
titleCell_1.setHorizontalAlignment(com.itextpdf.text.pdf.PdfPCell.ALIGN_CENTER);
titleCell_1.setPhrase(new com.itextpdf.text.Paragraph("航空器数据信息表", f8));
titleCell_1.setColspan(tableCol);
pdfpTable.addCell(titleCell_1);
com.itextpdf.text.pdf.PdfPCell titleCell = new com.itextpdf.text.pdf.PdfPCell();
titleCell.setBackgroundColor(new com.itextpdf.text.BaseColor(213, 141, 69));
titleCell.setHorizontalAlignment(com.itextpdf.text.pdf.PdfPCell.ALIGN_CENTER);
for (int i = 0; i < tableCol; i++) {
titleCell.setPhrase(new com.itextpdf.text.Paragraph(titles[i], f8));
pdfpTable.addCell(titleCell);
}
com.itextpdf.text.pdf.PdfPCell contextCell = new com.itextpdf.text.pdf.PdfPCell();
contextCell.setHorizontalAlignment(com.itextpdf.text.pdf.PdfPCell.ALIGN_CENTER);
if ((aircraftList != null) && (aircraftList.size() != 0)) {
for (Aircraft aircraft : aircraftList) {
contextCell.setPhrase(new com.itextpdf.text.Paragraph(aircraft.getEngineProduct(), f8));
pdfpTable.addCell(contextCell);
// .................................
if (aircraft.getFirstFlyDate() != null) {
contextCell.setPhrase(new com.itextpdf.text.Paragraph(DateFormatUtils.format(aircraft.getFirstFlyDate(), "yyyy-MM-dd"), f8));
} else {
contextCell.setPhrase(new com.itextpdf.text.Paragraph("", f8));
}
pdfpTable.addCell(contextCell);
}
}
document.add(pdfpTable);
return document;
}
/**
* {@inheritDoc}
*/
@Override
public InputStream exportWording(Aircraft aircraft) {
// 创建Document对象(word文档)
com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.A4);
// 新建字节数组输出流
ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream targetFile = null;
try {
// 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中
com.lowagie.text.rtf.RtfWriter2.getInstance(document, output);
// 打开文档
document.open();
// 处理word
document = this.makeWord(document, this.getAircraftList());
// 关闭文档
document.close();
byte[] ba = output.toByteArray();
targetFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
} catch (com.lowagie.text.DocumentException e) {
e.printStackTrace();
log.error(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e));
} catch (IOException e) {
e.printStackTrace();
}
return targetFile;
}
/**
* <p>
* makeWord方法-将数据写入word.
* </p>
* <p>
* 創建人 車水碼農 創建時間 Jul 8, 2012 - 10:28:32 PM
* </p>
* <blockquote>
* <h4>歷史修改記錄</h4>
* <ul>
* <li>修改人 修改時間 修改描述
* </ul>
* </blockquote>
* @param document
* @param aircraftList
* @return
* @throws DocumentException
*/
private com.lowagie.text.Document makeWord(com.lowagie.text.Document document, List<Aircraft> aircraftList) throws com.lowagie.text.DocumentException {
// 表头
String[] titles = new String[] {"机型", "展翼(m)", "机长(m)", "机高(m)", "标准载客数(人)", "巡航速度(km/h)", "航程(km)", "空机重(t)", "商载(t)", "最大起飞重(t)", "最大燃油量(t)", "动力装置", "发动机", "推力(磅)", "首飞时间"};
// 列宽百分比
int[] colWidths = new int[] {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 6, 8};
com.lowagie.text.Table table = null;
// 行数(集合的大小)
int totleRow = aircraftList.size();
if (0 == totleRow) {
totleRow = 1;
}
// 列数
int tableCol = titles.length;
// 列、行,构造表(加3,是因为表头占据了3行)
table = new com.lowagie.text.Table(tableCol, totleRow + 3);
// 设置每列所占比例
table.setWidths(colWidths);
// 表格所占页面宽度
table.setWidth(100);
// 居中显示
table.setAlignment(com.lowagie.text.Element.ALIGN_CENTER);
// 自动填满
table.setAutoFillEmptyCells(true);
// 第一行(标题)
String titleString = "这是一个测试列表";
com.lowagie.text.Paragraph title = new com.lowagie.text.Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(com.lowagie.text.Element.ALIGN_CENTER);
// RtfFont titleFont = new RtfFont("仿宋_GB2312", 12, Font.NORMAL, Color.BLACK);
title.setFont(WordUtil.titleFont);
document.add(title);
// 表头,表格标题
com.lowagie.text.Cell cellHeader = new com.lowagie.text.Cell(new com.lowagie.text.Phrase("航空器数据信息表", WordUtil.titleFont));
cellHeader.setColspan(tableCol);
cellHeader.setRowspan(2);
cellHeader.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
cellHeader.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
table.addCell(cellHeader);
com.lowagie.text.Cell[] cellHeaders = new com.lowagie.text.Cell[tableCol];
for (int i = 0; i < tableCol; i++) {
cellHeaders[i] = new com.lowagie.text.Cell(new com.lowagie.text.Phrase(titles[i], WordUtil.contextFont));
// 居中显示
cellHeaders[i].setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
// 纵向居中显示
cellHeaders[i].setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
table.addCell(cellHeaders[i]);
}
// 向表格填充数据
totleRow = aircraftList.size();
if (totleRow != 0) {
for (Aircraft aircraft : aircraftList) {
// 内容列
com.lowagie.text.Cell contextCell = new com.lowagie.text.Cell();
// RtfFont contextFont = new RtfFont("仿宋_GB2312", 9, Font.NORMAL, Color.BLACK);
contextCell = new com.lowagie.text.Cell(new com.lowagie.text.Phrase(aircraft.getProduct(), WordUtil.contextFont));
table.addCell(contextCell);
// ..................................
if (aircraft.getFirstFlyDate() != null) {
contextCell = new com.lowagie.text.Cell(new com.lowagie.text.Phrase(DateFormatUtils.format(aircraft.getFirstFlyDate(), "yyyy-MM-dd"), WordUtil.contextFont));
} else {
contextCell = new com.lowagie.text.Cell(new com.lowagie.text.Phrase("", WordUtil.contextFont));
}
table.addCell(contextCell);
}
} else {
// 没有可用的数据
com.lowagie.text.Cell noRowData = new com.lowagie.text.Cell(new com.lowagie.text.Phrase("没有可用的数据用于导出!", WordUtil.contextFont));
noRowData.setColspan(tableCol);
table.addCell(noRowData);
}
document.add(table);
return document;
}
}