Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  4. import java.util.Calendar;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. import org.apache.poi.ss.usermodel.Cell;
  10. import org.apache.poi.ss.usermodel.CellStyle;
  11. import org.apache.poi.ss.usermodel.FillPatternType;
  12. import org.apache.poi.ss.usermodel.Font;
  13. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  14. import org.apache.poi.ss.usermodel.IndexedColors;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.ss.usermodel.Sheet;
  17. import org.apache.poi.ss.usermodel.VerticalAlignment;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. import org.apache.poi.ss.util.CellReference;
  21. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23.  
  24. public class PoiTest {
  25.  
  26. public static void main(String[] args) throws IOException {
  27. args = new String[] { "HSSF", "5000", "15", "1" };
  28. run(args);
  29. }
  30.  
  31. private static void run(String[] args) throws IOException {
  32. if (args.length < 4) {
  33. usage("need at least four command arguments");
  34. }
  35.  
  36. String type = args[0];
  37. int rows = parseInt(args[1], "Failed to parse rows value as integer");
  38. int cols = parseInt(args[2], "Failed to parse cols value as integer");
  39. boolean saveFile = parseInt(args[3], "Failed to parse saveFile value as integer") != 0;
  40.  
  41. long timeStarted = System.currentTimeMillis();
  42. runWithArgs(type, rows, cols, saveFile);
  43. long timeFinished = System.currentTimeMillis();
  44.  
  45. System.out.println(String.format("Elapsed %.2f seconds for arguments %s on Java %s", ((double) timeFinished - timeStarted) / 1000, Arrays.toString(args), System.getProperty("java.version")));
  46. }
  47.  
  48. private static void runWithArgs(String type, int rows, int cols, boolean saveFile) throws IOException {
  49. try (Workbook workBook = createWorkbook(type)) {
  50. boolean isHType = workBook instanceof HSSFWorkbook;
  51. addContent(workBook, isHType, rows, cols);
  52.  
  53. if (saveFile) {
  54. String fileName = type + "_" + rows + "_" + cols + "." + getFileSuffix(type);
  55. saveFile(workBook, fileName);
  56. }
  57. }
  58. }
  59.  
  60. private static void addContent(Workbook workBook, boolean isHType, int rows, int cols) {
  61. Map<String, CellStyle> styles = createStyles(workBook);
  62.  
  63. Sheet sheet = workBook.createSheet("Main Sheet");
  64.  
  65. Cell headerCell = sheet.createRow(0).createCell(0);
  66. headerCell.setCellValue("Header text is spanned across multiple cells");
  67. headerCell.setCellStyle(styles.get("header"));
  68. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));
  69.  
  70. int sheetNo = 0;
  71. int rowIndexInSheet = 1;
  72. double value = 0;
  73. Calendar calendar = Calendar.getInstance();
  74. for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
  75. if (isHType && sheetNo != rowIndex / 0x10000) {
  76. sheet = workBook.createSheet("Spillover from sheet " + (++sheetNo));
  77. headerCell.setCellValue("Header text is spanned across multiple cells");
  78. headerCell.setCellStyle(styles.get("header"));
  79. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));
  80. rowIndexInSheet = 1;
  81. }
  82.  
  83. Row row = sheet.createRow(rowIndexInSheet);
  84. for (int colIndex = 0; colIndex < cols; colIndex++) {
  85. value = populateCell(styles, value, calendar, rowIndex, row, colIndex);
  86. }
  87. rowIndexInSheet++;
  88. }
  89. for (int i = 0; i < cols; i++) {
  90. sheet.autoSizeColumn(i);
  91. }
  92. }
  93.  
  94. private static double populateCell(Map<String, CellStyle> styles, double value, Calendar calendar, int rowIndex, Row row, int colIndex) {
  95. Cell cell = row.createCell(colIndex);
  96. String address = new CellReference(cell).formatAsString();
  97. switch (colIndex) {
  98. case 0:
  99. // column A: default number format
  100. cell.setCellValue(value++);
  101. break;
  102. case 1:
  103. // column B: #,##0
  104. cell.setCellValue(value++);
  105. cell.setCellStyle(styles.get("#,##0.00"));
  106. break;
  107. case 2:
  108. // column C: $#,##0.00
  109. cell.setCellValue(value++);
  110. cell.setCellStyle(styles.get("$#,##0.00"));
  111. break;
  112. case 3:
  113. // column D: red bold text on yellow background
  114. cell.setCellValue(address);
  115. cell.setCellStyle(styles.get("red-bold"));
  116. break;
  117. case 4:
  118. // column E: boolean
  119. // TODO booleans are shown as 1/0 instead of TRUE/FALSE
  120. cell.setCellValue(rowIndex % 2 == 0);
  121. break;
  122. case 5:
  123. // column F: date / time
  124. cell.setCellValue(calendar);
  125. cell.setCellStyle(styles.get("m/d/yyyy"));
  126. calendar.roll(Calendar.DAY_OF_YEAR, -1);
  127. break;
  128. case 6:
  129. // column F: formula
  130. // TODO formulas are not yet supported in SXSSF
  131. //cell.setCellFormula("SUM(A" + (rowIndex+1) + ":E" + (rowIndex+1)+ ")");
  132. //break;
  133. default:
  134. cell.setCellValue(value++);
  135. break;
  136. }
  137. return value;
  138. }
  139.  
  140. private static void saveFile(Workbook workBook, String fileName) {
  141. try {
  142. FileOutputStream out = new FileOutputStream(fileName);
  143. workBook.write(out);
  144. out.close();
  145. }
  146. catch (IOException ioe) {
  147. System.err.println("Error: failed to write to file \"" + fileName + "\", reason=" + ioe.getMessage());
  148. }
  149. }
  150.  
  151. static Map<String, CellStyle> createStyles(Workbook wb) {
  152. Map<String, CellStyle> styles = new HashMap<>();
  153. CellStyle style;
  154.  
  155. Font headerFont = wb.createFont();
  156. headerFont.setFontHeightInPoints((short) 14);
  157. headerFont.setBold(true);
  158. style = wb.createCellStyle();
  159. style.setAlignment(HorizontalAlignment.CENTER);
  160. style.setVerticalAlignment(VerticalAlignment.CENTER);
  161. style.setFont(headerFont);
  162. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  163. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  164. styles.put("header", style);
  165.  
  166. Font monthFont = wb.createFont();
  167. monthFont.setFontHeightInPoints((short) 12);
  168. monthFont.setColor(IndexedColors.RED.getIndex());
  169. monthFont.setBold(true);
  170. style = wb.createCellStyle();
  171. style.setAlignment(HorizontalAlignment.CENTER);
  172. style.setVerticalAlignment(VerticalAlignment.CENTER);
  173. style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
  174. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  175. style.setFont(monthFont);
  176. styles.put("red-bold", style);
  177.  
  178. String[] nfmt = { "#,##0.00", "$#,##0.00", "m/d/yyyy" };
  179. for (String fmt : nfmt) {
  180. style = wb.createCellStyle();
  181. style.setDataFormat(wb.createDataFormat().getFormat(fmt));
  182. styles.put(fmt, style);
  183. }
  184.  
  185. return styles;
  186. }
  187.  
  188. static void usage(String message) {
  189. System.err.println(message);
  190. System.err.println("usage: java SSPerformanceTest HSSF|XSSF|SXSSF rows cols saveFile (0|1)? [--unsynchronized-xmlbeans] [--with-warmup-run]");
  191. System.exit(1);
  192. }
  193.  
  194. static Workbook createWorkbook(String type) {
  195. if ("HSSF".equals(type)) {
  196. return new HSSFWorkbook();
  197. }
  198. else if ("XSSF".equals(type)) {
  199. return new XSSFWorkbook();
  200. }
  201. else if ("SXSSF".equals(type)) {
  202. return new SXSSFWorkbook();
  203. }
  204.  
  205. usage("Unknown type \"" + type + "\"");
  206. throw new IllegalArgumentException("Should not reach this point");
  207. }
  208.  
  209. static String getFileSuffix(String type) {
  210. if ("HSSF".equals(type)) {
  211. return "xls";
  212. }
  213. else if ("XSSF".equals(type)) {
  214. return "xlsx";
  215. }
  216. else if ("SXSSF".equals(type)) {
  217. return "xlsx";
  218. }
  219. return null;
  220. }
  221.  
  222. static int parseInt(String value, String msg) {
  223. try {
  224. return Integer.parseInt(value);
  225. }
  226. catch (NumberFormatException e) {
  227. usage(msg);
  228. }
  229. return 0;
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement