import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XLSExport {
private static String DATE_FORMAT = "m/d/yy"; // "m/d/yy h:mm" // 日期格式
private static String NUMBER_FORMAT = "#,##0.00"; // 浮点数格式
private String xlsFileName;
private HSSFWorkbook workbook;
private HSSFSheet sheet;
private HSSFRow row;
private HSSFCellStyle dateCellStyle; // 日期的单元格样式
private HSSFCellStyle doubleCellStyle; // 日期的单元格样式
/**
* 初始化Excel
*
* @param fileName
* 导出文件名
*/
public XLSExport(String fileName) {
this.xlsFileName = fileName;
this.workbook = new HSSFWorkbook();
this.sheet = workbook.createSheet();
dateCellStyle = workbook.createCellStyle(); // 建立date类型cell样式
dateCellStyle.setDataFormat(HSSFDataFormat
.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
doubleCellStyle = workbook.createCellStyle(); // 建立double类型cell样式
HSSFDataFormat format = workbook.createDataFormat();
doubleCellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
// 设置表格默认列宽度为20个字节
// sheet.setDefaultColumnWidth(10);
}
/**
* 导出Excel文件
*/
public void exportXLS() {
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(xlsFileName);
workbook.write(fOut);// 将写好的excel写入文件流
fOut.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fOut != null)
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建行
*
* @param index
* 行号
*/
public void createRow(int index) {
this.row = this.sheet.createRow(index);
}
/**
* 创建单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell(int index, Object value) {
HSSFCell cell = this.row.createCell(index);
if (value == null) {// 单元格值不允许为NULL
return;
}
// 各种类型的匹配
if (value instanceof Date) {
cell.setCellValue((Date) value);
// 指定当单元格内容显示不下时自动换行
// cellStyle.setWrapText(true);
cell.setCellStyle(dateCellStyle); // 设置该cell日期的显示格式
} else if (value instanceof Calendar) {
cell.setCellValue(((Calendar) value).getTime());
cell.setCellStyle(dateCellStyle); // 设置该cell日期的显示格式
} else if (value instanceof Number) {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.valueOf(value.toString()));
if (value instanceof Double || value instanceof BigDecimal
|| value instanceof Float) {
cell.setCellStyle(doubleCellStyle); // 设置该cell浮点数的显示格式
}
} else {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(value.toString());
}
}
public static void main(String[] args) throws Exception {
String fileName = "d:/xlstest.xls";
XLSExport e = new XLSExport(fileName);
e.createRow(0);
e.setCell(0, " 订单号 ");
e.setCell(1, " 订单名 ");
e.setCell(2, " 价格");
e.setCell(3, " 创建日期 ");
e.createRow(1);
e.setCell(0, 1L);
e.setCell(1, " aa ");
e.setCell(2, 111.11111);
e.setCell(3, Calendar.getInstance());
e.createRow(2);
e.setCell(0, 2L);
e.setCell(1, " bb ");
e.setCell(2, 222.11);
e.setCell(3, new Date());
e.exportXLS();
}
}