我的应用程序使用的是Apache Poi 3.12版本,但我想使用4.1.0中的流式公式求值器(即SXSSFFormulaEvaluator.evaluateAllFormulaCells)。我无法升级罐子,所以我想到了使用OSGI捆绑软件进行公式评估,但出现以下错误: java.lang.NoSuchMethodError:org.apache.poi.xssf.streaming.SXSSFFormulaEvaluator.evaluateAllFormulaCells(Lorg / apache / poi / ss / usermodel / Workbook;)V 我不确定是否可以以这种方式使用OSGI。
我创建了一个激活器和一个包含EvaluationAllFormulas()及其实现类的服务,并从中创建了捆绑包。 在我的Java应用程序中,添加了以上捆绑包用于公式评估。
<form action="/new/" method="POST" class="form-group" id="new_card_form">
{% csrf_token %}
<div class="form-control form-control-lg">
<strong>Full name:</strong>
{% render_field form.cardholders_name type="input" %}
</div>
<div class="form-control form-control-lg">
<strong>Card number:</strong>
{% render_field form.card_number type="input" %}
</div>
<div class="form-control form-control-lg">
<strong>Amount (in €):</strong>
{% render_field form.card_balance type="input" %}
</div>
<!-- Input type submit bind jquery pop up message -->
<input id="pop_up" type="submit" value="Submit" class="form-control btn btn-primary">
</form>
我希望OSGIBundle jar中的validateAllFormula()可以正常工作,但出现错误:
package tutorial.example1;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceRegistration;
import tutorial.example1.client.HelloServiceImpl;
import tutorial.example1.service.HelloService;
import org.osgi.framework.ServiceEvent;
public class Activator implements BundleActivator, ServiceListener
{
private ServiceRegistration registration;
public void start(BundleContext context)
{
registration = context.registerService(
HelloService.class.getName(),
new HelloServiceImpl(),
null);
System.out.println("Starting to listen for service events");
context.addServiceListener(this);
}
public void stop(BundleContext context)
{
registration.unregister();
context.removeServiceListener(this);
System.out.println("Stopped listening for service events.");
}
public void serviceChanged(ServiceEvent arg0) {
// TODO Auto-generated method stub
}
}
package tutorial.example1.service;
import java.io.File;
public interface HelloService {
public void evaluateAllFormula(File file);
}
package tutorial.example1.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.apache.poi.xssf.streaming.SXSSFFormulaEvaluator;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import tutorial.example1.service.HelloService;
public class HelloServiceImpl implements HelloService{
public void evaluateAllFormula(File file) {
File tmpWorkBookFile = file;
FileInputStream tmpWorkBookIS;
try {
tmpWorkBookIS = new FileInputStream(tmpWorkBookFile);
XSSFWorkbook workbook = new XSSFWorkbook(tmpWorkBookIS);
SXSSFWorkbook sxssfWorkbook =new SXSSFWorkbook(workbook);
SXSSFFormulaEvaluator.evaluateAllFormulaCells(sxssfWorkbook);
FileOutputStream tmpWorkBookOS = new FileOutputStream("Test234.xlsx");
sxssfWorkbook.write(tmpWorkBookOS);
tmpWorkBookOS.close();
sxssfWorkbook.dispose();
sxssfWorkbook.close();
workbook.close();
System.out.println("Done Formula evaluation");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e) {
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>felix-tutorial</groupId>
<artifactId>example-1</artifactId>
<version>1.0</version>
<packaging>bundle</packaging>
<name>example-1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Build Configuration -->
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>*;scope=provided</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Name>Service listener example</Bundle-Name>
<Bundle-Description>A bundle that displays messages at startup
and
when service events occur</Bundle-Description>
<Bundle-Vendor>Apache Felix</Bundle-Vendor>
<Bundle-Version>1.0.0</Bundle-Version>
<Bundle-Activator>tutorial.example1.Activator</Bundle-Activator>
<Export-Package>tutorial.example1.service,
org.apache.poi;version="4.1.0",
org.apache.poi.common.*;version="4.1.0",
org.apache.poi.ddf.*;version="4.1.0",
org.apache.poi.extractor.*;version="4.1.0",
org.apache.poi.hpsf.*;version="4.1.0",
org.apache.poi.hssf.*;version="4.1.0",
org.apache.poi.poifs.*;version="4.1.0",
org.apache.poi.ss.*;version="4.1.0",
org.apache.poi.sl.*;version="4.1.0",
org.apache.poi.util.*;version="4.1.0",
org.apache.poi.wp.*;version="4.1.0",
org.apache.poi.ooxml.*;version="4.1.0",
org.apache.poi.openxml4j.*;version="4.1.0",
org.apache.poi.xddf.*;version="4.1.0",
org.apache.poi.xdgf.*;version="4.1.0",
org.apache.poi.xslf.*;version="4.1.0",
org.apache.poi.xssf.*;version="4.1.0",
org.apache.poi.xwpf.*;version="4.1.0"
</Export-Package>
<Import-Package>org.osgi.framework
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<!-- Dependecies Management -->
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package test.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import tutorial.example1.client.HelloServiceImpl;
import tutorial.example1.service.HelloService;
public class TestOsgiExcel {
public static void main(String[] args) {
File tmpWorkBookFile = new File("TestTemplate.xlsx");
try {
FileInputStream tmpWorkBookIS = new FileInputStream(tmpWorkBookFile);
XSSFWorkbook workbook = new XSSFWorkbook(tmpWorkBookIS);
tmpWorkBookIS.close();
//Method call will be there to write to excel
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook);
FileOutputStream tmpWorkBookOS = new FileOutputStream("Test234.xlsx");
sxssfWorkbook.write(tmpWorkBookOS);
tmpWorkBookOS.close();
sxssfWorkbook.dispose();
sxssfWorkbook.close();
workbook.close();
System.out.println("Done Streaming");
HelloService service = new HelloServiceImpl();
service.evaluateAllFormula(new File ("Test234.xlsx"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}