`

POI兼容读取Excel2003和Excel2007

 
阅读更多
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.csair.smms.inituserinfo.dto.UserInsert;
import com.csair.smms.inituserinfo.service.InitUserInfoService;
import com.csair.smms.preventive.service.PreventiveInfoService;

@ParentPackage("json-default")
@Namespace("/upload")
@Controller
public class UploadUserAction {
	private static int version2003 = 2003;
	private static int version2007 = 2007;
	private static int version = version2003;

	private File xlsBook;
	private String xlsBookFileName;
	private String xlsBookContentType;

	private static Logger logger = Logger.getLogger(UploadUserAction.class);
	@Autowired
	private InitUserInfoService initUserInfoService;

	@Autowired
	private PreventiveInfoService preventiveInfoService;

	@JSON(serialize = false)
	public File getXlsBook() {
		return xlsBook;
	}

	public void setXlsBook(File xlsBook) {
		this.xlsBook = xlsBook;
	}

	@JSON(serialize = false)
	public String getXlsBookFileName() {
		return xlsBookFileName;
	}

	public void setXlsBookFileName(String xlsBookFileName) {
		this.xlsBookFileName = xlsBookFileName;
	}

	@JSON(serialize = false)
	public String getXlsBookContentType() {
		return xlsBookContentType;
	}

	public void setXlsBookContentType(String xlsBookContentType) {
		this.xlsBookContentType = xlsBookContentType;
	}

        //此方法判别Excel2003和Excel2007
	public void initType() {
		String name = getXlsBookFileName();
		if (name != null) {
			int index = getXlsBookFileName().indexOf(".");
			String suffex = name.substring(index);
			if ("xls".equals(suffex)) {
				version = version2003;
			} else if ("xlsx".equals(suffex)) {
				version = version2007;
			}
		}
	}

    @Action(value = "uploadUser", results = { @Result(type = "json") })
	public String analizeExcel() {
		if (xlsBook != null) {
			initType();
			InputStream is = null;
			List<UserInsert> userList = null;
			try {
				is = new FileInputStream(xlsBook);

				version = (xlsBookFileName.endsWith(".xls") ? version2003
						: version2007);
				if (version == 2003) {// 2003
					POIFSFileSystem fs = new POIFSFileSystem(is);
					HSSFWorkbook wb = new HSSFWorkbook(fs);
					HSSFSheet sheet = wb.getSheetAt(0);
					userList = readUser(sheet);
				} else if (version == 2007) {// 2007
					XSSFWorkbook xwb = new XSSFWorkbook(is);
					XSSFSheet sheet = xwb.getSheetAt(0);
					userList = readUser(sheet);
				}

			} catch (FileNotFoundException e) {

			} catch (IOException e) {

			}
                       // 保存javabean逻辑
                       //自己的逻辑代码

		}
	return com.opensymphony.xwork2.Action.SUCCESS;	
	}

        //此方法为读取表格核心方法
	public List<UserInsert> readUser(Sheet sheet) {
		List<UserInsert> userList = new ArrayList<UserInsert>();

		int rowNum = sheet.getPhysicalNumberOfRows();
		UserInsert ui = null;
		for (int i = 1; i < rowNum; i++) {
			Row row = sheet.getRow(i);
			Cell c = row.getCell(0);
			ui = new UserInsert();
			if (c != null) {
				if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
					long id = (long) row.getCell(0).getNumericCellValue();
					ui.setId(id + "");
				} else {
					ui.setId(c.getStringCellValue());
				}

				c = row.getCell(1);
				if (c != null) {
					ui.setName(row.getCell(1).getStringCellValue());
				} else {
					ui.setName("");
				}

				c = row.getCell(2);
				if (c != null) {
					ui.setSex(row.getCell(2).getStringCellValue());
				} else {
					ui.setSex("");
				}

				c = row.getCell(3);
				if (c != null) {
					ui.setBase(row.getCell(3).getStringCellValue());
				} else {
					ui.setBase("");
				}

				c = row.getCell(4);
				if (c != null) {
					ui.setBaseCode(row.getCell(4).getStringCellValue());
				} else {
					ui.setBaseCode("");
				}

				c = row.getCell(5);
				if (c != null) {
					ui.setDepartment(row.getCell(5).getStringCellValue());
				} else {
					ui.setDepartment("");
				}

				c = row.getCell(6);
				if (c != null) {
					ui.setPosition(row.getCell(6).getStringCellValue());
				} else {
					ui.setPosition("");
				}

				c = row.getCell(7);
				if (c != null) {
					ui.setRole(row.getCell(7).getStringCellValue());
				} else {
					ui.setRole("");
				}

				c = row.getCell(8);
				if (c != null) {
					if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
						long m = (long) row.getCell(8).getNumericCellValue();
						ui.setMobile(m + "");
					} else {
						ui.setMobile(c.getStringCellValue());
					}
				} else {
					ui.setMobile("");
				}

				c = row.getCell(9);
				if (c != null) {
					if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
						long t = (long) row.getCell(9).getNumericCellValue();
						ui.setTelephone(t + "");
					} else {
						ui.setTelephone(c.getStringCellValue());
					}
				} else {
					ui.setTelephone("");
				}

				c = row.getCell(10);
				if (c != null) {
					ui.setEmail(row.getCell(10).getStringCellValue());
				} else {
					ui.setEmail("");
				}
				userList.add(ui);
			}

		}
		return userList;
	}

	public static void main(String[] args) {
		UploadUserAction uua = new UploadUserAction();
		uua.analizeExcel();
		System.out.println("end-----------------");
	}

}



JavaBean类:
public class UserInsert implements Serializable{
	
	private static final long serialVersionUID = -796538816605301094L;
	private String id;// 员工号
	private String name;// 姓名
	private String sex;// 性别
	private String base;// 所属基地
	private String baseCode;// 基地三字码
	private String department;// 部门
	private String position;// 职务
	private String role;// 系统角色
	private String mobile;// 手机
	private String telephone;// 办公电话
	private String email;// 邮箱

        //此处省略各属性的setter和getter方法
}


说明:
此类为Struts文件上传类,需要POI包的支持,在附件有,包含的jar包为:
poi-3.8.jar
poi-ooxml-3.8.jar
poi-ooxml-schemas-3.8.jar
xmlbeans-2.4.jar


  • POI.zip (8.2 MB)
  • 下载次数: 112
0
4
分享到:
评论

相关推荐

    通过POI统一读取Excel文件(兼容97-2003和2007+两种格式)

    这篇博客“通过POI统一读取Excel文件(兼容97-2003和2007+两种格式)”正是介绍了如何使用Apache POI库来处理不同版本的Excel文件。 Apache POI 提供了两个主要的API来处理Excel文件:HSSF(Horrible Spreadsheet ...

    poi读取excel2007和2003兼容工具例子

    在这个"poi读取excel2007和2003兼容工具例子"中,我们将探讨如何使用POI来读取不同版本的Excel文件,特别是Excel 2003(.xls)和Excel 2007及更高版本(.xlsx)。 1. **Apache POI库**:Apache POI是Apache软件基金...

    POI兼容Excel2003和2007

    在本文中,我们将重点讨论如何使用POI库来实现对Excel 2003和2007文件的兼容性操作。Excel 2003使用的是.BIFF(Binary Interchange File Format)格式,而Excel 2007及以上版本则引入了新的.OpenXML(.xlsx)格式,...

    poi读取excel文件实例(兼容excel2007)

    在“poi读取excel文件实例”中,我们将讨论如何使用Apache POI API来读取和操作Excel 2007文件。以下是一些关键知识点: 1. **创建工作簿对象**:首先,你需要通过`WorkbookFactory`类的`create()`方法打开或创建一...

    jsp+jspsmart上传+poi3.8读取excel2007+jxl读取excel2003

    在给定的标题和描述中,"jsp+jspsmart上传+poi3.8读取excel2007+jxl读取excel2003" 提到了一种结合Java Web技术实现文件上传,并通过不同库解析Excel文件的解决方案。这里我们将详细探讨涉及的各个知识点。 首先,`...

    poi3.9读写excel兼容03和07版本

    "poi3.9读写excel兼容03和07版本"这个标题指的是使用Apache POI 3.9版本的API,能够兼容两种不同格式的Excel文件:.xls(Excel 2003及更早版本)和.xlsx(Excel 2007及更高版本)。 在描述中提到的"完美修订版本...

    poi读取excel文件

    标题提到的"poi读取excel文件",指的是使用Apache POI库来处理Excel数据。在最新的版本中,POI引入了更高效和强大的功能,使得处理Excel文件变得更加方便。 描述中提到了"最新版poi相关的6个jar包",这些jar包是...

    POI读取EXCEL教程

    读取Excel文件使用HSSF的usermodel API非常直观。首先,你需要创建一个`InputStream`,然后实例化`HSSFWorkbook`: ```java InputStream myxls = new FileInputStream("workbook.xls"); HSSFWorkbook wb = new ...

    android POI 读取excel 精简jar

    在Android平台上,使用Apache POI库来读取Excel文件是一种常见的解决方案。Apache POI是一个流行的开源项目,它允许程序员创建、修改和显示Microsoft Office格式的文件,包括Excel(.xls和.xlsx)。在这个“android ...

    java中poi读写excel封装工具类(兼容office2003和2007等版本)

    它不仅支持旧版的Excel文件格式(.xls,用于Office 2003及更早版本),还支持新版本的Excel文件格式(.xlsx,自Office 2007起)。以下是对"java中poi读写excel封装工具类"这一主题的详细解释。 1. **Apache POI介绍...

    POI兼容2003 和 2007

    ### POI兼容2003 和 2007 #### 概述 Apache POI 是一个用于读写Microsoft Office格式文件(如Excel、Word等)的Java API库。随着Office版本的更新,POI也提供了不同的API来支持不同版本的文件格式。本篇文章将深入...

    (实例)java poi完美解决excel2003和2007、2010之间兼容问题

    在Java世界中,当需要处理Excel数据时,POI库是一个常用的选择,尤其在需要跨版本兼容性时,如在Excel 2003、2007和2010之间。以下将详细讲解如何使用Java POI来完美解决这些版本之间的兼容问题。 首先,Excel 2003...

    java读取excel(兼容2003和2007)

    在Java编程中,读取Excel文件是一项常见的任务,特别是在数据处理和分析的场景下。Excel文件格式有两种主要版本,即2003年的.xls格式和2007年及以后的.xlsx格式。为了兼容这两种格式,Java引入了Apache POI库,这是...

    poi java读取excel所需jar包

    以下是关于"poi java读取excel所需jar包"的详细知识讲解。 1. **Apache POI 主要jar包**: - `poi-3.7-sources.jar`:这是Apache POI的主要源代码包,虽然在运行时并不需要源代码,但如果你需要查看或调试POI的...

    poi读取excel的jar包

    标题中的"poi读取excel的jar包"指的是Apache POI库的一个版本,它包含了读取Excel 2007文件所需的类和方法。在Java程序中,引入这个jar包后,开发者可以编写代码来处理.xlsx文件,无论是读取数据、解析工作表还是...

    POI 读取Excel文件

    - POI 不支持读取 .xls 文件(Excel 97-2003 格式),除非使用 HSSFWorkbook 类,但不推荐,因为 .xlsx 格式更现代且兼容性更好。 - 处理大型文件时,要特别注意内存管理,避免因数据量过大导致 OutOfMemoryError...

    POI导入excel大数据处理,支持excel2003,2007

    标题提到的“POI导入excel大数据处理”是指利用Apache POI进行大量Excel数据的导入操作,同时它兼容Excel 2003(.xls格式)和2007以上版本(.xlsx格式)的文件。 POI库的主要优点包括: 1. **多格式支持**:不仅...

    Excel2003和excel2007读取方法

    Excel2003和Excel2007虽然版本不同,但它们都提供了强大的数据管理和分析功能。这里我们将详细讨论如何在不同的操作系统和环境中读取这两种版本的Excel文件。 首先,Excel2003是微软发布的第十一版电子表格程序,...

    java 读取 Excel 读取 兼容2003,2007

    以下将详细介绍如何使用Java来读取Excel文件,同时兼容2003和2007版。 1. Apache POI 库 Apache POI 是一个开源项目,它为Microsoft Office格式提供了一个强大的API。对于Excel文件,POI提供了HSSF(Horrible ...

Global site tag (gtag.js) - Google Analytics