poi 生成excel文件.xls或者.xls实例,包括日期写入处理

时间:2023-01-22 11:44:35
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportExcel {

<span style="white-space:pre"> </span>private String excelPath = "E://haha.xlsx";

public static void main(String[] args) throws Exception{
ExportExcel excel = new ExportExcel();
if(excel.createExcelFile()) {
System.out.println("data.xlsx is created successfully.");
}
}
public boolean createExcelFile() {
int flag=0;
boolean isCreateSuccess = false;
Workbook workbook = null;
try {
workbook = new XSSFWorkbook();
}catch(Exception e) {
System.out.println("It cause Error on CREATING excel workbook: ");
e.printStackTrace();
}
if(workbook != null) {
Sheet sheet = workbook.createSheet("testdata");
Row row0 = sheet.createRow(0);
row0.setHeight((short)1000);//设置单元格宽和高
sheet.setColumnWidth((short) 0, (short) 250);
for(int i = 0; i < 13; i++) {
Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);
flag=0;
CellStyle style = getStyle(workbook,flag,0);
cell_1.setCellStyle(style);
//此处是用来设置第一行标题
switch (i) {
case 0:
cell_1.setCellValue("主题(Subject)");
break;
case 1:
cell_1.setCellValue("用例引入ID");
break;
case 2:
cell_1.setCellValue("用例修改状态");
break;
case 3:
cell_1.setCellValue("用例名称");
break;
case 4:
cell_1.setCellValue("用例等级");
break;
case 5:
cell_1.setCellValue("预置条件");
break;
case 6:
cell_1.setCellValue("操作步骤");
break;
case 7:
cell_1.setCellValue("预期结果");
break;
case 8:
cell_1.setCellValue("注释");
break;
case 9:
cell_1.setCellValue("设计者");
break;
case 10:
cell_1.setCellValue("用例修改者");
break;
case 11:
cell_1.setCellValue("用例编写时间");
break;
case 12:
cell_1.setCellValue("用例修改时间");
break;
}
sheet.autoSizeColumn(i);
}

for (int rowNum = 1; rowNum < 20; rowNum++) {
Row row = sheet.createRow(rowNum);
row.setHeight((short)1500);
for(int i = 0; i < 13; i++) {
flag=1;
CellStyle style = getStyle(workbook,flag,i);
Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
switch (i) {
case 0:
cell.setCellValue("hhh");
break;
case 1:
cell.setCellValue(1212);
break;
case 2:
cell.setCellValue("qqq");
break;
case 3:
cell.setCellValue("dddd");
break;
case 4:
cell.setCellValue("level_2");
break;
case 5:
cell.setCellValue("ssss");
break;
case 6:
cell.setCellValue("sss");
break;
case 7:
cell.setCellValue("ggg");
break;
case 8:
cell.setCellValue("hhh");
break;
case 9:
cell.setCellValue("jjj");
break;
case 10:
cell.setCellValue("kkjl");
break;
case 11:
cell.setCellValue(new Date());
break;
case 12:
cell.setCellValue(new Date());
break;
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(excelPath);
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
isCreateSuccess = true;
} catch (Exception e) {
System.out.println("It cause Error on WRITTING excel workbook: ");
e.printStackTrace();
}
}
File sss = new File(excelPath);
System.out.println(sss.getAbsolutePath());
return isCreateSuccess;
}
private CellStyle getStyle(Workbook workbook,int flag,int i){//flag是用来判断是否是标题,从而来设置不同的标题,i是用来判断是某列,来对某列的样式进行设定
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//给单元格设置底色,如果要给单元格设置颜色,必须先设置底色才行,否则颜色出不来
if(flag==0){
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//设置标题颜色
}else {
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);//设置正文背景色
}

if(i==11||i==12){//因为第12行13行是要写入日期,所要预先设置日期格式,然后直接设置,注意是date类型
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));//设置日期格式
}

// 设置单元格字体
Font headerFont = workbook.createFont(); // 字体
headerFont.setFontHeightInPoints((short)14);
if(flag==0){
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
}
headerFont.setFontName("宋体");
style.setFont(headerFont);
style.setWrapText(true);

// 设置单元格边框及颜色
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
style.setWrapText(true);
return style;
}

}