Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractExcelUtils.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.converter;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  20. import org.apache.poi.hssf.usermodel.HSSFSheet;
  21. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  22. import org.apache.poi.hssf.util.HSSFColor;
  23. import org.apache.poi.hwpf.converter.AbstractWordUtils;
  24. import org.apache.poi.ss.usermodel.BorderStyle;
  25. import org.apache.poi.ss.util.CellRangeAddress;
  26. import org.apache.poi.util.Beta;
  27. import org.apache.poi.util.IOUtils;
  28. /**
  29. * Common class for {@link ExcelToFoUtils} and {@link ExcelToHtmlUtils}
  30. *
  31. * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
  32. * @see AbstractWordUtils
  33. */
  34. @Beta
  35. public class AbstractExcelUtils
  36. {
  37. static final String EMPTY = "";
  38. private static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
  39. private static final int UNIT_OFFSET_LENGTH = 7;
  40. public static String getAlign( short alignment )
  41. {
  42. switch ( alignment )
  43. {
  44. case HSSFCellStyle.ALIGN_CENTER:
  45. return "center";
  46. case HSSFCellStyle.ALIGN_CENTER_SELECTION:
  47. return "center";
  48. case HSSFCellStyle.ALIGN_FILL:
  49. // XXX: shall we support fill?
  50. return "";
  51. case HSSFCellStyle.ALIGN_GENERAL:
  52. return "";
  53. case HSSFCellStyle.ALIGN_JUSTIFY:
  54. return "justify";
  55. case HSSFCellStyle.ALIGN_LEFT:
  56. return "left";
  57. case HSSFCellStyle.ALIGN_RIGHT:
  58. return "right";
  59. default:
  60. return "";
  61. }
  62. }
  63. public static String getBorderStyle( BorderStyle xlsBorder )
  64. {
  65. final String borderStyle;
  66. switch ( xlsBorder )
  67. {
  68. case NONE:
  69. borderStyle = "none";
  70. break;
  71. case DASH_DOT:
  72. case DASH_DOT_DOT:
  73. case DOTTED:
  74. case HAIR:
  75. case MEDIUM_DASH_DOT:
  76. case MEDIUM_DASH_DOT_DOT:
  77. case SLANTED_DASH_DOT:
  78. borderStyle = "dotted";
  79. break;
  80. case DASHED:
  81. case MEDIUM_DASHED:
  82. borderStyle = "dashed";
  83. break;
  84. case DOUBLE:
  85. borderStyle = "double";
  86. break;
  87. default:
  88. borderStyle = "solid";
  89. break;
  90. }
  91. return borderStyle;
  92. }
  93. public static String getBorderWidth( BorderStyle xlsBorder )
  94. {
  95. final String borderWidth;
  96. switch ( xlsBorder )
  97. {
  98. case MEDIUM_DASH_DOT:
  99. case MEDIUM_DASH_DOT_DOT:
  100. case MEDIUM_DASHED:
  101. borderWidth = "2pt";
  102. break;
  103. case THICK:
  104. borderWidth = "thick";
  105. break;
  106. default:
  107. borderWidth = "thin";
  108. break;
  109. }
  110. return borderWidth;
  111. }
  112. public static String getColor( HSSFColor color )
  113. {
  114. StringBuilder stringBuilder = new StringBuilder( 7 );
  115. stringBuilder.append( '#' );
  116. for ( short s : color.getTriplet() )
  117. {
  118. if ( s < 10 )
  119. stringBuilder.append( '0' );
  120. stringBuilder.append( Integer.toHexString( s ) );
  121. }
  122. String result = stringBuilder.toString();
  123. if ( result.equals( "#ffffff" ) )
  124. return "white";
  125. if ( result.equals( "#c0c0c0" ) )
  126. return "silver";
  127. if ( result.equals( "#808080" ) )
  128. return "gray";
  129. if ( result.equals( "#000000" ) )
  130. return "black";
  131. return result;
  132. }
  133. /**
  134. * See <a href=
  135. * "http://apache-poi.1045710.n5.nabble.com/Excel-Column-Width-Unit-Converter-pixels-excel-column-width-units-td2301481.html"
  136. * >here</a> for Xio explanation and details
  137. */
  138. public static int getColumnWidthInPx( int widthUnits )
  139. {
  140. int pixels = ( widthUnits / EXCEL_COLUMN_WIDTH_FACTOR )
  141. * UNIT_OFFSET_LENGTH;
  142. int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
  143. pixels += Math.round( offsetWidthUnits
  144. / ( (float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH ) );
  145. return pixels;
  146. }
  147. /**
  148. * @param mergedRanges
  149. * map of sheet merged ranges built with
  150. * {@link ExcelToHtmlUtils#buildMergedRangesMap(HSSFSheet)}
  151. * @return {@link CellRangeAddress} from map if cell with specified row and
  152. * column numbers contained in found range, <tt>null</tt> otherwise
  153. */
  154. public static CellRangeAddress getMergedRange(
  155. CellRangeAddress[][] mergedRanges, int rowNumber, int columnNumber )
  156. {
  157. CellRangeAddress[] mergedRangeRowInfo = rowNumber < mergedRanges.length ? mergedRanges[rowNumber]
  158. : null;
  159. CellRangeAddress cellRangeAddress = mergedRangeRowInfo != null
  160. && columnNumber < mergedRangeRowInfo.length ? mergedRangeRowInfo[columnNumber]
  161. : null;
  162. return cellRangeAddress;
  163. }
  164. static boolean isEmpty( String str )
  165. {
  166. return str == null || str.length() == 0;
  167. }
  168. static boolean isNotEmpty( String str )
  169. {
  170. return !isEmpty( str );
  171. }
  172. public static HSSFWorkbook loadXls( File xlsFile ) throws IOException
  173. {
  174. final FileInputStream inputStream = new FileInputStream( xlsFile );
  175. try
  176. {
  177. return new HSSFWorkbook( inputStream );
  178. }
  179. finally
  180. {
  181. IOUtils.closeQuietly( inputStream );
  182. }
  183. }
  184. }