import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Vector;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile;
private Vector testData = null;
private Vector excelObjects = null;
public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}
public void write() throws IOException, WriteException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
private void createLabel(WritableSheet sheet)
throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);
// Create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(
WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
//cv.setAutosize(true);
// Write a few headers
addCaption(sheet, 0, 0, "TestCase");
addCaption(sheet, 1, 0, "Action");
addCaption(sheet, 2, 0, "ServiceType");
addCaption(sheet, 3, 0, "InputParam");
addCaption(sheet, 4, 0, "Status");
}
private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
ExcelObject excelObj = null;
int column = 0;
int row = 1;
Enumeration enumObj = testData.elements();
while(enumObj.hasMoreElements()) {
excelObj = (ExcelObject) enumObj.nextElement();
addLabel(sheet, column, row, excelObj.testCaseId);
addLabel(sheet, ++column, row, excelObj.actionType);
addLabel(sheet, ++column, row, excelObj.serviceType);
addLabel(sheet, ++column, row, excelObj.params);
addLabel(sheet, ++column, row, excelObj.status);
column = 0;
++row;
}
}
private void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}
private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}
public static void main(String[] args) throws WriteException, IOException {
ExportToExcel test = new ExportToExcel();
Vector testData = test.populateTestData();
test.setOutputFile("C:\\TestXSL\\Book1.xls");
//test.write();
test.write(testData);
System.out
.println("Please check the result file under C:/TestXSL/Book1.xls ");
}
}
Post a Comment