|
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
* @author 刘毅
* @date 2010-1-12
* @param
*/
public class CreateExcelForCreateDateCells {
/***
* 创建空的xls格式的文档,并根据行号创建cell,创建日期格式
*/
public void createXlsForDateCell(){
Workbook wb = new HSSFWorkbook();
//创建的帮助类
CreationHelper createHelper = wb.getCreationHelper();
//cell样式
CellStyle cellStyle = wb.createCellStyle();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
//直接NEW出来的日期.
cell.setCellValue(new Date());
//经过样式处理过的日期
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// cell = row.createCell(2);
// cell.setCellValue(Calendar.getInstance());
// cell.setCellStyle(cellStyle);
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("src/workbook1.xls");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CreateExcelForCreateDateCells create = new CreateExcelForCreateDateCells();
create.createXlsForDateCell();
}
} |
|