0

I want to write excel sheet and for that I write a code, but when my program execute in the object WritableSheet it get the below warning. May I know where I am going wrong?? Also, I am using keydriven framework for writing a sheet.

Warning:  Sheet name D:\eclipse-jee-kepler-SR1-win32\Workspace\AutomationFramework\configuration\GmailTestSuite.xls too long - truncating
Warning:  : is not a valid character within a sheet name - replacing
Warning:  \ is not a valid character within a sheet name - replacing

Code which I am using for writing a sheet:

public class WritableData {

    Workbook wbook;
    WritableWorkbook wwbCopy;
    String ExecutedTestCasesSheet;
    WritableSheet shSheet;

    public WritableData(String testSuitePath, String string) {
        // TODO Auto-generated constructor stub

        try {
            wbook = Workbook.getWorkbook(new File(testSuitePath));
            wwbCopy = Workbook.createWorkbook(new File(testSuitePath));
            // shSheet=wwbCopy.getSheet(1);
            shSheet = wwbCopy.createSheet(testSuitePath, 1);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("Exception message" + e.getMessage());
            e.printStackTrace();
        }
    }

    public void shSheet(String strSheetName, int iColumnNumber, int iRowNumber,
            String strData) throws WriteException {
        // TODO Auto-generated method stub

        WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);
        WritableFont cellFont = null;
        WritableCellFormat cellFormat = null;

        if (strData.equalsIgnoreCase("PASS")) {
            cellFont = new WritableFont(WritableFont.TIMES, 12);
            cellFont.setColour(Colour.GREEN);
            cellFont.setBoldStyle(WritableFont.BOLD);

            cellFormat = new WritableCellFormat(cellFont);
            cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
        }

        else if (strData.equalsIgnoreCase("FAIL")) {
            cellFont = new WritableFont(WritableFont.TIMES, 12);
            cellFont.setColour(Colour.RED);
            cellFont.setBoldStyle(WritableFont.BOLD);

            cellFormat = new WritableCellFormat(cellFont);
            cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
        }

        else {
            cellFont = new WritableFont(WritableFont.TIMES, 12);
            cellFont.setColour(Colour.BLACK);

            cellFormat = new WritableCellFormat(cellFont);
            cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
            cellFormat.setWrap(true);
        }

        Label labTemp = new Label(iColumnNumber, iRowNumber, strData,
                cellFormat);
        try {
            wshTemp.addCell(labTemp);

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

    }

    public void closeFile() {
        try {
            // write the value in work book
            wwbCopy.write();
            // wwbCopy.close();

            // Closing the original work book
            wbook.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}
ChayanC
  • 201
  • 7
  • 29

2 Answers2

1

First you should understand the terminology.

Workbook = the whole Excel document, it is the whole content of the file.

Sheet = part of the workbook, one Excel "page". A workbook can have multiple sheets.

Sheet name is in the small tab in the bottom of your Excel file. Pass there something shorter and more useful, such as Data.

enter image description here


There are multiple problems in your code. I will discuss some of them which are related to your question.

Here you open the file/workbook:

wbook = Workbook.getWorkbook(new File(testSuitePath));

On the next line, you create a new workbook with the same name - by this you overwrite your previous file so it is empty now:

wwbCopy = Workbook.createWorkbook(new File(testSuitePath));

And finally here you create one new sheet with an incorrect name:

shSheet = wwbCopy.createSheet(testSuitePath, 1);

You shouldn't name your sheet the same as your file.


And by the way, in Java you should use '/' in your file names, not '\'. Java will turn all the slashes into the correct character according to the target operating system.

As for your exception handling, read Is it really that bad to catch a general exception? or In Java, what is the difference between catch a generic exception and a specific exception (eg. IOException?). Do not catch a generic exception.

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • Thanks for your kind reply, actually in createSheet it take createSheet(String, int.arg1). So, how can we pass sheet name like you told? – ChayanC Feb 22 '15 at 10:20
  • 1
    Try createSheet("data", 1) :-) supposed that the number means the sheet index within the workbook, check if it starts from 0 or from 1. – Honza Zidek Feb 22 '15 at 10:26
  • via your last comment I am not getting that warning but what happen now I have given sheet name on which I have to write say "Data" as you suggest. But when I am executing a program it delete all the other workbook which it contains and only create one. Why is it so? – ChayanC Feb 23 '15 at 16:01
  • Can you please let me know how to write in existing sheet? Actually I am trying to set status of executed test cases but whenever I'll execute the cases it create new workbook. I read your editable content but don't understand that how I can write in sheet? :( – ChayanC Feb 26 '15 at 16:43
  • I edited my answer so the text which answers your last question is now **in bold**. You cannot get a fish on StackOverflow, but a fishing rod instead. – Honza Zidek Feb 26 '15 at 17:18
0

Looks like you are passing path to excel file in your "testSuitePath" variable. Kindly debug and make sure that you are passing correct value in testSuitePath.

Hope it helps

Mayur Shah
  • 518
  • 3
  • 8