java 直接调用micorosoft office (2003-2007和2010版本)中excel中计算函数

主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx

读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL

你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息

使用JXL技术 :java的jxl技术导入Excel

下面是本文的项目结构:

========================================

项目中所需要的jar文件:

==================================

所用的Excel数据(2003-2007,2010都是一样的数据

===========================================================================

运行效果:

=================================================

源码部分:

=================================================

/Excel2010/src/com/b510/common/Common.java

/**
*
*/
package com.b510.common;

/**
* @author Hongten
* @created 2014-5-21
*/
public class Common {

public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";

public static final String EMPTY = "";
public static final String POINT = ".";
public static final String LIB_PATH = "lib";
public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2003_POSTFIX;
public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2010_POSTFIX;
public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
public static final String PROCESSING = "Processing...";

}

==============================================

=================================================

/Excel2010/src/com/b510/excel/ReadExcel.java

/**
*
*/
package com.b510.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.b510.common.Common;
import com.b510.excel.util.Util;
import com.b510.excel.vo.Student;

/**
* @author Hongten
* @created 2014-5-20
*/
public class ReadExcel {

/**
* read the Excel file
* @param path the path of the Excel file
* @return
* @throws IOException
*/
public List<Student> readExcel(String path) throws IOException {
if (path == null || Common.EMPTY.equals(path)) {
return null;
} else {
String postfix = Util.getPostfix(path);
if (!Common.EMPTY.equals(postfix)) {
if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
return readXls(path);
} else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
return readXlsx(path);
}
} else {
System.out.println(path + Common.NOT_EXCEL_FILE);
}
}
return null;
}

/**
* Read the Excel 2010
* @param path the path of the excel file
* @return
* @throws IOException
*/
public List<Student> readXlsx(String path) throws IOException {
System.out.println(Common.PROCESSING + path);
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow != null) {
student = new Student();
XSSFCell no = xssfRow.getCell(0);
XSSFCell name = xssfRow.getCell(1);
XSSFCell age = xssfRow.getCell(2);
XSSFCell score = xssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
}

/**
* Read the Excel 2003-2007
* @param path the path of the Excel
* @return
* @throws IOException
*/
public List<Student> readXls(String path) throws IOException {
System.out.println(Common.PROCESSING + path);
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
student = new Student();
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
HSSFCell age = hssfRow.getCell(2);
HSSFCell score = hssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
}

@SuppressWarnings("static-access")
private String getValue(XSSFCell xssfRow) {
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfRow.getNumericCellValue());
} else {
return String.valueOf(xssfRow.getStringCellValue());
}
}

@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
}

=========================

=============================================================================

/Excel2010/src/com/b510/excel/client/Client.java

/**
*
*/
package com.b510.excel.client;

import java.io.IOException;
import java.util.List;

import com.b510.common.Common;
import com.b510.excel.ReadExcel;
import com.b510.excel.vo.Student;

/**
* @author Hongten
* @created 2014-5-21
*/
public class Client {

public static void main(String[] args) throws IOException {
String excel2003_2007 = Common.STUDENT_INFO_XLS_PATH;
String excel2010 = Common.STUDENT_INFO_XLSX_PATH;
// read the 2003-2007 excel
List<Student> list = new ReadExcel().readExcel(excel2003_2007);
if (list != null) {
for (Student student : list) {
System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
}
}
System.out.println("======================================");
// read the 2010 excel
List<Student> list1 = new ReadExcel().readExcel(excel2010);
if (list1 != null) {
for (Student student : list1) {
System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
}
}
}
}

===============================================-----------========================

====================

/Excel2010/src/com/b510/excel/util/Util.java

/**
*
*/
package com.b510.excel.util;

import com.b510.common.Common;

/**
* @author Hongten
* @created 2014-5-21
*/
public class Util {

/**
* get postfix of the path
* @param path
* @return
*/
public static String getPostfix(String path) {
if (path == null || Common.EMPTY.equals(path.trim())) {
return Common.EMPTY;
}
if (path.contains(Common.POINT)) {
return path.substring(path.lastIndexOf(Common.POINT) + 1, path.length());
}
return Common.EMPTY;
}
}

===============================

===============================================================================

/Excel2010/src/com/b510/excel/vo/Student.java

/**
*
*/
package com.b510.excel.vo;

