List of usage examples for org.apache.poi.ss.usermodel Workbook getSheetAt
Sheet getSheetAt(int index);
From source file:lp.XLSXhandler.java
public void create_xlsx_file(String absolute, String model, Object[] obj, double[][] results) { /*/*from w ww . ja v a2 s . com*/ obj[0] = data; -- obj[1] = rows; obj[2] = columns; obj[3] = variables; -- obj[4] = dmu Names; -- */ double[][] data = (double[][]) obj[0]; // Rows number in file int rows_number = (Integer) obj[1]; // Columns in file (1-based) int col_number = (Integer) obj[2]; //int res = results.length; int results_number = results[0].length; int total_col = col_number + results_number;//9 String[] dmuNames = (String[]) obj[4]; String[] var = (String[]) obj[3]; String[] variables = new String[0]; /* Get the selected model/s and create the matrix "variables" for each model respectively. */ if (model.equals("multiplicative") || model.equals("composition")) { variables = new String[total_col]; System.arraycopy(var, 0, variables, 0, var.length); variables[col_number] = "e1"; variables[col_number + 1] = "e2"; variables[col_number + 2] = "Overall Efficiency"; } if (model.equals("additive")) { variables = new String[total_col]; System.arraycopy(var, 0, variables, 0, var.length); variables[col_number] = "Weight 1"; variables[col_number + 1] = "Weight 2"; variables[col_number + 2] = "Overall Efficiency"; variables[col_number + 3] = "Theta 1"; variables[col_number + 4] = "Theta 2"; } try { Workbook wb = new XSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream(absolute + "\\" + model + ".xlsx"); wb.write(fileOut); fileOut.close(); InputStream inp = new FileInputStream(absolute + "\\" + model + ".xlsx"); wb = WorkbookFactory.create(inp); Sheet sheet = wb.createSheet("new"); sheet = wb.getSheetAt(0); //int rows_number = results.length; //int col_number = results[0].length; Row row = sheet.createRow(0); row = sheet.getRow(0); if (row == null) { row = sheet.createRow(0); } Cell cell = row.getCell(0); if (cell == null) { //the first ROW: the names and variable names for each column for (int l = 0; l < variables.length; l++) { cell = row.createCell(l, cell.CELL_TYPE_STRING); cell.setCellValue(variables[l]); } for (int i = 0; i < rows_number; i++) { row = sheet.createRow(i + 1); int helper = 0; //the rest file... for (int k = 0; k < total_col + 1; k++) { if (k == 0) { //the first column (dmu names) cell = row.createCell(k, cell.CELL_TYPE_STRING); cell.setCellValue(dmuNames[i]); } if (k > 0 && k < col_number) { //the rest of the columns with data cell = row.createCell(k, cell.CELL_TYPE_NUMERIC); cell.setCellValue(data[i][k - 1]); } if (k >= col_number && helper < results_number) { //the columns with efficiency results cell = row.createCell(k, cell.CELL_TYPE_NUMERIC); cell.setCellValue(results[i][helper]); helper++; } } } } // Write the output to a file FileOutputStream fOut = new FileOutputStream(absolute + "\\" + model + ".xlsx"); wb.write(fOut); fOut.close(); inp.close(); } catch (FileNotFoundException e) { System.out.println("--EXCEPTION: " + e.getMessage()); } catch (IOException e) { System.out.println("--EXCEPTION: " + e.getMessage()); } catch (InvalidFormatException ex) { Logger.getLogger(XLSXhandler.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:main.KeywordList.java
public void writeToExcel(String fileName) { final int GROUP = 0; final int PACKAGENAME = 1; final int SUBPACKAGENAME = 2; final int POSITIONTYPE = 3; final int KEYWORD = 4; final int DATE = 5; final int PRIORITY = 6; try {/* w ww . j a v a 2s. c o m*/ System.out.println("Writing XML: " + fileName); ForcastUi.consoleLog("Writing XML: " + fileName); FileInputStream fileIn = new FileInputStream(fileName); Workbook wb = WorkbookFactory.create(fileIn); Sheet sheet = wb.getSheetAt(0); keywordList.stream().filter(key -> key.isUsed()).forEach(keyword -> { //? ? or Don't int rowInt = 1; while (true) { Row row = sheet.getRow(rowInt); if (row == null) break; Cell cellKeyword = row.getCell(KEYWORD); String shortName = cellKeyword.getRichStringCellValue().getString(); if (shortName.equalsIgnoreCase(keyword.getKeyword())) {//If Match, Update Date Cell cellDate = row.getCell(DATE); cellDate.setCellType(Cell.CELL_TYPE_NUMERIC); cellDate.setCellValue(new Date()); //cellDate.setCellValue(ForcastUi.dateToString(new Date())); //cellDate.setCellType(Cell.CELL_TYPE_STRING); //cellDate.setCellValue(ForcastUi.dateToString(new Date())); //break; } rowInt++; } }); // Write the output to a file FileOutputStream fileOut = new FileOutputStream(fileName); wb.write(fileOut); fileOut.close(); fileIn.close(); } catch (FileNotFoundException e) { ErrorMessages.printErrorMsg(ErrorMessages.FILENOTFOUND, fileName); ForcastUi.consoleLog(e.getMessage()); e.printStackTrace(); } catch (IOException e) { ErrorMessages.printErrorMsg(ErrorMessages.FILECOR, fileName); ForcastUi.consoleLog(e.getMessage()); e.printStackTrace(); } catch (Exception ex) { ErrorMessages.printErrorMsg(ErrorMessages.FILECOR, fileName); ForcastUi.consoleLog(ex.getMessage()); Logger.getLogger(TopStockDescriptionList.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:manager.supplier.service.SupplierFileService.java
private List<MainCustomer> getCustomerListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<MainCustomer> items = new ArrayList<MainCustomer>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;//from w w w .j av a 2 s.c o m } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(6);// ? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { MainCustomer item = new MainCustomer(); if (row.getCell(0) == null || row.getCell(1) == null) { json.put("success", false); json.put("message", "?7" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); item.setCustomer_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setCompany_name((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"?".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
private List<Metarial> getMetarialListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<Metarial> items = new ArrayList<Metarial>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;/*from w w w. ja v a 2 s . c o m*/ } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(5); //? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { Metarial item = new Metarial(); if (row.getCell(0) == null || row.getCell(1) == null || row.getCell(2) == null) { json.put("success", false); json.put("message", "?6" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); item.setMaterial_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setMaterial_brand((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); item.setCompany_name((row.getCell(2) == null) ? "" : row.getCell(2).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"???".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
private List<Competitor> getCompetitorListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<Competitor> items = new ArrayList<Competitor>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;//from ww w . ja v a 2 s .c o m } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(7); //? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { Competitor item = new Competitor(); if (row.getCell(0) == null || row.getCell(1) == null) { json.put("success", false); json.put("message", "?8" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); item.setCompetitor_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setCompany_name((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"?".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
private List<Goods> getGoodsListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<Goods> items = new ArrayList<Goods>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;//from ww w. jav a2 s . com } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(4); //? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { Goods item = new Goods(); if (row.getCell(0) == null || row.getCell(1) == null || row.getCell(2) == null) { json.put("success", false); json.put("message", "?5" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); item.setGoods_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setGoods_brand((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); item.setCompany_name((row.getCell(2) == null) ? "" : row.getCell(2).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"??".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
private List<Devicelist> getDeviceListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<Devicelist> items = new ArrayList<Devicelist>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;/*w ww .ja v a2 s.c o m*/ } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(3); //? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { Devicelist item = new Devicelist(); if (row.getCell(0) == null || row.getCell(5) == null || row.getCell(7) == null) { json.put("success", false); json.put("message", "?4" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); if (row.getCell(1) != null) { row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); } if (row.getCell(2) != null) { row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); } if (row.getCell(6) != null) { row.getCell(6).setCellType(Cell.CELL_TYPE_STRING); } row.getCell(7).setCellType(Cell.CELL_TYPE_STRING); item.setDevice_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setSpecifications((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); item.setPlace((row.getCell(2) == null) ? "" : row.getCell(2).getStringCellValue()); item.setPrice((row.getCell(3) == null) ? 0 : row.getCell(3).getNumericCellValue()); item.setBuy_day((row.getCell(4) == null) ? null : row.getCell(4).getDateCellValue()); item.setDevice_num((int) ((row.getCell(5) == null) ? 0 : row.getCell(5).getNumericCellValue())); item.setAdvanced((row.getCell(6) == null) ? "" : row.getCell(6).getStringCellValue()); item.setCompany_name((row.getCell(7) == null) ? "" : row.getCell(7).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
private List<InvoiceTitle> getTitleListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<InvoiceTitle> items = new ArrayList<InvoiceTitle>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;//from ww w. j av a2 s . c o m } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(2); //? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { InvoiceTitle item = new InvoiceTitle(); if (row.getCell(0) == null || row.getCell(1) == null) { json.put("success", false); json.put("message", "?3" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); item.setInvoice_title_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setCompany_name((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"?".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
private List<BankAccount> getAccountListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<BankAccount> items = new ArrayList<BankAccount>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true;//w w w .ja v a 2 s. c o m } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(1); //? for (Row row : sheet) { //? // if (row.getRowNum() > 2) { BankAccount item = new BankAccount(); if (row.getCell(0) == null || row.getCell(1) == null || row.getCell(2) == null) { json.put("success", false); json.put("message", "?2" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); item.setAccount_name((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setAccount_code((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); item.setCompany_name((row.getCell(2) == null) ? "" : row.getCell(2).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"?".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }
From source file:manager.supplier.service.SupplierFileService.java
/** * @Description:?EXCEL//from w w w . j a v a 2s. c om * SupplierFileService * getListFromExcel * @param finalFile * @param json * @param request * @return * @throws Exception List<SupplierFile> * @author yukai * 2016-10-13 ?4:11:14 */ private List<SupplierFile> getSupplierListFromExcel(File finalFile, JSONObject json, HttpServletRequest request) throws Exception { List<SupplierFile> items = new ArrayList<SupplierFile>(); String fileName = finalFile.getName(); boolean isE2007 = false; //?excel2007? if (fileName.endsWith("xlsx")) { isE2007 = true; } InputStream input = new FileInputStream(finalFile); Workbook wb = null; //??(20032007)?? if (isE2007) { wb = new XSSFWorkbook(input); } else { wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(0); //? if (!sheet.iterator().hasNext()) { json.put("success", false); json.put("message", "excel"); return items; } int trLength = sheet.getLastRowNum(); if (trLength <= 2) { json.put("success", false); json.put("message", "?"); } for (Row row : sheet) { //? // if (row.getRowNum() > 2) { SupplierFile item = new SupplierFile(); if ((row.getCell(0) == null || row.getCell(0).toString().equals("")) && (row.getCell(1) == null || row.getCell(1).toString().equals("")) && (row.getCell(2) == null || row.getCell(2).toString().equals("")) && (row.getCell(3) == null || row.getCell(3).toString().equals("")) && (row.getCell(4) == null || row.getCell(4).toString().equals("")) && (row.getCell(5) == null || row.getCell(5).toString().equals("")) && (row.getCell(6) == null || row.getCell(6).toString().equals("")) && (row.getCell(7) == null || row.getCell(7).toString().equals("")) && (row.getCell(8) == null || row.getCell(8).toString().equals("")) && (row.getCell(9) == null || row.getCell(9).toString().equals("")) && (row.getCell(10) == null || row.getCell(10).toString().equals("")) && (row.getCell(11) == null || row.getCell(11).toString().equals(""))) { if (row.getRowNum() == 3) { json.put("success", false); json.put("message", "?"); } break;//? } if (row.getCell(0) == null || row.getCell(1) == null || row.getCell(2) == null || row.getCell(3) == null || row.getCell(4) == null || row.getCell(5) == null || row.getCell(6) == null || row.getCell(7) == null || row.getCell(8) == null || row.getCell(9) == null || row.getCell(10) == null) { json.put("success", false); json.put("message", "" + (row.getRowNum() + 1) + "??"); break; } row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); row.getCell(3).setCellType(Cell.CELL_TYPE_STRING); row.getCell(4).setCellType(Cell.CELL_TYPE_STRING); row.getCell(5).setCellType(Cell.CELL_TYPE_STRING); row.getCell(6).setCellType(Cell.CELL_TYPE_STRING); row.getCell(8).setCellType(Cell.CELL_TYPE_STRING); //if(row.getCell(9)!=null){ row.getCell(9).setCellType(Cell.CELL_TYPE_STRING); // } //if(row.getCell(10)!=null){ row.getCell(10).setCellType(Cell.CELL_TYPE_STRING); // } if (row.getCell(11) != null) { row.getCell(11).setCellType(Cell.CELL_TYPE_STRING); } item.setCpyname_cn((row.getCell(0) == null) ? "" : row.getCell(0).getStringCellValue()); item.setCorporation((row.getCell(1) == null) ? "" : row.getCell(1).getStringCellValue()); item.setContact_addr((row.getCell(2) == null) ? "" : row.getCell(2).getStringCellValue()); item.setContacts((row.getCell(3) == null) ? "" : row.getCell(3).getStringCellValue()); item.setM_phone((row.getCell(4) == null) ? "" : row.getCell(4).getStringCellValue()); item.setClass_name((row.getCell(5) == null) ? "" : row.getCell(5).getStringCellValue()); item.setNature_name((row.getCell(6) == null) ? "" : row.getCell(6).getStringCellValue()); item.setReg_fund((row.getCell(7) == null) ? 0 : row.getCell(7).getNumericCellValue()); item.setCurrency_name((row.getCell(8) == null) ? "" : row.getCell(8).getStringCellValue()); item.setReg_account((row.getCell(9) == null) ? "" : row.getCell(9).getStringCellValue()); item.setPassword((row.getCell(10) == null) ? "" : row.getCell(10).getStringCellValue()); item.setReg_email((row.getCell(11) == null) ? "" : row.getCell(11).getStringCellValue()); items.add(item); } else if (row.getRowNum() == 1) {// String name = (row.getCell(0) == null) ? "" : row.getCell(0).toString().trim(); if (!"".equals(name)) { json.put("success", false); json.put("message", "?"); break; } } } input.close(); return items; }