使用Java写入excel文件时无法更改文本的字体颜色

时间:2011-06-09 11:32:48

标签: java excel selenium

我需要从Excel工作表中获取测试数据,并在另一个Excel工作表中将测试结果写为Pass / Fail。 我正在使用SeleniumRC + Java + Eclipse + Junit。

如何在使用Java写入Excel工作表时更改文本的字体颜色。

我在Label,WorkBook,WrittableWorkbook,WrittableSheet,Sheet的帮助下做。

这是我的示例代码..

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class FinalTestSuite extends TestCase 
{
public static  File file = new File("C:/Selenium/selenium-remote-control-1.0.3/TestingResult.xls");
public static  File sourceFile = new File("C:/Selenium/selenium-remote-control-1.0.3/Portland.xls");
public static  WorkbookSettings ws;
public static  WritableWorkbook workbook;
public static  WritableSheet wSheet1, wSheet2;
public static  Sheet sh1, sh2;
public static  Sheet sheetFinal;
public static  Workbook sourceWorkbook;
public static  Label label;
}


public void getTestResult(String passOrfail) throws IOException,BiffException, WriteException

{
try
{

FinalTestSuite.file = new File("C:/Selenium/selenium-remote-control-1.0.3/TestingResult.xls");
FinalTestSuite.sourceFile = new File("C:/Selenium/selenium-remote-control-1.0.3/TestDetails.xls");
FinalTestSuite.ws = new WorkbookSettings();
FinalTestSuite.workbook = Workbook.createWorkbook(FinalTestSuite.file, FinalTestSuite.ws);
                FinalTestSuite.sourceWorkbook=Workbook.getWorkbook(FinalTestSuite.sourceFile);
FinalTestSuite.sh1 = FinalTestSuite.sourceWorkbook.getSheet(0);
FinalTestSuite.sh2= FinalTestSuite.sourceWorkbook.getSheet(1);
FinalTestSuite.wSheet1 = FinalTestSuite.workbook.importSheet("Begin", 0 , FinalTestSuite.sh1);
FinalTestSuite.wSheet2 = FinalTestSuite.workbook.importSheet("Reports", 0 , FinalTestSuite.sh2);
colCount = FinalTestSuite.wSheet2.getColumns();

rowCount = FinalTestSuite.wSheet2.getRows();

                for(col=1;col<=colCount;col++)
                {

                    for(row = 1;row<=rowCount;row++)
                    {
                        jxl.Cell dataCell =  FinalTestSuite.wSheet2.getCell(col, 0);

                        if(clientName.equals(client))
                        {
                            FinalTestSuite.label = new Label(col, row, passOrfail);
                            FinalTestSuite.wSheet2.addCell(FinalTestSuite.label);
                            break;                          


                        }

                    }
                }

        }
        catch(Exception e)
        {
             e.printStackTrace();
        } 

请有人告诉我如何修改它以获得测试结果,字体颜色为红色和绿色...

1 个答案:

答案 0 :(得分:0)

在格式化您要写入的单元格时使用WritableFont.setColour(Colour colour)。有关基础知识,请参阅this tutorial。 API应该足以知道如何为可用的格式化示例添加颜色。