| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
package org.wiztools.xml2spreadsheet.poiimpl; |
| 8 |
|
|
| 9 |
|
import org.wiztools.xml2spreadsheet.WorkBook; |
| 10 |
|
import org.wiztools.xml2spreadsheet.WorkBookGenerationHandler; |
| 11 |
|
import org.wiztools.xml2spreadsheet.entity.CellEntity; |
| 12 |
|
import org.wiztools.xml2spreadsheet.entity.RowEntity; |
| 13 |
|
import org.wiztools.xml2spreadsheet.entity.SheetEntity; |
| 14 |
|
import org.wiztools.xml2spreadsheet.entity.WorkBookEntity; |
| 15 |
|
import org.wiztools.xml2spreadsheet.exception.OperationException; |
| 16 |
|
import java.util.Calendar; |
| 17 |
|
import java.util.Date; |
| 18 |
|
import java.util.Map; |
| 19 |
|
import org.apache.poi.hssf.usermodel.HSSFCell; |
| 20 |
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle; |
| 21 |
|
import org.apache.poi.hssf.usermodel.HSSFRow; |
| 22 |
|
import org.apache.poi.hssf.usermodel.HSSFSheet; |
| 23 |
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 24 |
|
import org.apache.poi.hssf.util.Region; |
| 25 |
|
import org.wiztools.xml2spreadsheet.util.StyleRepository; |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public class POIWorkBookCreator implements WorkBookGenerationHandler{ |
| 33 |
|
|
| 34 |
|
private HSSFCell cell; |
| 35 |
|
private HSSFRow row; |
| 36 |
|
private HSSFSheet sheet; |
| 37 |
|
private HSSFWorkbook workBook; |
| 38 |
|
private StyleRepository styleRepo; |
| 39 |
|
|
| 40 |
|
|
| 41 |
47 |
public POIWorkBookCreator() { |
| 42 |
47 |
} |
| 43 |
|
|
| 44 |
|
public void setCellValue(final String data) throws OperationException{ |
| 45 |
31937 |
this.cell.setCellValue(data); |
| 46 |
31937 |
} |
| 47 |
|
|
| 48 |
|
public void setCellFormula(final String formula) throws OperationException{ |
| 49 |
28 |
this.cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); |
| 50 |
28 |
this.cell.setCellFormula(formula); |
| 51 |
28 |
} |
| 52 |
|
|
| 53 |
|
public void setCellValue(final double data) throws OperationException{ |
| 54 |
40668 |
this.cell.setCellValue(data); |
| 55 |
40668 |
} |
| 56 |
|
|
| 57 |
|
public void setCellValue(final Date date) throws OperationException{ |
| 58 |
28 |
this.cell.setCellValue(date); |
| 59 |
28 |
} |
| 60 |
|
|
| 61 |
|
public void setCellValue(final Calendar date) throws OperationException{ |
| 62 |
28 |
this.cell.setCellValue(date); |
| 63 |
28 |
} |
| 64 |
|
|
| 65 |
|
public void setCellValue(final boolean data) throws OperationException{ |
| 66 |
28 |
this.cell.setCellValue(data); |
| 67 |
28 |
} |
| 68 |
|
|
| 69 |
|
private HSSFCellStyle getStyle(String cellStyleVal) |
| 70 |
|
throws OperationException{ |
| 71 |
72493 |
return POIStyleCreator.getInstance().getStyle(workBook, cellStyleVal, styleRepo); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
public void createCell(final CellEntity cell, final short placement) |
| 75 |
|
throws OperationException{ |
| 76 |
72717 |
HSSFCell hcell = this.row.createCell((short)placement); |
| 77 |
|
|
| 78 |
72717 |
Map<String, String> attributes = cell.getAttributes(); |
| 79 |
72717 |
if(attributes != null){ |
| 80 |
72493 |
String styleVal = attributes.get("style"); |
| 81 |
72493 |
if(styleVal != null){ |
| 82 |
72493 |
HSSFCellStyle style = getStyle(styleVal); |
| 83 |
72493 |
hcell.setCellStyle(style); |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
72717 |
this.cell = hcell; |
| 87 |
72717 |
} |
| 88 |
|
|
| 89 |
|
public void createRow(final RowEntity row, final int placement) |
| 90 |
|
throws OperationException{ |
| 91 |
24801 |
HSSFRow hrow = this.sheet.createRow(placement); |
| 92 |
24801 |
this.row = hrow; |
| 93 |
24801 |
} |
| 94 |
|
|
| 95 |
|
public void mergeCells(final int row1, final short col1, |
| 96 |
|
final int row2, final short col2) |
| 97 |
|
throws OperationException{ |
| 98 |
408 |
this.sheet.addMergedRegion(new Region( |
| 99 |
|
row1, |
| 100 |
|
col1, |
| 101 |
|
row2, |
| 102 |
|
col2)); |
| 103 |
408 |
} |
| 104 |
|
|
| 105 |
|
public void setColumnWidth(final short column, final short width) |
| 106 |
|
throws OperationException{ |
| 107 |
142 |
this.sheet.setColumnWidth(column, width); |
| 108 |
142 |
} |
| 109 |
|
|
| 110 |
|
public void createSheet(final SheetEntity sheet) |
| 111 |
|
throws OperationException{ |
| 112 |
160 |
Map<String, String> attributes = sheet.getAttributes(); |
| 113 |
160 |
String name = null; |
| 114 |
160 |
if(attributes != null){ |
| 115 |
132 |
name = attributes.get("name"); |
| 116 |
|
} |
| 117 |
160 |
HSSFSheet hsheet = null; |
| 118 |
160 |
if(name==null){ |
| 119 |
28 |
hsheet = this.workBook.createSheet(); |
| 120 |
|
} |
| 121 |
|
else{ |
| 122 |
132 |
hsheet = this.workBook.createSheet(name); |
| 123 |
|
} |
| 124 |
160 |
this.sheet = hsheet; |
| 125 |
160 |
} |
| 126 |
|
|
| 127 |
|
public void createWorkBook(final WorkBookEntity workBook) |
| 128 |
|
throws OperationException{ |
| 129 |
47 |
HSSFWorkbook hworkBook = new HSSFWorkbook(); |
| 130 |
47 |
this.workBook = hworkBook; |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
47 |
this.styleRepo = new StyleRepository(); |
| 135 |
47 |
} |
| 136 |
|
|
| 137 |
|
public WorkBook getWorkBook() { |
| 138 |
47 |
return new POIWorkBookAdapter(this.workBook); |
| 139 |
|
} |
| 140 |
|
} |