本文概述
- Apache POI Excel日期单元格示例
让我们看一个示例,其中我们将创建一个包含当前日期的单元格。
Apache POI Excel日期单元格示例
package poiexample;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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;
public class DateCellExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
try(OutputStream os = new FileOutputStream("srcmini.xls")){
Sheet sheet = wb.createSheet("New Sheet");
Row row= sheet.createRow(0);
Cell cell= row.createCell(0);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("d/m/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
wb.write(os);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
【apache poi excel日期单元格】输出:
文章图片
推荐阅读
- apache poi excel对齐单元格
- apache poi excel工作簿
- apache poi excel文档处理
- apache poi安装
- apache poi的特性
- apache poi架构
- apache poi的介绍
- apache poi入门教程