├── src ├── META-INF │ └── MANIFEST.MF └── isc │ └── poi │ ├── WorkbookUtils.java │ └── Main.java ├── out └── production │ └── java │ └── META-INF │ └── MANIFEST.MF ├── .idea ├── description.html ├── project-template.xml ├── vcs.xml ├── modules.xml ├── artifacts │ └── java_jar.xml ├── libraries │ └── POI.xml ├── misc.xml └── workspace.xml ├── README.md ├── .gitignore ├── java.iml └── LICENSE /src/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: isc.poi.Main 3 | 4 | -------------------------------------------------------------------------------- /out/production/java/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: isc.poi.Main 3 | 4 | -------------------------------------------------------------------------------- /.idea/description.html: -------------------------------------------------------------------------------- 1 | Simple Java application that includes a class with main() method -------------------------------------------------------------------------------- /.idea/project-template.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # apache-poi 2 | Apache poi integration with InterSyterms Data Platform 3 | 4 | For instructions refer to [this repository](https://github.com/intersystems-ru/apache-poi-os). 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/artifacts/java_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | $PROJECT_DIR$/out/artifacts/java_jar 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/libraries/POI.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /java.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 InterSystems 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/isc/poi/WorkbookUtils.java: -------------------------------------------------------------------------------- 1 | package isc.poi; 2 | 3 | import com.pnuema.java.barcode.Barcode; 4 | import com.pnuema.java.barcode.EncodingType; 5 | import org.apache.poi.ss.usermodel.*; 6 | import org.apache.poi.ss.util.CellReference; 7 | import org.apache.poi.xssf.usermodel.XSSFClientAnchor; 8 | import org.apache.poi.xssf.usermodel.XSSFDrawing; 9 | 10 | import javax.imageio.ImageIO; 11 | import java.awt.image.BufferedImage; 12 | import java.io.*; 13 | import java.util.Objects; 14 | 15 | public class WorkbookUtils 16 | { 17 | Workbook workbook = null; 18 | 19 | public String init(String filename) 20 | { 21 | String result = ""; 22 | File file = new File(filename); 23 | 24 | try 25 | { 26 | workbook = WorkbookFactory.create(file); 27 | } 28 | catch (Exception e) 29 | { 30 | StringWriter sw = new StringWriter(); 31 | e.printStackTrace(new PrintWriter(sw)); 32 | String exceptionAsString = sw.toString(); 33 | result = e.getLocalizedMessage() + " " + exceptionAsString; 34 | } 35 | 36 | return result; 37 | } 38 | 39 | public String finishFillAndCreateResultFile(String outFilename) 40 | { 41 | String result = ""; 42 | try 43 | { 44 | //workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); 45 | workbook.setForceFormulaRecalculation(true); 46 | FileOutputStream out = new FileOutputStream(outFilename); 47 | workbook.write(out); 48 | out.close(); 49 | } 50 | catch (Exception e) 51 | { 52 | StringWriter sw = new StringWriter(); 53 | e.printStackTrace(new PrintWriter(sw)); 54 | String exceptionAsString = sw.toString(); 55 | result = e.getLocalizedMessage() + " " + exceptionAsString; 56 | } 57 | 58 | return result; 59 | } 60 | 61 | public String fillBookSheet(int sheetNumber, String[] params) 62 | { 63 | String result = ""; 64 | 65 | try 66 | { 67 | Sheet sheet = workbook.getSheetAt(sheetNumber); 68 | 69 | for (String param: params) 70 | { 71 | String[] paramArray = param.split("\u0001"); 72 | String address = paramArray[0]; 73 | String value; 74 | String type; 75 | 76 | if (paramArray.length > 1) 77 | { 78 | value = paramArray[1]; 79 | } else { 80 | value = ""; 81 | } 82 | 83 | if (paramArray.length > 2) 84 | { 85 | type = paramArray[2].toUpperCase(); 86 | } else { 87 | type = ""; 88 | } 89 | 90 | CellReference cellReference = new CellReference(address); 91 | Row row = sheet.getRow(cellReference.getRow()); 92 | 93 | if (row == null) { 94 | row = sheet.createRow(cellReference.getRow()); 95 | } 96 | 97 | Cell cell = row.getCell(cellReference.getCol(), Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 98 | 99 | if (Objects.equals(type, "NUMERIC")) 100 | { 101 | cell.setCellType(CellType.NUMERIC); 102 | cell.setCellValue(Double.parseDouble(value)); 103 | } 104 | else if (Objects.equals(type, "BARCODE")) 105 | { 106 | byte[] data = createBarCode(value); 107 | inputImage(sheet, cell, data); 108 | } 109 | else 110 | { 111 | cell.setCellType(CellType.STRING); 112 | cell.setCellValue(value); 113 | } 114 | } 115 | } catch (Exception e) { 116 | StringWriter sw = new StringWriter(); 117 | e.printStackTrace(new PrintWriter(sw)); 118 | String exceptionAsString = sw.toString(); 119 | 120 | result = e.toString() + " " + exceptionAsString; 121 | } 122 | 123 | return result; 124 | } 125 | 126 | public void inputImage(Sheet sheet, Cell cell, byte[] data) throws IOException 127 | { 128 | int inputImagePictureID1 = workbook.addPicture(data, Workbook.PICTURE_TYPE_PNG); 129 | 130 | XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch(); 131 | 132 | XSSFClientAnchor ironManAnchor = new XSSFClientAnchor(); 133 | 134 | int row = cell.getRowIndex(); 135 | int col = cell.getColumnIndex(); 136 | 137 | ironManAnchor.setCol1(col); // Sets the column (0 based) of the first cell. 138 | ironManAnchor.setCol2(col+1); // Sets the column (0 based) of the Second cell. 139 | ironManAnchor.setRow1(row); // Sets the row (0 based) of the first cell. 140 | ironManAnchor.setRow2(row+1); // Sets the row (0 based) of the Second cell. 141 | 142 | drawing.createPicture(ironManAnchor, inputImagePictureID1); 143 | } 144 | 145 | public byte[] createBarCode(String stringToEncode) throws IOException 146 | { 147 | //https://github.com/barnhill/barcode-java 148 | Barcode barcode = new Barcode(); 149 | BufferedImage img = (BufferedImage) barcode.encode(EncodingType.UPCA, stringToEncode); 150 | 151 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 152 | ImageIO.write(img, "png", bos ); 153 | 154 | return bos.toByteArray(); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/isc/poi/Main.java: -------------------------------------------------------------------------------- 1 | package isc.poi; 2 | 3 | import org.apache.poi.ss.usermodel.*; 4 | 5 | import java.io.*; 6 | import java.sql.SQLException; 7 | import java.util.ArrayList; 8 | import java.util.Iterator; 9 | 10 | import org.apache.poi.ss.usermodel.Cell; 11 | import org.apache.poi.ss.usermodel.Sheet; 12 | import org.apache.poi.ss.usermodel.Workbook; 13 | import org.apache.poi.ss.usermodel.WorkbookFactory; 14 | 15 | 16 | 17 | import org.apache.poi.ss.usermodel.DateUtil; 18 | import org.apache.poi.ss.util.CellReference; 19 | 20 | 21 | import static org.apache.poi.ss.usermodel.CellType.*; 22 | 23 | public class Main { 24 | 25 | private static final char separator = (char)1; 26 | 27 | public static String[] getBookFromStream(byte[] stream) { 28 | String[] result = new String[1]; 29 | 30 | 31 | ArrayList bookList = new ArrayList(); 32 | String bookInfo = ""; 33 | 34 | Workbook workbook = loadBook(new ByteArrayInputStream(stream), result); 35 | 36 | if (result[0]==null) { 37 | Iterator sheetIterator = workbook.sheetIterator(); 38 | while(sheetIterator.hasNext()) { 39 | Sheet sheet = sheetIterator.next(); 40 | ArrayList rowList = getSheetInternal(sheet); 41 | bookList.addAll(rowList); 42 | 43 | bookInfo += String.valueOf(rowList.size()) + (char)1; 44 | bookInfo += sheet.getSheetName() + (char)1 + (char)1; 45 | } 46 | bookInfo = bookInfo.substring(0, bookInfo.length() - 2); 47 | bookList.add(bookInfo); 48 | result = bookList.toArray(new String[bookList.size()]); 49 | } 50 | 51 | return result; 52 | } 53 | 54 | private static Workbook loadBook(String filename, String[] result) { 55 | Workbook workbook = null; 56 | File file = new File(filename); 57 | 58 | try { 59 | workbook = WorkbookFactory.create(file); 60 | } catch (Exception e) { 61 | // IOException InvalidFormatException 62 | result[0] = e.toString(); 63 | } 64 | 65 | return workbook; 66 | } 67 | 68 | private static Workbook loadBook(InputStream stream, String[] result) { 69 | Workbook workbook = null; 70 | try { 71 | workbook = WorkbookFactory.create(stream); 72 | } catch (Exception e) { 73 | // IOException InvalidFormatException 74 | result[0] = e.toString(); 75 | } 76 | 77 | return workbook; 78 | } 79 | 80 | public static String[] getBook(String filename) { 81 | String[] result = new String[1]; 82 | ArrayList bookList = new ArrayList(); 83 | String bookInfo = ""; 84 | Workbook workbook = loadBook(filename, result); 85 | 86 | if (result[0]==null) { 87 | Iterator sheetIterator = workbook.sheetIterator(); 88 | while(sheetIterator.hasNext()) { 89 | Sheet sheet = sheetIterator.next(); 90 | ArrayList rowList = getSheetInternal(sheet); 91 | bookList.addAll(rowList); 92 | 93 | bookInfo += String.valueOf(rowList.size()) + (char)1; 94 | bookInfo += sheet.getSheetName() + (char)1 + (char)1; 95 | } 96 | 97 | bookInfo = bookInfo.substring(0, bookInfo.length() - 2); 98 | bookList.add(bookInfo); 99 | result = bookList.toArray(new String[bookList.size()]); 100 | } 101 | try { 102 | workbook.close(); 103 | } catch (IOException e) { 104 | result = new String[1]; 105 | result[0] = e.toString(); 106 | } 107 | return result; 108 | } 109 | 110 | /// Iterators - do not skip empty rows? 111 | /// https://stackoverflow.com/questions/30519539/apache-poi-skips-rows-that-have-never-been-updated 112 | public static String[] getSheet(String filename, int sheetNumber) { 113 | File file = new File(filename); 114 | 115 | Workbook workbook = null; 116 | String[] result = new String[1]; 117 | 118 | try { 119 | workbook = WorkbookFactory.create(file); 120 | } catch (Exception e) { 121 | // IOException InvalidFormatException 122 | result[0] = e.toString(); 123 | } 124 | 125 | if (result[0]==null) { 126 | Sheet sheet = null; 127 | try { 128 | sheet = workbook.getSheetAt(sheetNumber); 129 | } catch (Exception e) { 130 | result[0] = e.toString(); 131 | } 132 | 133 | if (result[0]==null) { 134 | ArrayList rowList = getSheetInternal(sheet); 135 | result = rowList.toArray(new String[rowList.size()]); 136 | 137 | try { 138 | workbook.close(); 139 | } catch (IOException e) { 140 | result = new String[1]; 141 | result[0] = e.toString(); 142 | } 143 | } 144 | } 145 | return result; 146 | } 147 | 148 | /// Pass ArrayList here? 149 | private static ArrayList getSheetInternal(Sheet sheet) { 150 | 151 | Object value = null; 152 | ArrayList rowList = new ArrayList(); 153 | Iterator rows = sheet.rowIterator(); 154 | int rownum=0; 155 | 156 | while (rows.hasNext()) { 157 | rownum++; 158 | String list= ""; 159 | Row row = (Row) rows.next(); 160 | 161 | for (int i = 0; i < row.getLastCellNum(); i++) { 162 | value = ""; 163 | Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 164 | switch (cell.getCellType()) { 165 | case FORMULA: 166 | switch (cell.getCachedFormulaResultType()) { 167 | case NUMERIC: 168 | if (DateUtil.isCellDateFormatted(cell)) { 169 | value = new java.sql.Date(cell.getDateCellValue().getTime()); 170 | } else { 171 | value = cell.getNumericCellValue(); 172 | } 173 | break; 174 | case STRING: 175 | value = cell.getRichStringCellValue(); 176 | break; 177 | case ERROR: 178 | value = ""; 179 | break; 180 | } 181 | break; 182 | case NUMERIC: 183 | if (DateUtil.isCellDateFormatted(cell)) { 184 | value = new java.sql.Date(cell.getDateCellValue().getTime()); 185 | } else { 186 | value = cell.getNumericCellValue(); 187 | } 188 | break; 189 | case STRING: 190 | value = cell.getRichStringCellValue().getString(); 191 | break; 192 | case _NONE: 193 | case BLANK: 194 | case ERROR: 195 | // not a null 196 | value = ""; 197 | break; 198 | case BOOLEAN: 199 | value = cell.getBooleanCellValue(); 200 | } 201 | 202 | 203 | if (value instanceof String) { 204 | list += (String)value; 205 | } else if (value instanceof Double) { 206 | Double doubleValue = (Double)value; 207 | if (doubleValue == Math.floor(doubleValue) && !Double.isInfinite(doubleValue)) { 208 | list += doubleValue.longValue(); 209 | } else { 210 | list += value.toString(); 211 | } 212 | } else { 213 | list += value.toString(); 214 | } 215 | list += separator; 216 | cell = null; 217 | value = null; 218 | } 219 | // remove last separator 220 | rowList.add(list.substring(0, list.length() - 1)); 221 | 222 | row = null; 223 | if (rownum % 1000 == 0) { 224 | System.gc(); 225 | } 226 | } 227 | return rowList; 228 | } 229 | 230 | public static int getSheetCount(String filename) throws Exception { 231 | File file = new File(filename); 232 | Workbook workbook = WorkbookFactory.create(file); 233 | return workbook.getNumberOfSheets(); 234 | } 235 | 236 | public static String fillBook(String filename, String outFilename, int sheetNumber, String[] params) throws IOException { 237 | String result = ""; 238 | Workbook workbook = null; 239 | try { 240 | File file = new File(filename); 241 | 242 | workbook = WorkbookFactory.create(file); 243 | Sheet sheet = workbook.getSheetAt(sheetNumber); 244 | for (String param: params) 245 | { 246 | String[] paramArray = param.split("\u0001"); 247 | String address = paramArray[0]; 248 | String value; 249 | 250 | if (paramArray.length > 1) 251 | { 252 | value = paramArray[1]; 253 | } else { 254 | value = ""; 255 | } 256 | 257 | CellReference cellReference = new CellReference(address); 258 | Row row = sheet.getRow(cellReference.getRow()); 259 | 260 | if (row == null) { 261 | row = sheet.createRow(cellReference.getRow()); 262 | } 263 | 264 | Cell cell = row.getCell(cellReference.getCol(), Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 265 | 266 | if (value.matches("\\d+(\\.\\d+)?")) 267 | { 268 | cell.setCellType(CellType.NUMERIC); 269 | cell.setCellValue(Double.parseDouble(value)); 270 | }else 271 | { 272 | cell.setCellType(CellType.STRING); 273 | cell.setCellValue(value); 274 | } 275 | } 276 | 277 | //workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); 278 | workbook.setForceFormulaRecalculation(true); 279 | 280 | 281 | FileOutputStream out = new FileOutputStream(outFilename); 282 | workbook.write(out); 283 | out.close(); 284 | 285 | } catch (Exception e) { 286 | StringWriter sw = new StringWriter(); 287 | e.printStackTrace(new PrintWriter(sw)); 288 | String exceptionAsString = sw.toString(); 289 | 290 | result = e.toString() + " " + exceptionAsString; 291 | } 292 | //File file = new File(filename); 293 | //Workbook workbook = WorkbookFactory.create(file); 294 | return result; 295 | } 296 | 297 | } 298 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 27 | 28 | 29 | 30 | 31 | 36 | 37 | 38 | 39 | WorkbookFactory 40 | setValues 41 | setValue 42 | toStri 43 | getData 44 | XmlException 45 | 46 | 47 | 48 | 50 | 51 | 54 | 55 | 56 | 62 | 63 | 64 | 66 | 67 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 |