PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/org.openscada.deploy.iolist.utils/src/org/openscada/deploy/iolist/utils/SpreadSheetPoiHelper.java

https://github.com/denodoro/org.openscada.deploy
Java | 367 lines | 301 code | 60 blank | 6 comment | 59 complexity | 4a7ca4a683a9f5ccf8a2f445a41aaab9 MD5 | raw file
Possible License(s): LGPL-3.0
  1. package org.openscada.deploy.iolist.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Collection;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map;
  12. import org.apache.poi.hssf.usermodel.HSSFFont;
  13. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. import org.apache.poi.hssf.usermodel.HeaderFooter;
  16. import org.apache.poi.hssf.util.HSSFColor;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.CellStyle;
  19. import org.apache.poi.ss.usermodel.Font;
  20. import org.apache.poi.ss.usermodel.Footer;
  21. import org.apache.poi.ss.usermodel.IndexedColors;
  22. import org.apache.poi.ss.usermodel.PrintSetup;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.usermodel.Workbook;
  26. import org.apache.poi.ss.util.CellRangeAddress;
  27. import org.openscada.deploy.iolist.model.Item;
  28. import org.openscada.deploy.iolist.model.ModelFactory;
  29. public class SpreadSheetPoiHelper extends GenericSpreadSheetHelper
  30. {
  31. private final Workbook workbook;
  32. private final Sheet sheet;
  33. private final CellStyle ackRequiredStyle;
  34. private final CellStyle headerStyle;
  35. private final CellStyle strikeoutStyle;
  36. protected SpreadSheetPoiHelper ()
  37. {
  38. this.workbook = new HSSFWorkbook ();
  39. this.sheet = this.workbook.createSheet ( "IO-List" );
  40. this.ackRequiredStyle = this.workbook.createCellStyle ();
  41. this.ackRequiredStyle.setFillForegroundColor ( IndexedColors.RED.getIndex () );
  42. this.ackRequiredStyle.setFillPattern ( CellStyle.SOLID_FOREGROUND );
  43. final Font headerFont = this.workbook.createFont ();
  44. headerFont.setBoldweight ( Font.BOLDWEIGHT_BOLD );
  45. final Font strikeoutFont = this.workbook.createFont ();
  46. strikeoutFont.setStrikeout ( true );
  47. this.headerStyle = this.workbook.createCellStyle ();
  48. this.headerStyle.setFont ( headerFont );
  49. this.strikeoutStyle = this.workbook.createCellStyle ();
  50. this.strikeoutStyle.setFont ( strikeoutFont );
  51. // enable auto filter
  52. this.sheet.setAutoFilter ( new CellRangeAddress ( 0, 0, 0, Header.values ().length - 1 ) );
  53. // fit to one page width
  54. this.sheet.setAutobreaks ( true );
  55. final PrintSetup ps = this.sheet.getPrintSetup ();
  56. ps.setLandscape ( true );
  57. ps.setPaperSize ( PrintSetup.A4_PAPERSIZE );
  58. ps.setFitWidth ( (short)1 );
  59. ps.setFitHeight ( (short)0 );
  60. ps.setFooterMargin ( 0.25 );
  61. this.sheet.setMargin ( Sheet.LeftMargin, 0.25 );
  62. this.sheet.setMargin ( Sheet.RightMargin, 0.25 );
  63. this.sheet.setMargin ( Sheet.TopMargin, 0.25 );
  64. this.sheet.setMargin ( Sheet.BottomMargin, 0.5 );
  65. final Footer footer = this.sheet.getFooter ();
  66. footer.setRight ( "Page " + HeaderFooter.page () + " of " + HeaderFooter.numPages () );
  67. // freeze area
  68. this.sheet.createFreezePane ( 0, 1, 0, 1 );
  69. // repeat headers
  70. this.workbook.setRepeatingRowsAndColumns ( 0, -1, -1, 0, 1 );
  71. }
  72. public static void writeSpreadsheet ( final File file, final Collection<? extends Item> items ) throws Exception
  73. {
  74. final SpreadSheetPoiHelper helper = new SpreadSheetPoiHelper ();
  75. helper.writeHeader ();
  76. helper.writeItems ( items );
  77. helper.autoAdjust ( helper.sheet );
  78. helper.write ( file );
  79. }
  80. private void write ( final File file ) throws Exception
  81. {
  82. final FileOutputStream fileOut = new FileOutputStream ( file );
  83. this.workbook.write ( fileOut );
  84. fileOut.close ();
  85. }
  86. private void autoAdjust ( final Sheet sheet )
  87. {
  88. for ( int i = 0; i < Header.values ().length; i++ )
  89. {
  90. sheet.autoSizeColumn ( i );
  91. }
  92. }
  93. private Cell createCell ( final Sheet sheet, final int row, final int column )
  94. {
  95. Row rowData = sheet.getRow ( row );
  96. if ( rowData == null )
  97. {
  98. rowData = sheet.createRow ( row );
  99. }
  100. return rowData.createCell ( column );
  101. }
  102. @Override
  103. protected void strikeThroughRow ( final int rowIndex ) throws Exception
  104. {
  105. final Row row = this.sheet.getRow ( rowIndex );
  106. for ( final Iterator<Cell> i = row.cellIterator (); i.hasNext (); )
  107. {
  108. final Cell cell = i.next ();
  109. cell.setCellStyle ( this.strikeoutStyle );
  110. }
  111. }
  112. @Override
  113. protected void addHeaderCell ( final String string, final int index ) throws Exception
  114. {
  115. final Cell cell = createCell ( this.sheet, 0, index );
  116. cell.setCellValue ( string );
  117. cell.setCellStyle ( this.headerStyle );
  118. }
  119. @Override
  120. protected void addData ( final int row, final int column, final Double data, final boolean ack )
  121. {
  122. final Cell cell = createCell ( this.sheet, row, column );
  123. if ( data != null )
  124. {
  125. cell.setCellValue ( data );
  126. if ( ack )
  127. {
  128. cell.setCellStyle ( this.ackRequiredStyle );
  129. }
  130. }
  131. else
  132. {
  133. cell.setCellType ( Cell.CELL_TYPE_BLANK );
  134. }
  135. }
  136. @Override
  137. protected void addData ( final int row, final int column, final String data, final boolean ack )
  138. {
  139. final Cell cell = createCell ( this.sheet, row, column );
  140. if ( data != null )
  141. {
  142. cell.setCellValue ( data );
  143. if ( ack )
  144. {
  145. cell.setCellStyle ( this.ackRequiredStyle );
  146. }
  147. }
  148. else
  149. {
  150. cell.setCellType ( Cell.CELL_TYPE_BLANK );
  151. }
  152. }
  153. @Override
  154. protected void addSelectiveDataAck ( final int row, final int column, final boolean available, final Double value, final boolean ack )
  155. {
  156. final Cell cell = createCell ( this.sheet, row, column );
  157. if ( available && value == null )
  158. {
  159. cell.setCellValue ( "X" );
  160. if ( ack )
  161. {
  162. cell.setCellStyle ( this.ackRequiredStyle );
  163. }
  164. }
  165. else if ( available && value != null )
  166. {
  167. cell.setCellValue ( value );
  168. if ( ack )
  169. {
  170. cell.setCellStyle ( this.ackRequiredStyle );
  171. }
  172. }
  173. else
  174. {
  175. cell.setCellType ( Cell.CELL_TYPE_BLANK );
  176. }
  177. }
  178. @Override
  179. protected void addSelectiveData ( final int row, final int column, final boolean available, final String value, final boolean ackRequired )
  180. {
  181. final Cell cell = createCell ( this.sheet, row, column );
  182. if ( available && value == null )
  183. {
  184. cell.setCellValue ( "X" );
  185. if ( ackRequired )
  186. {
  187. cell.setCellStyle ( this.ackRequiredStyle );
  188. }
  189. }
  190. else if ( available && value != null )
  191. {
  192. cell.setCellValue ( value );
  193. if ( ackRequired )
  194. {
  195. cell.setCellStyle ( this.ackRequiredStyle );
  196. }
  197. }
  198. else
  199. {
  200. cell.setCellType ( Cell.CELL_TYPE_BLANK );
  201. }
  202. }
  203. public static List<Item> loadExcel ( final String fileName ) throws IOException
  204. {
  205. final List<Item> result = new LinkedList<Item> ();
  206. final HSSFWorkbook workbook = new HSSFWorkbook ( new FileInputStream ( fileName ) );
  207. final Sheet sheet = workbook.getSheetAt ( 0 );
  208. final Map<Integer, Header> header = loadHeader ( sheet );
  209. for ( int row = 1; row <= sheet.getLastRowNum (); row++ )
  210. {
  211. final Item item = convertToItem ( workbook, header, sheet.getRow ( row ), String.format ( "%s@%s", fileName, row ) );
  212. if ( item != null )
  213. {
  214. result.add ( item );
  215. }
  216. }
  217. return result;
  218. }
  219. private static Item convertToItem ( final HSSFWorkbook workbook, final Map<Integer, Header> header, final Row row, final String debugInformation )
  220. {
  221. if ( row == null || row.getLastCellNum () < 2 )
  222. {
  223. return null;
  224. }
  225. // ignore empty lines
  226. if ( row.getLastCellNum () < 3 || row.getCell ( 2 ) == null || row.getCell ( 2 ).getStringCellValue () == null || row.getCell ( 2 ).getStringCellValue ().length () == 0 )
  227. {
  228. return null;
  229. }
  230. final Item item = ModelFactory.eINSTANCE.createItem ();
  231. item.setDebugInformation ( debugInformation );
  232. final Map<Header, Value> mapped = makeRow ( workbook, header, row );
  233. for ( final Map.Entry<Header, Value> entry : mapped.entrySet () )
  234. {
  235. entry.getKey ().apply ( item, entry.getValue () );
  236. }
  237. return item;
  238. }
  239. private static String makeStringValue ( final HSSFWorkbook workbook, final Cell cell )
  240. {
  241. if ( cell == null )
  242. {
  243. return null;
  244. }
  245. final HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator ( workbook );
  246. switch ( cell.getCellType () )
  247. {
  248. case Cell.CELL_TYPE_BLANK:
  249. return "";
  250. case Cell.CELL_TYPE_FORMULA:
  251. return eval.evaluate ( cell ).getStringValue ();
  252. default:
  253. return cell.toString ();
  254. }
  255. }
  256. private static Map<Header, Value> makeRow ( final HSSFWorkbook workbook, final Map<Integer, Header> headers, final Row row )
  257. {
  258. final Map<Header, Value> result = new HashMap<Header, Value> ();
  259. for ( int i = 0; i < row.getLastCellNum (); i++ )
  260. {
  261. final Header header = headers.get ( i );
  262. if ( header != null && row.getCell ( i ) != null )
  263. {
  264. final Cell cell = row.getCell ( i );
  265. final Value value = new Value ();
  266. value.setValue ( makeStringValue ( workbook, cell ) );
  267. value.setBackgroundColor ( new RGB ( 255, 255, 255 ) );
  268. if ( cell.getCellStyle () != null )
  269. {
  270. final HSSFFont font = workbook.getFontAt ( cell.getCellStyle ().getFontIndex () );
  271. if ( font != null )
  272. {
  273. value.setStrikeThrough ( font.getStrikeout () );
  274. }
  275. final short color = cell.getCellStyle ().getFillForegroundColor ();
  276. if ( color == HSSFColor.RED.index )
  277. {
  278. value.setBackgroundColor ( new RGB ( 255, 0, 0 ) );
  279. }
  280. }
  281. result.put ( header, value );
  282. }
  283. }
  284. return result;
  285. }
  286. private static Map<Integer, Header> loadHeader ( final Sheet sheet )
  287. {
  288. final Map<Integer, Header> headerMap = new HashMap<Integer, Header> ();
  289. final Row row = sheet.getRow ( 0 );
  290. for ( int i = 0; i < row.getLastCellNum (); i++ )
  291. {
  292. try
  293. {
  294. final Header header = Header.valueOf ( row.getCell ( i ).getStringCellValue () );
  295. if ( header != null )
  296. {
  297. headerMap.put ( i, header );
  298. }
  299. }
  300. catch ( final IllegalArgumentException e )
  301. {
  302. // ignore extra header
  303. }
  304. }
  305. return headerMap;
  306. }
  307. }