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;
}
}
poi 生成excel文件.xls或者.xls實例,包括日期寫入處理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.