Существует задача по экспорту данных из файла Excel в AggreGate.
Решил попробовать сделать это с использованием библиотеки Apache POI http://poi.apache.org/
Создал модель и написал функцию, которая должна вернуть необходимые данные. Код ниже:
- Code: Select all
import com.tibbo.aggregate.common.context.*;
import com.tibbo.aggregate.common.datatable.*;
import com.tibbo.aggregate.common.server.*;
import com.tibbo.linkserver.*;
import com.tibbo.linkserver.context.*;
import org.apache.poi.openxml4j.exceptions.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class %ScriptClassNamePattern% implements FunctionImplementation
{
private final static String FILE_PATH = "C:\\example\\";
private final static String SHEET = "исполнение";
private static TableFormat makeFormat() {
TableFormat tf = new TableFormat();
tf.addField(FieldFormat.STRING_FIELD, "pnm", "ПНМ");
tf.addField(FieldFormat.DOUBLE_FIELD, "planDay", "План");
tf.addField(FieldFormat.DOUBLE_FIELD, "factDay", "Факт");
tf.addField(FieldFormat.DOUBLE_FIELD, "executionPercentDay", "Исполнение");
tf.addField(FieldFormat.DOUBLE_FIELD, "deviationDay", "Отклонение");
tf.addField(FieldFormat.DOUBLE_FIELD, "planTotal", "ПланМес");
tf.addField(FieldFormat.DOUBLE_FIELD, "factTotal", "ФактМес");
tf.addField(FieldFormat.DOUBLE_FIELD, "executionPercentTotal", "ИсполнениеМес");
tf.addField(FieldFormat.DOUBLE_FIELD, "deviationTotal", "ОтклонениеМес");
return tf;
}
public DataTable execute(Context con, FunctionDefinition def, CallerController caller, RequestController request, DataTable parameters) throws ContextException
{
Date date = new Date();
SimpleDateFormat fmtMonth = new SimpleDateFormat("MMMM");
SimpleDateFormat fmtYear = new SimpleDateFormat("yy");
String excelFile = fmtMonth.format(date)+"_"+fmtYear.format(date)+".xlsx";
DataTable tbl = new DataTable(makeFormat());
try {
FileInputStream inputStream = new FileInputStream(FILE_PATH+excelFile);
Workbook wb = WorkbookFactory.create(inputStream);
Sheet sheet = wb.getSheet(SHEET);
for (int i = 5; i <= 13; i++) {
Row row = sheet.getRow(i);
DataRecord rec = new DataRecord(makeFormat());
rec.setValue(0, row.getCell(0).getStringCellValue())
.setValue(1, row.getCell(9).getNumericCellValue())
.setValue(2, row.getCell(10).getNumericCellValue())
.setValue(3, row.getCell(11).getNumericCellValue())
.setValue(4, row.getCell(18).getNumericCellValue())
.setValue(5, row.getCell(19).getNumericCellValue())
.setValue(6, row.getCell(20).getNumericCellValue())
.setValue(7, row.getCell(21).getNumericCellValue())
.setValue(8, row.getCell(22).getNumericCellValue());
tbl.addRecord(rec);
}
}
catch (FileNotFoundException fnfEx) {
DataRecord rec = new DataRecord(makeFormat());
rec.setValue(0, fnfEx.getLocalizedMessage())
.setValue(1, 0)
.setValue(2, 0)
.setValue(3, 0)
.setValue(4, 0)
.setValue(5, 0)
.setValue(6, 0)
.setValue(7, 0)
.setValue(8, 0);
tbl.addRecord(rec);
}
catch (IOException ioEx){
DataRecord rec = new DataRecord(makeFormat());
rec.setValue(0, ioEx.getLocalizedMessage())
.setValue(1, 0)
.setValue(2, 0)
.setValue(3, 0)
.setValue(4, 0)
.setValue(5, 0)
.setValue(6, 0)
.setValue(7, 0)
.setValue(8, 0);
tbl.addRecord(rec);
}
catch (InvalidFormatException invalidFormatEx){
DataRecord rec = new DataRecord(makeFormat());
rec.setValue(0, invalidFormatEx.getLocalizedMessage())
.setValue(1, 0)
.setValue(2, 0)
.setValue(3, 0)
.setValue(4, 0)
.setValue(5, 0)
.setValue(6, 0)
.setValue(7, 0)
.setValue(8, 0);
tbl.addRecord(rec);
}
return tbl;
}
}
*.jar файлы Apache POI поместил в каталог \jar установки AG. Компиляция и сохранение функции модели происходят без ошибок, но при вызове функции выдается следующая ошибка:
- Code: Select all
com.tibbo.aggregate.common.context.ContextException: Ошибка вызова функции 'getData' из контекста 'Excel Reader': class "org.apache.poi.ss.usermodel.Workbook"'s signer information does not match signer information of other classes in the same package
at com.tibbo.aggregate.client.action.executor.ShowErrorExecutor.execute(ShowErrorExecutor.java:31)
at com.tibbo.aggregate.client.action.ExecutionHelper.executeCommand(ExecutionHelper.java:29)
at com.tibbo.aggregate.client.operation.InvokeActionOperation$ActionWorker.processCommand(InvokeActionOperation.java:314)
at com.tibbo.aggregate.client.operation.InvokeActionOperation$ActionWorker.stepAction(InvokeActionOperation.java:291)
at com.tibbo.aggregate.client.operation.InvokeActionOperation$ActionWorker.run(InvokeActionOperation.java:239)
com.tibbo.aggregate.common.context.ContextException: Ошибка вызова функции 'getData' из контекста 'Excel Reader': class "org.apache.poi.ss.usermodel.Workbook"'s signer information does not match signer information of other classes in the same package
at com.tibbo.aggregate.common.context.AbstractContext.callFunction(AbstractContext.java:2917)
at com.tibbo.aggregate.common.context.AbstractContext.callFunction(AbstractContext.java:3025)
at com.tibbo.aggregate.common.context.AbstractContext.callFunction(AbstractContext.java:3031)
at com.tibbo.linkserver.action.CallFunctionAction$CallFunctionActionImpl.execute(CallFunctionAction.java:292)
at com.tibbo.linkserver.action.CallFunctionAction$CallFunctionActionImpl.execute(CallFunctionAction.java:236)
at com.tibbo.aggregate.common.action.SingleThreadAction$ActionThread.run(SingleThreadAction.java:261)
Caused by: java.lang.SecurityException: class "org.apache.poi.ss.usermodel.Workbook"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at users_admin_models_excelReader_getData.execute(users_admin_models_excelReader_getData.java:50)
at com.tibbo.aggregate.common.context.AbstractContext.executeImplementation(AbstractContext.java:2943)
at com.tibbo.aggregate.common.context.AbstractContext.callFunction(AbstractContext.java:2877)
... 5 more
Судя по всему пакеты Apache POI конфликтуют с пакетами AG. Это как-то можно обойти?