测试日志写入自动生成的excel

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:\\测试日志\\"+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[] { "编号", "检查点", "预期结果", "实际结果","检查结果", "截图" };

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);//讲第二列宽度设为30个字符宽度

sheet.setColumnWidth(5, 40 * 256);//讲第6列宽度设为40个字符宽度

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);

// 设置单元格字体

Font headerFont = workbook.createFont(); // 字体

headerFont.setFontHeightInPoints((short) 14);

headerFont.setColor(HSSFColor.RED.index);

headerFont.setFontName("宋体");

style.setFont(headerFont);

style.setWrapText(true);

// 设置单元格边框及颜色

style.setBorderBottom((short) 1);

style.setBorderLeft((short) 1);

style.setBorderRight((short) 1);

style.setBorderTop((short) 1);

style.setWrapText(true);

return style;

}

}

你可能感兴趣的:(测试日志写入自动生成的excel)