Java写入excel数据
FileInputStream file = new FileInputStream ( "E:\\testr.xls" ) ;
HSSFWorkbook workbook = new HSSFWorkbook ( file) ;
Font font = workbook. createFont ( ) ;
font. setFontHeightInPoints ( ( short) 20 ) ;
font. setFontName ( "宋体" ) ;
font. setColor ( Font. COLOR_RED ) ;
CellStyle cellStyle = workbook. createCellStyle ( ) ;
cellStyle. setFont ( font) ;
HSSFSheet sheet1 = workbook. getSheetAt ( 0 ) ;
Row row = sheet1. getRow ( 2 ) ;
Cell column = row. getCell ( 2 ) ;
String updatename = "fail" ;
column. setCellValue ( updatename) ;
column. setCellStyle ( cellStyle) ;
file. close ( ) ;
FileOutputStream out = new FileOutputStream ( "E:\\testr.xls" ) ;
workbook. write ( out) ;
out. close ( ) ;
python写入excel数据
class WriteExcel ( ) :
"" "文件写入数据" ""
def __init__ ( self, fileName) :
self. filename = fileName
if not os. path. exists ( self. filename) :
# 文件不存在,则拷贝模板文件至指定报告目录下
shutil. copyfile ( setting. SOURCE_FILE , setting. TARGET_FILE )
self. wb = load_workbook ( self. filename)
self. ws = self. wb. active# 激活sheet
def write_data ( self, row_n, value) :
"" "
写入测试结果
: param row_n: 数据所在行数
: param value: 测试结果值
: return : 无
"" "
font_GREEN = Font ( name= '宋体' , color= GREEN , bold= True)
font_RED = Font ( name= '宋体' , color= RED , bold= True)
#以上定义字体颜色
font1 = Font ( name= '宋体' , color= DARKYELLOW , bold= True)
align = Alignment ( horizontal= 'center' , vertical= 'center' ) #定义字体格式
# 获数所在行数
L_n = "L" + str ( row_n)
M_n = "M" + str ( row_n)
if value == "PASS" :
self. ws. cell ( row_n, 12 , value) #写入数据 指定行指定12 列
self. ws[ L_n] . font = font_GREEN #根据表头给字体分配颜色
if value == "FAIL" :
self. ws. cell ( row_n, 12 , value)
self. ws[ L_n] . font = font_RED #根据表头给字体分配颜色
self. ws. cell ( row_n, 13 , name)
self. ws[ L_n] . alignment = align #设置字体样式居中
self. ws[ M_n] . font = font1
self. ws[ M_n] . alignment = align
self. wb. save ( self. filename) #写入后保存