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.Label;
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;
import jxl.Range;
public class ExportToExcel {
private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile;
//private String width;
private Vector testData = null;
public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}
/* public void setColumnWidth(String width ){
this.width = width;
}*/
public Vector populateTestData() {
Vector v = new Vector();
ExcelObject exObj = new ExcelObject();
exObj.testCaseId = "Create_Voice";
exObj.actionType = "Create";
exObj.serviceType = "Voice";
exObj.params = "Test Data : Test Data";
exObj.status = "Passed";
v.add(exObj);
ExcelObject exObj1 = new ExcelObject();
exObj1.testCaseId = "Create_Voice";
exObj1.actionType = "Create";
exObj1.serviceType = "Voice";
exObj1.params = "Test Data : Test Data";
exObj1.status = "Passed";
v.add(exObj1);
return v;
}
public void write(Vector excelObj) throws IOException, WriteException {
this.testData = excelObj;
write();
}
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("Sheet1", 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.setSize(7);
//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:\\TestXls\\report.xls");
//test.write();
test.write(testData);
System.out
.println("Please check the result file under C:/TestXls/report.xls ");
}
}
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.Label;
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;
import jxl.Range;
public class ExportToExcel {
private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile;
//private String width;
private Vector testData = null;
public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}
/* public void setColumnWidth(String width ){
this.width = width;
}*/
public Vector populateTestData() {
Vector v = new Vector();
ExcelObject exObj = new ExcelObject();
exObj.testCaseId = "Create_Voice";
exObj.actionType = "Create";
exObj.serviceType = "Voice";
exObj.params = "Test Data : Test Data";
exObj.status = "Passed";
v.add(exObj);
ExcelObject exObj1 = new ExcelObject();
exObj1.testCaseId = "Create_Voice";
exObj1.actionType = "Create";
exObj1.serviceType = "Voice";
exObj1.params = "Test Data : Test Data";
exObj1.status = "Passed";
v.add(exObj1);
return v;
}
public void write(Vector excelObj) throws IOException, WriteException {
this.testData = excelObj;
write();
}
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("Sheet1", 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.setSize(7);
//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:\\TestXls\\report.xls");
//test.write();
test.write(testData);
System.out
.println("Please check the result file under C:/TestXls/report.xls ");
}
}
Post a Comment