public class CommonExcelMgr extends ExcelMgr {
private List list = null;
private String filePath = null;
private String name = null;
private String time = null;
@Override
public String getReportName() throws Exception {
if (name == null)
throw new Exception();
return this.name;
}
@Override
public String getReportTime() throws Exception {
if (name == null)
throw new Exception();
return this.time;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
protected int getObjectListLen() throws Exception {
if (list == null)
throw new Exception();
return list.size();
}
@Override
protected String superGetFilePath() throws Exception {
if (filePath == null)
throw new Exception();
if (filePath.length() == 0)
throw new Exception();
return filePath;
}
@Override
protected Object[] getListObject(int index) throws Exception {
return (Object[]) list.get(index);
}
@Override
protected int getObjectSize() {
return 9;
}
@Override
protected int getTitleListLen() {
return titleList.length;
}
@Override
protected String getTitleValue(int index) {
String title = "";
title = titleList[index];
return title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String[] getTitleList() {
return titleList;
}
public void setTitleList(String[] titleList) {
this.titleList = titleList;
}
@Override
protected String getValue(Object[] object, int row, int column, String flag)
throws Exception {
String value = "";
for (int i = 0; i < object.length; i++) {
value = object[column] != null ? object[column].toString() : " ";
}
return value;
}
public boolean expoxt(List list, String flag) {
// 生成Excel对象
HSSFWorkbook wb = new HSSFWorkbook();
// 生成Excel中某一个sheet对象
HSSFSheet sheetExport = wb.createSheet();
String localFlag = flag;
try {
// 生成表名,在第一行第5列
{
HSSFRow reportName = sheetExport.createRow((short) 0);
HSSFCell cell3 = reportName.createCell((short) 3);
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//cellstyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index); // 设置单元格颜色
cell3.setCellStyle(cellstyle);
sheetExport.addMergedRegion(new Region(0, (short) 3, 0,
(short) 4));// 合并单元格
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setCellValue(getReportName());
//构建假植成本表头
if(localFlag.equals("jiaZhiCost")){
ExcelTitleUtil.jiaZhiCost(sheetExport,cellstyle);
}
}
// 生成列名,列的信息由getTitleListLen()(表示有多少列) ;getTitleValue(index)
// 得到某一列的名称,由index决定
HSSFRow titleRow = sheetExport.createRow((short) 2);
short columnId = 0;
HSSFCell cell = titleRow.createCell(columnId++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
for (int x = 0; x < getTitleListLen(); x++) {
cell = titleRow.createCell(columnId);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(getTitleValue(x));
columnId++;
}
// 生成数据行,有多少行由getObjectListLen()决定,
// 取得某一行的数据用getListObject(index),index表示某一行
for (int i = 0; i < getObjectListLen(); i++) {
Object object = getListObject(i);
HSSFRow row = sheetExport.createRow((short) i + 3);
columnId = 0;
cell = row.createCell(columnId++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i + 1 + startIndex);
// 在某一行中,设置每一列的数据,有多少列由getObjectSize()决定,
// getValue(object,row,column,flag),取得第row行第column列的数据
for (int j = 0; j < titleList.length; j++) {
int fieldIndex = j;
cell = row.createCell(columnId);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(getValue(getListObject(i), i,
fieldIndex + 1, flag));
columnId++;
}
}
// 在最后一行,生成时间
{
HSSFRow reportName = sheetExport
.createRow((short) getObjectListLen() + 3);
cell = reportName.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("生成时间");
cell = reportName.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(getReportTime());
}
// 生成文件,文件名和路径由superGetFilePath()决定
File file = new File(superGetFilePath());
if (!file.exists())
file.createNewFile();
// 将Excel对象输出到文件
FileOutputStream fOut;
fOut = new FileOutputStream(superGetFilePath());
wb.write(fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
}