/**
* Student
*
* @author Hongten
* @created 2014-5-18
*/
public class Student {
/**
* id
*/
private Integer id;
/**
* 学号
*/
private String no;
/**
* 姓名
*/
private String name;
/**
* 学院
*/
private String age;
/**
* 成绩
*/
private float score;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getNo() {
return no;
}

public void setNo(String no) {
this.no = no;
}

public String getName() {
return name;
}

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

public String getAge() {
return age;
}

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

public float getScore() {
return score;
}

public void setScore(float score) {
this.score = score;
}

}

==============================================================================

++++++++++++++++++++++++++++++++++++++++++++++_____________________+++++++++++++++++

时间: 2024-10-10 21:59:31

java 直接调用micorosoft office (2003-2007和2010版本)中excel中计算函数的相关文章

Office 2003 2007 2010 配置进度 正在配置 解决方案 (转载)

在安装过Office2003.2007 或者2010之后,如果没有选择全部的组件,或者是因为安装到非系统盘,有时候打开 Office 文档的时候就会出现正在配置Office,或者Office配置进度的对话框,虽然总不会影响使用,但每次打开之后都会出现,是很浪费时间的一件事,对于我们这些视时间如金钱的人来说,当然不能容忍它再次发生了.所以现在给出解决办法. 如果出现这种情况,就 “开始”--“运行”对话框里输入 “cmd”--回车(如果是Vista 或者 Windows 7,则右键--管理员身份打

C# Winform 关于97-2003 和 2007 两个版本的excel 的 数据连接声明

//2003(Microsoft.Jet.Oledb.4.0) string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath); //2007(Microsoft.ACE.OLEDB.12.0) string strConn = string.Format(&q

【Java】无须额外的包,把Java中的内容输出到Excel中,无乱码,绝对兼容Excel2003与2007

Java输出一段文本到txt中大家基本都会了,这已经是学习Java的必修课了,不会也没有问题,具体可以看<[Java]输入与输出与JDK1.5之后的新型字符串StringBuilder>(点击打开链接).网上对于Java内容转化成Excel的内容大多数都是需要什么poi包,jsl包,一堆奇奇怪怪的jar插件.其实仅仅利用java.io.*;这个基本包就能够把Java中的内容输出到Excel表中,当然,如果你是要处理Mysql数据库不要这样做了,直接一条Mysql的查询命令就能把Mysql的查询

如何实现Office 2003自动配置邮件账户

众所周知,Office 2003中的outlook是不支持autodiscover的,想要在配置邮件账户的过程中实现自动读取服务器信息,最起码客户端Office的版本要升级到Office 2007,但是很多情况下手动配置邮件账户是一件很繁琐,而且很枯燥的事情,尤其是对于一些IT环境比较薄弱的企业来说,管理员可能最常做的就是为员工配置Outlook账户,那么有没有方法可以让这个过程变得简单一些呢,前些天做了个小实验,下边来介绍一下. 这个方法的主要思路其实很简单,就是利用OCT这个工具,OCT这个

Microsoft Office 2003 SP3和Microsoft Office Professional Plus 2010 SP1 VOL完美共存安装教程

Microsoft Office 2003 SP3和Microsoft Office Professional Plus 2010 SP1 VOL完美共存安装教程 1.安装版本准备 建议从http://msdn.itellyou.cn/下载正版Microsoft Office 2003 SP3和Microsoft Office Professional Plus 2010 SP1 VOL 2.先安装Microsoft Office 2003,在安装SP3升级包,以及Office 2007/201

让 Visio 2003/2007 同时开多个独立窗口

1. 打开 Visio 2003/2007 2. 点击菜单[工具] -> [选项]: 3. 在弹出的“选项” 对话框中选择“高级”选项页: 4. 去掉“在同一窗口中打开每一 ShapeSheet” 前的对勾: 5. 选中“将所有设置放入 Windows 注册表中” 前的对勾: 6. 最好关闭 Visio 再打开,确保将设置项写入注册表: 7. 点[开始]菜单,点“运行(R)...”,输入 regedit 打开注册表编辑器: 8. 进入 HKEY_CURRENT_USER/Software/Mic

分布式服务架构之java远程调用技术浅析

分布式服务架构之java远程调用技术浅析     在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MINA.ESB.Burlap.Hessian.SOAP.EJB和JMS等,这些名词之间到底是些什么关系呢,它们背后到底是基于什么原理实现的呢,了解这些是实现分布式服务框架的基础知识,而如果在性能上有高的要求的话,那深入了解这些技术背后的机制就是必须的了,在这篇blog中我们将来一探究竟,抛砖引玉,欢迎大家提供更多的实现远程通讯

JAVA读取Excel2003、2007、2010教程

import java.io.File;import java.io.FileInputStream;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.usermodel.WorkbookFactory; public class TestExcel

Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import