Skip to content

Commit

Permalink
[refs #1] - Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjoy-sust committed Feb 26, 2019
1 parent 8e49ac7 commit 878acb5
Show file tree
Hide file tree
Showing 15 changed files with 785 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ dist/
nbdist/
classes/
.nb-gradle/
/lib/
/out/



Binary file added Imported_Resident.xlsx
Binary file not shown.
Binary file added Resident_File.xlsx
Binary file not shown.
81 changes: 81 additions & 0 deletions src/TestProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Java Program to demonstrate adjacency list
// representation of graphs

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class TestProblem

{
public static void main(String[] args) throws IOException {

/*
InputStream inp = null;
inp = new FileInputStream("E:\\Projects\\PoiAdvanceExample\\stackProblem.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
int rowsCount = sheet.getLastRowNum();
int columnCount = sheet.getRow(0).getLastCellNum();
String[][] inputData = new String[rowsCount+1][columnCount];
for (int i = 0; i <= rowsCount; i++) {
Row row = sheet.getRow(i);
int colCounts = row.getLastCellNum();
for (int j = 0; j < colCounts; j++) {
Cell cell = row.getCell(j);
if(cell.getCellType() == CellType.NUMERIC) {
inputData[i][j] = Double.toString(cell.getNumericCellValue());
}
if(cell.getCellType() == CellType.FORMULA) {
inputData[i][j] = cell.getCellFormula();
}
if(cell.getCellType() == CellType.STRING) {
inputData[i][j] = cell.getStringCellValue();
}
}
}*/

writeData();

}

private static void writeData() throws IOException {

Workbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet();

int r = 0;
for (int i=0;i<2;i++) {
Row row = sheet.createRow(r++);
int column = 0;
for (int j =0;j<2;j++) {
XSSFCell cell = (XSSFCell) row.createCell(column++);
if (r == 1 || column == 1) cell.setCellValue(i);

else if (column == 2) {
cell.setCellFormula("OFFSET(IU220,0,1)");
XSSFFormulaEvaluator evaluator =
(XSSFFormulaEvaluator) workbook.getCreationHelper().createFormulaEvaluator();
System.out.println(evaluator.evaluateInCell(cell).getCellType());
}
}
}


FileOutputStream fileOut = new FileOutputStream("stackProblem.xlsx");
workbook.write(fileOut);
workbook.close();
}
}
//XSSFCell cell = sheet.getRow(1).createCell(1); cell.setCellFormula("OFFSET(IV220,0,1)");
71 changes: 71 additions & 0 deletions src/main/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main;

import main.com.poi.example.model.Resident;
import main.com.poi.example.service.ExcelFileManagerService;
import main.com.poi.example.service.ExcelFileManagerServiceImpl;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Created by Lenovo on 24I/10/2018.
*/
public class App {
public static void main(String[] args) {
ExcelFileManagerService service = new ExcelFileManagerServiceImpl();
Scanner scanner = new Scanner(System.in);
System.out.println("For Import press I\n" +
"For Export press X\n" +
"For Validate press V");
String operationType = scanner.next();

try {
if (operationType.equals("I")) {
System.out.println("Importing...");
System.out.println("Enter how many resident you want to import :");
int numberOfResident = scanner.nextInt();
List<Resident> residentList = new ArrayList<>();
for (int i = 0; i < numberOfResident; i++) {
Resident resident = new Resident();
System.out.println("Name : ");
resident.setName(scanner.next());
System.out.println("Address : ");
resident.setAddress(scanner.next());
System.out.println("Mobile : ");
resident.setMobile(scanner.next());
System.out.println("Email : ");
resident.setEmail(scanner.next());
System.out.println("Age : ");
resident.setAge(scanner.nextInt());
System.out.println("NationalId : ");
resident.setNationalId(scanner.next());
residentList.add(resident);
}
service.importExcel("Imported_Resident.xlsx", residentList);
} else if (operationType.equals("X")) {
service.exportExcel("Resident_File.xlsx");
} else if (operationType.equals("V")) {
try {
service.exportExcel("Resident_File.xlsx");
if (service.validateExcelFile("Resident_File.xlsx")) {
System.out.println("File is valid");
}
} catch (OpenXML4JException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}

} catch (IOException e) {
System.out.printf("Not Imported/Exported %s", e.getMessage());
}
}
}
76 changes: 76 additions & 0 deletions src/main/com/poi/example/model/Resident.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main.com.poi.example.model;

/**
* Created by Lenovo on 17/09/2018.
*/
public class Resident {
String name;
String nationalId;
String email;
String mobile;
int age;
String address;
boolean isMarried;

public boolean isMarried() {
return isMarried;
}

public void setIsMarried(boolean isMarried) {
this.isMarried = isMarried;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getNationalId() {
return nationalId;
}

public void setNationalId(String nationalId) {
this.nationalId = nationalId;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Name : " + this.name + " \n" +
"Identity : " + this.getNationalId();
}
}
34 changes: 34 additions & 0 deletions src/main/com/poi/example/model/SheetModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main.com.poi.example.model;

/**
* Created by Lenovo on 16/11/2018.
*/
public class SheetModel {
public long numberOfRows;
public long numberOfColumns;
public long timeToProcess;

public long getNumberOfRows() {
return numberOfRows;
}

public void setNumberOfRows(long numberOfRows) {
this.numberOfRows = numberOfRows;
}

public long getNumberOfColumns() {
return numberOfColumns;
}

public void setNumberOfColumns(long numberOfColumns) {
this.numberOfColumns = numberOfColumns;
}

public long getTimeToProcess() {
return timeToProcess;
}

public void setTimeToProcess(long timeToProcess) {
this.timeToProcess = timeToProcess;
}
}
8 changes: 8 additions & 0 deletions src/main/com/poi/example/model/XSSFDataTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main.com.poi.example.model;

/**
* Created by Lenovo on 14/11/2018.
*/
public enum XSSFDataTypes {
BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER;
}
21 changes: 21 additions & 0 deletions src/main/com/poi/example/service/ExcelFileManagerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main.com.poi.example.service;

import main.com.poi.example.model.Resident;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;

/**
* Created by Lenovo on 24/09/2018.
*/
public interface ExcelFileManagerService {
boolean importExcel(String fileName,List<Resident> residentList) throws IOException;

void exportExcel(String fileName) throws IOException;

boolean validateExcelFile(String fileName) throws OpenXML4JException, IOException, ParserConfigurationException, SAXException;
}
56 changes: 56 additions & 0 deletions src/main/com/poi/example/service/ExcelFileManagerServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main.com.poi.example.service;

import main.com.poi.example.model.Resident;
import main.com.poi.example.util.ParseExcelFile;
import main.com.poi.example.util.ResidentExcelUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Lenovo on 24/09/2018.
*/
public class ExcelFileManagerServiceImpl implements ExcelFileManagerService {
@Override
public boolean importExcel(String fileName,List<Resident> residentList) throws IOException {
ResidentExcelUtil.buildFile(fileName, residentList);
return true;
}

@Override
public void exportExcel(String fileName) throws IOException {
List<Resident> residentList = new ArrayList<>();
for(int i = 0;i<10;i++)
{
Resident resident = new Resident();
resident.setName("Name"+i);
resident.setMobile("0142485824" + i);
resident.setAddress("ABC" + i);
resident.setEmail("count" + i + "@gmail.com");
resident.setNationalId("8687678687687" + i);
resident.setAge(i+30);
residentList.add(resident);
}
ResidentExcelUtil.buildFile(fileName, residentList);
}

@Override
public boolean validateExcelFile(String fileName) throws OpenXML4JException, IOException, ParserConfigurationException, SAXException {

File file = new File(fileName);
OPCPackage opcPackage = OPCPackage.open(file);
ParseExcelFile parsedFile = new ParseExcelFile(opcPackage,10,Resident.class);
boolean isValid = parsedFile.process();
return isValid;
}
}
21 changes: 21 additions & 0 deletions src/main/com/poi/example/service/ResidentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main.com.poi.example.service;

import main.com.poi.example.model.Resident;

/**
* Created by Lenovo on 17/09/2018.
*/
public class ResidentService {

public Resident getResident() {
Resident resident = new Resident();
resident.setName("Flop Coder");
resident.setAddress("Coding Area");
resident.setEmail("flop.official@gmail.com");
resident.setMobile("+8801719149***");
resident.setAge(38);
resident.setNationalId("36524585458585545555");
resident.setIsMarried(true);
return resident;
}
}
13 changes: 13 additions & 0 deletions src/main/com/poi/example/util/ExcelFileHeaderConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main.com.poi.example.util;

/**
* Created by Lenovo on 23/10/2018.
*/
public final class ExcelFileHeaderConstants {
public static final String NAME = "NAME";
public static final String NATIONAL_ID = "NID";
public static final String AGE = "AGE";
public static final String MOBILE = "MOBILE";
public static final String EMAIL = "EMAIL";
public static final String ADDRESS = "ADDRESS";
}
Loading

0 comments on commit 878acb5

Please sign in to comment.