Browse Source

create excell works

master
esoe 2 years ago
parent
commit
7debe765b3
  1. 35
      pom.xml
  2. BIN
      report.xlsx
  3. 3
      src/main/java/ru/egspt/DataPane.java
  4. 12
      src/main/java/ru/egspt/ReportControls.java
  5. 21
      src/main/java/ru/egspt/ReportPane.java
  6. 79
      src/main/java/ru/egspt/ReportTableModel.java
  7. BIN
      target/classes/ru/egspt/DataPane.class
  8. BIN
      target/classes/ru/egspt/ReportControls.class
  9. BIN
      target/classes/ru/egspt/ReportPane.class
  10. BIN
      target/classes/ru/egspt/ReportTableModel.class

35
pom.xml

@ -21,6 +21,41 @@
<jacoco.unit-tests.limit.method-complexity>5</jacoco.unit-tests.limit.method-complexity> <jacoco.unit-tests.limit.method-complexity>5</jacoco.unit-tests.limit.method-complexity>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId> org.apache.cassandra</groupId>
<artifactId>cassandra-all</artifactId>
<version>4.0.3</version>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>

BIN
report.xlsx

Binary file not shown.

3
src/main/java/ru/egspt/DataPane.java

@ -43,8 +43,9 @@ public class DataPane extends JPanel{
public void setupControlPane(){ public void setupControlPane(){
controlPane.add(generateXLSXButton); controlPane.add(generateXLSXButton);
controlPane.add(generateTXTButton); controlPane.add(generateTXTButton);
TitledBorder border = new TitledBorder(new LineBorder(Color.black), "ControlPane", TitledBorder.CENTER, TitledBorder.CENTER); TitledBorder border = new TitledBorder(new LineBorder(Color.black), "data-controls-pane", TitledBorder.CENTER, TitledBorder.CENTER);
controlPane.setBorder(border); controlPane.setBorder(border);
controlPane.setVisible(false);
} }
/** /**
* Настройка панели пользователей * Настройка панели пользователей

12
src/main/java/ru/egspt/ReportControls.java

@ -0,0 +1,12 @@
package ru.egspt;
public class ReportControls {
public static void getExcellReport(App app){
System.out.println("Инициировано событие getExcellReport ...");
app.getReportPane().getReportTableModel().toExcell();
}
public static void implantData(App app){
System.out.println("Инициировано событие implantData ...");
}
}

21
src/main/java/ru/egspt/ReportPane.java

@ -3,6 +3,7 @@ package ru.egspt;
import java.awt.Color; import java.awt.Color;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTable; import javax.swing.JTable;
@ -15,20 +16,36 @@ public class ReportPane extends JPanel {
private ReportTableModel reportTableModel; private ReportTableModel reportTableModel;
private JTable reportTable; private JTable reportTable;
private JPanel reportTextPane = new JPanel(); private JPanel reportTextPane = new JPanel();
private JPanel reportSettingsPane = new JPanel(); private JPanel reportControlsPane = new JPanel();
private JButton getExcellReportButton = new JButton();
private JButton implantDataButton = new JButton();
//панель по данным App //панель по данным App
public ReportPane(App app){ public ReportPane(App app){
setupReportTablePane(app.getData()); setupReportTablePane(app.getData());
setupReportControlsPane(app);
} }
public void init(){ public void init(){
this.setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
this.add(reportTablePane, BorderLayout.CENTER); this.add(reportTablePane, BorderLayout.CENTER);
//this.add(reportTablePane, BorderLayout.NORTH); this.add(reportControlsPane, BorderLayout.NORTH);
TitledBorder border = new TitledBorder(new LineBorder(Color.black), "report", TitledBorder.CENTER, TitledBorder.CENTER); TitledBorder border = new TitledBorder(new LineBorder(Color.black), "report", TitledBorder.CENTER, TitledBorder.CENTER);
this.setBorder(border); this.setBorder(border);
this.setVisible(true); this.setVisible(true);
} }
public void setupReportControlsPane(App app){
getExcellReportButton.setText("create excell");
getExcellReportButton.addActionListener(e -> ReportControls.getExcellReport(app));
implantDataButton.setText("implant data");
implantDataButton.addActionListener(e -> ReportControls.implantData(app));
reportControlsPane.add(getExcellReportButton);
reportControlsPane.add(implantDataButton);
TitledBorder border = new TitledBorder(new LineBorder(Color.black), "report-controls-pane", TitledBorder.CENTER, TitledBorder.CENTER);
reportControlsPane.setBorder(border);
reportControlsPane.setVisible(true);
}
public void setupReportTablePane(Data report){ public void setupReportTablePane(Data report){
reportTableModel = new ReportTableModel(report); reportTableModel = new ReportTableModel(report);
reportTable = new JTable(reportTableModel); reportTable = new JTable(reportTableModel);

79
src/main/java/ru/egspt/ReportTableModel.java

@ -1,9 +1,19 @@
package ru.egspt; package ru.egspt;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReportTableModel extends AbstractTableModel{ public class ReportTableModel extends AbstractTableModel{
private Object[] header; private Object[] header;
@ -172,6 +182,75 @@ public class ReportTableModel extends AbstractTableModel{
public Object[][] getData() { public Object[][] getData() {
return data; return data;
} }
public void toExcell(){
System.out.println("Формирование report.xlsx ...");
// создание самого excel файла в памяти
XSSFWorkbook book = new XSSFWorkbook();
// создание листа
XSSFSheet sheet = book.createSheet("Report");
//пишем названия тестов в заголовки xlsx
int row = 0;//строка
int col = 0;//столбец
Row r = sheet.createRow(row);
while (col < getHeader().length){
r.createCell(col).setCellValue(getHeader()[col].toString());
col++;
}
System.out.println("Заголовки в report.xlsx сформированы.");
//построчно пишем данные
System.out.println("Построчно пишем данные .... ");
row++;
int userCurr = 0;
while (userCurr < getData().length){
col = 0;
r = sheet.createRow(row);
while(col < getHeader().length){
Cell c = r.createCell(col);
if (getData()[userCurr][col] == null){
c.setCellValue("");
}
else{
c.setCellValue(getData()[userCurr][col].toString());
}
//
col++;
}
userCurr++;
row++;
}
//пишем книгу в файл
String location = System.getProperty("user.dir") + "\\report.xlsx";
Path path = Paths.get(location);
if (Files.exists(path)) {
try (FileOutputStream out = new FileOutputStream(location)) {
book.write(out);
book.close();
out.close();
System.out.println("Excel файл успешно перезаписан!");
System.out.println(location);
}
catch (IOException e){
e.printStackTrace();
}
}
else {
try (FileOutputStream out = new FileOutputStream(new File(location))) {
book.write(out);
book.close();
out.close();
System.out.println("Excel файл успешно создан!");
System.out.println(location);
}
catch (IOException e){
e.printStackTrace();
}
}
}
/*
* выводим данные модели в консоль
*/
public void toConsole(){ public void toConsole(){
//заголовки //заголовки
int i = 0; int i = 0;

BIN
target/classes/ru/egspt/DataPane.class

Binary file not shown.

BIN
target/classes/ru/egspt/ReportControls.class

Binary file not shown.

BIN
target/classes/ru/egspt/ReportPane.class

Binary file not shown.

BIN
target/classes/ru/egspt/ReportTableModel.class

Binary file not shown.
Loading…
Cancel
Save