POI是apache 的java读写excel文件的类库,通过POI在服务器端读写excel文件非常方便。
如果在windows服务器或客户端上,不安装ms office,也希望能够通过vb、vc、delphi等语言批量生成或读取excel文件,则可以通过vj++6.0将poi封装为com库。下面代码是封装的例子:
vj++6.0将class ExcelWorkBook中的public方法,生成为com的接口方法,调用是直接调用相关的com方法就可以,如VB中的调用:
如果在windows服务器或客户端上,不安装ms office,也希望能够通过vb、vc、delphi等语言批量生成或读取excel文件,则可以通过vj++6.0将poi封装为com库。下面代码是封装的例子:
import
java.io.
*
;
import java.util. * ;
import java.lang.Integer;
import com.ms.security. * ;
import org.apache.poi.hssf.record. * ;
import org.apache.poi.hssf.model. * ;
import org.apache.poi.hssf.usermodel. * ;
import org.apache.poi.hssf.util. * ;
import org.apache.poi.poifs.filesystem. * ;
/**
* This class is designed to be packaged with a COM DLL output format.
* The class has no standard entry points, other than the constructor.
* Public methods will be exposed as methods on the default COM interface.
* @com.register ( clsid=711166A6-EDBA-483D-A4FC-A2FB7879859E, typelib=CE31E3B2-4F7C-4D78-83AD-D6A0FACEB746 , version="1.0", description="ExcelWorkBook Class" )
*/
public class ExcelWorkBook
{
public ExcelWorkBook()
{
m_wb = null;
m_out = null;
m_sheets = new Hashtable();
m_ActiveSheet = null;
m_Styles = new Hashtable();
m_Fonts = new Hashtable();
//m_filename = null;
m_lastErrMsg = "";
}
/**
* 创建excel文件
*/
public boolean createWorkBook(String filename)
{
try{
if (Class.forName("com.ms.security.PolicyEngine") != null){
PolicyEngine.assertPermission(PermissionID.FILEIO);
}
// create a new file
m_out = new FileOutputStream(filename);
// create a new workbook
m_wb = new HSSFWorkbook();
}catch (Exception e){
System.out.println("createWorkBook:"+e.getMessage());
m_lastErrMsg = "createWorkBook:"+e.getMessage();
return false;
}
return true;
}
/**
* 创建工作表
* @param name 表名
* @return true or false
*/
public boolean createWorkSheet(String name)
{
try{
HSSFSheet sheet = m_wb.createSheet(name);
m_sheets.put(name, sheet);
m_ActiveSheet = sheet;
}catch (Exception e){
System.out.println("createWorkSheet:"+e.getMessage());
m_lastErrMsg = "createWorkSheet:"+e.getMessage();
return false;
}
return true;
}
/**
* 打开excel文件
*/
public boolean openWorkBook(String filename)
{
try{
if (Class.forName("com.ms.security.PolicyEngine") != null){
PolicyEngine.assertPermission(PermissionID.FILEIO);
}
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
m_wb = new HSSFWorkbook(fs);
//int i=0;
//while (true)
for (int i=0;i<m_wb.getNumberOfSheets();i++)
{
HSSFSheet sheet = m_wb.getSheetAt(i);
if (sheet==null)
break;
String name = m_wb.getSheetName(i);
m_sheets.put(name, sheet);
m_ActiveSheet = sheet;
}
}catch (Exception e){
System.out.println("openWorkBook:"+e.getMessage());
e.printStackTrace();
m_lastErrMsg = "openWorkBook:"+e.getMessage();
return false;
}
return true;
}
.....
import java.util. * ;
import java.lang.Integer;
import com.ms.security. * ;
import org.apache.poi.hssf.record. * ;
import org.apache.poi.hssf.model. * ;
import org.apache.poi.hssf.usermodel. * ;
import org.apache.poi.hssf.util. * ;
import org.apache.poi.poifs.filesystem. * ;
/**
* This class is designed to be packaged with a COM DLL output format.
* The class has no standard entry points, other than the constructor.
* Public methods will be exposed as methods on the default COM interface.
* @com.register ( clsid=711166A6-EDBA-483D-A4FC-A2FB7879859E, typelib=CE31E3B2-4F7C-4D78-83AD-D6A0FACEB746 , version="1.0", description="ExcelWorkBook Class" )
*/
public class ExcelWorkBook
{
public ExcelWorkBook()
{
m_wb = null;
m_out = null;
m_sheets = new Hashtable();
m_ActiveSheet = null;
m_Styles = new Hashtable();
m_Fonts = new Hashtable();
//m_filename = null;
m_lastErrMsg = "";
}
/**
* 创建excel文件
*/
public boolean createWorkBook(String filename)
{
try{
if (Class.forName("com.ms.security.PolicyEngine") != null){
PolicyEngine.assertPermission(PermissionID.FILEIO);
}
// create a new file
m_out = new FileOutputStream(filename);
// create a new workbook
m_wb = new HSSFWorkbook();
}catch (Exception e){
System.out.println("createWorkBook:"+e.getMessage());
m_lastErrMsg = "createWorkBook:"+e.getMessage();
return false;
}
return true;
}
/**
* 创建工作表
* @param name 表名
* @return true or false
*/
public boolean createWorkSheet(String name)
{
try{
HSSFSheet sheet = m_wb.createSheet(name);
m_sheets.put(name, sheet);
m_ActiveSheet = sheet;
}catch (Exception e){
System.out.println("createWorkSheet:"+e.getMessage());
m_lastErrMsg = "createWorkSheet:"+e.getMessage();
return false;
}
return true;
}
/**
* 打开excel文件
*/
public boolean openWorkBook(String filename)
{
try{
if (Class.forName("com.ms.security.PolicyEngine") != null){
PolicyEngine.assertPermission(PermissionID.FILEIO);
}
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
m_wb = new HSSFWorkbook(fs);
//int i=0;
//while (true)
for (int i=0;i<m_wb.getNumberOfSheets();i++)
{
HSSFSheet sheet = m_wb.getSheetAt(i);
if (sheet==null)
break;
String name = m_wb.getSheetName(i);
m_sheets.put(name, sheet);
m_ActiveSheet = sheet;
}
}catch (Exception e){
System.out.println("openWorkBook:"+e.getMessage());
e.printStackTrace();
m_lastErrMsg = "openWorkBook:"+e.getMessage();
return false;
}
return true;
}
.....
vj++6.0将class ExcelWorkBook中的public方法,生成为com的接口方法,调用是直接调用相关的com方法就可以,如VB中的调用:
Dim excel As New NTOffice.ExcelWorkBookClass()
exel.createWorkBook("c:/a.xls");