package com.enjar.system.xls;
/**
* Copyright 2010 XIAMEN ENJAR INFORMATION Co. Ltd.All right reserved.
* description
* @author <a href="mailto:rjw@enjar.com">ruanjingwang</a>
* @version 1.0
* @date Oct 19, 2011 1:18:28 PM
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
/**
* excel 产生工具类
*/
public abstract class XlsCreator implements XlsCreatorInterface
{
private final Log log = LogFactory.getLog(XlsCreator.class);
protected int lineX = 0;
protected FileOutputStream fos = null;
protected HSSFWorkbook wb;
protected HSSFSheet sheet;
protected HSSFCellStyle dateCellStyle;
protected HSSFCellStyle alignmentCenter;
protected HSSFCellStyle alignmentRight;
protected HSSFCellStyle alignmentLeft;
protected HSSFCellStyle cellStylehead;
protected Object obj;
// 定义IO流
protected ByteArrayOutputStream baos = null;
protected ByteArrayInputStream bais = null;
public abstract void run(File excelfile, String title, String[] arrs) throws Exception;
// 以IO方式输出,不在服务器端产生excel文件
public abstract ByteArrayInputStream run(String title) throws Exception;
public abstract void setColumns(List columns);
public abstract void setDatalist(List dataList);
/**
* 附加数据
* @param dataList
*/
public void appendDatalist(List dataList)
{
lineX++;
}
public String trimToEmpty(String obj)
{
if (obj == null)
return "";
else
{
return obj;
}
}
/**
* 字符串型
* @param cell
* @param value
*/
public void createCell(HSSFCell cell, String value)
{
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if (value.length() < 32767)
{
cell.setCellValue(new HSSFRichTextString(value));
} else
{
log.warn("max length>32767");
cell.setCellValue("");
}
}
/**
* 字符串型
* @param cellStyle
* @param cell
* @param value
*/
public void createCell(HSSFCell cell, String value, HSSFCellStyle cellStyle)
{
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString(value));
}
/**
* 数字型数据
* @param cell
* @param value
*/
public void createCell(HSSFCell cell, double value,HSSFCellStyle cellStyle)
{
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(value);
}
/**
* 单据号码,图书代码等字符串类型的数字,中间加空格显示
* @param cell
* @param value
* @param len 第几位开始加空格
*/
public void createCell(HSSFCell cell, String value, int len)
{
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if (value.length() > len)
value = value.substring(0, len) + " " + value.substring(len, value.length());
cell.setCellValue(new HSSFRichTextString(value));
}
/**
* @param cell
* @param calendar
* @param cellStyle0
*/
public void createCell(HSSFCell cell, Calendar calendar)
{
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(dateCellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(calendar.getTime());
}
/**
* 以百分数显示
* @param workbook
* @param cell
* @param value
*/
public void createCell(HSSFCellStyle cellStyle, HSSFCell cell, double value)
{
cellStyle.setDataFormat((short) 0xa);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue(value);
}
/**
* @param value
* @param cellStyle
* @param cell
*/
public void createCell(double value, HSSFCellStyle cellStyle, HSSFCell cell)
{
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue(value);
}
/**
* 设置了小数位数的小数显示方式
* @param workbook
* @param cell
* @param value
* @param styleLen
*/
public void createCell(HSSFCellStyle cellStyle, double value, HSSFCell cell)
{
cellStyle.setDataFormat((short) 2);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue(value);
}
/**
* 数字型数据,显示格式根据format确定
* @param cell
* @param cellStyle
* @param value
* @param format 数据显示格式
*/
public void createCell(HSSFCell cell, double value, HSSFCellStyle cellStyle, short format)
{
cellStyle.setDataFormat(format);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue(value);
}
/**
* 根据正则表达式判断是否為数字型
* @param source
* @return boolean
*/
public boolean idDigital(Object source)
{
boolean result = false;
if (source != null)
{
String string = source.toString();
if (string.trim().length() > 0)
{
result = Pattern.matches("\\d+.?\\d+", string);
}
}
return result;
}
/**
* 判断是否為日期型
* @param string
* @return
*/
public boolean isCalendar(String source)
{
boolean result = false;
if (source != null)
{
String string = source.toString();
if (string.trim().length() > 0)
{
Pattern pattern = Pattern
.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
result = pattern.matcher(source).matches();
}
}
return result;
}
/**
* 如果参数对象能以String的形式表示,将其转化為String类型并返回,否则返回null
* @param object
* @return
*/
public String getString(Object object)
{
String result = "";
if (object != null && object.toString().trim().length() > 0)
{
result = object.toString().trim();
}
return result;
}
/**
* 如果参数对象能以Double的形式表示,将其转化為Double类型并返回,否则返回null
* @param object
* @return
*/
public Double getDouble(Object object)
{
Double result = 0D;
if (object != null && object.toString().trim().length() > 0)
{
result = Double.valueOf(object.toString().trim());
}
return result;
}
/**
* 如果参数对象能以Calendar的形式表示,将其转化為Calendar类型并返回,否则返回null
* @param object
* @return
*/
public Calendar getCalendar(Object object)
{
Calendar result = null;
if (object != null && object.toString().trim().length() > 0)
{
result = null;//;DateUtil.getCalendar(object.toString(), "yyyy-MM-dd");
}
return r
- 1
- 2
- 3
前往页