package wrapFunc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.opc.OPCPackage;
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.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
public class CreateExcel {
Date date=new Date();
String excelPath="F:\\測(cè)試日志\\"+String.valueOf(DateUtil.getYear(date))+"-"+String.valueOf(DateUtil.getMonth(date))+"-"+String.valueOf(DateUtil.getDay(date))+"-"+String.valueOf(DateUtil.getHour(date))+"-"+String.valueOf(DateUtil.getMinute(date))+".xlsx";
public void writeHeader() throws FileNotFoundException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("testdata1");
FileOutputStream outputStream = new FileOutputStream(excelPath);
try {
Row row0 = sheet.createRow(0);
String[] headers = new String[] { "編號(hào)", "檢查點(diǎn)", "預(yù)期結(jié)果", "實(shí)際結(jié)果","檢查結(jié)果", "截圖" };
for (int i = 0; i < headers.length; i++) {
Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);
CellStyle style = getStyle(workbook);
cell_1.setCellStyle(style);
cell_1.setCellValue(headers[i]);
sheet.autoSizeColumn(i);
}
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeRow(String[] inputFile) {
try {
File file = new File(excelPath);
InputStream fis = new FileInputStream(file);
OPCPackage opcPackage = OPCPackage.open(fis);
Workbook wb = new XSSFWorkbook(opcPackage);
Sheet sheet = wb.getSheetAt(0);
sheet.setColumnWidth(1, 30 * 256);//講第二列寬度設(shè)為30個(gè)字符寬度
sheet.setColumnWidth(5, 40 * 256);//講第6列寬度設(shè)為40個(gè)字符寬度
Row row = (Row) sheet.getRow(0);
FileOutputStream out = new FileOutputStream(excelPath);
row = sheet.createRow((sheet.getLastRowNum() + 1));
int rowNum = sheet.getLastRowNum();
for (int i = 0; i < inputFile.length; i++) {
if (i == 0) {
Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING);
cell.setCellValue(rowNum);
}
else {
Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
// cell.setHyperlink((XSSFHyperlink) cell.getHyperlink());
cell.setCellValue(inputFile[i]);
//? ? ? cell.setHyperlink(inputFile[inputFile.length-1]);
}
}
out.flush();
wb.write(out);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private CellStyle getStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 設(shè)置單元格字體
Font headerFont = workbook.createFont(); // 字體
headerFont.setFontHeightInPoints((short) 14);
headerFont.setColor(HSSFColor.RED.index);
headerFont.setFontName("宋體");
style.setFont(headerFont);
style.setWrapText(true);
// 設(shè)置單元格邊框及顏色
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
style.setBorderTop((short) 1);
style.setWrapText(true);
return style;
}
}