poi excel

package com.hd.oper;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.PushbackInputStream;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import org.apache.poi.POIXMLDocument;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

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.openxml4j.exceptions.InvalidFormatException;

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.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import org.apache.poi.xssf.usermodel.XSSFFont;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Exceloper {

public static void main(String[] args) {

String path2003 = "d:\\Desktop\\夏.xls ";// Excel2003文件路径

String path2007 = "d:\\Desktop\\夏.xls ";// Excel2007文件路径

path2003 = System.getProperties().getProperty("user.dir")

+ File.separator + "Excel" + File.separator + "user_2003.xls";

path2007 = System.getProperties().getProperty("user.dir")

+ File.separator + "Excel" + File.separator + "user_2007.xlsx";

long start2003 = System.currentTimeMillis();

System.out.println("Excel文件路径:" + path2003);

List<User> list2003 = parseExcel(path2003);

System.out.println("解析2003输出总人数:" + list2003.size());

long end2003 = System.currentTimeMillis();

System.out.println("解析Excel2003完毕!共用时" + (end2003 - start2003) + "毫秒!");

long start2007 = System.currentTimeMillis();

System.out.println("Excel文件路径:" + path2007);

List<User> list2007 = parseExcel(path2007);

System.out.println("解析2007输出总人数:" + list2007.size());

long end2007 = System.currentTimeMillis();

System.out.println("解析Excel完毕!共用时" + (end2007 - start2007) + "毫秒!");

System.out.println("》》》》》》》》》》开始执行Excel文件导出:");

buildXSLExcel();// 导出Excel 2003

buildXSLXExcel();// 导出Excel 2007

}

/**

* 根据路径加载解析Excel

*

* @param path

* @return

*/

public static List<User> parseExcel(String path) {

List<User> list = new ArrayList<User>();

File file = null;

InputStream input = null;

Workbook workBook = null;

Sheet sheet = null;

if (path != null && path.length() > 7) {

// 判断文件是否是Excel(2003、2007)

String suffix = path

.substring(path.lastIndexOf("."), path.length());

if (".xls".equals(suffix) || ".xlsx".equals(suffix)) {// 2003后缀或2007后缀

file = new File(path);

try {

input = new FileInputStream(file);

} catch (FileNotFoundException e) {

System.out.println("未找到指定的文件!");

e.printStackTrace();

} catch (Exception e) {

System.out.println("读取Excel文件发生异常!");

e.printStackTrace();

}

if (!input.markSupported()) {

input = new PushbackInputStream(input, 8);

}

try {

if (POIFSFileSystem.hasPOIFSHeader(input)

|| POIXMLDocument.hasOOXMLHeader(input)) {

workBook = WorkbookFactory.create(input);

} else {

System.out.println("非法的输入流:当前输入流非OLE2流或OOXML流!");

}

} catch (IOException e) {

System.out.println("创建表格工作簿对象发生IO异常!原因:" + e.getMessage());

e.printStackTrace();

} catch (InvalidFormatException e) {

// Your InputStream was neither an OLE2 stream, nor an OOXML

// stream.

System.out.println("非法的输入流:当前输入流非OLE2流或OOXML流!");

e.printStackTrace();

}

try {

if (workBook != null) {

int numberSheet = workBook.getNumberOfSheets();

if (numberSheet > 0) {

sheet = workBook.getSheetAt(0);// 获取第一个工作簿(Sheet)的内容【注意根据实际需要进行修改】

list = getExcelContent(sheet);

} else {

System.out.println("目标表格工作簿(Sheet)数目为0!");

}

}

input.close();

} catch (IOException e) {

System.out.println("关闭输入流异常!" + e.getMessage());

e.printStackTrace();

}

} else {

System.out.println("非法的Excel文件后缀!");

}

} else {

System.out.println("非法的文件路径!");

}

return list;

}

/**

* 解析(读取)Excel内容

*

* @param sheet

* @return

*/

@SuppressWarnings("static-access")

public static List<User> getExcelContent(Sheet sheet) {

List<User> list = new ArrayList<User>();

int rowCount = sheet.getPhysicalNumberOfRows();// 总行数

if (rowCount > 1) {

Row titleRow = sheet.getRow(0);// 标题行

for (int i = 1; i < rowCount; i++) {// 遍历行,略过标题行,从第二行开始

Row row = sheet.getRow(i);

User entity = new User();

for (int j = 0; j < 3; j++) {

Cell cell = row.getCell(j);

if (titleRow.getCell(j).getStringCellValue().indexOf("姓名") >= 0) {

if (cell != null

&& cell.getCellType() == cell.CELL_TYPE_STRING) {

entity.setName(cell.getStringCellValue().trim());

}

}

if (titleRow.getCell(j).getStringCellValue().indexOf("性别") >= 0) {

if (cell != null

&& cell.getCellType() == cell.CELL_TYPE_STRING) {

entity.setSex(cell.getStringCellValue().trim());

}

}

if (titleRow.getCell(j).getStringCellValue().indexOf("年龄") >= 0) {

if (cell != null

&& cell.getCellType() == cell.CELL_TYPE_NUMERIC) {

entity.setAge((int) cell.getNumericCellValue());

} else if (cell != null

&& cell.getCellType() == cell.CELL_TYPE_STRING) {

entity.setAge(Integer.parseInt(cell

.getStringCellValue().trim()));

}

}

}

list.add(entity);

}

}

return list;

}

/**

* 生成2003 Excel

*/

public static void buildXSLExcel() {

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");

String now = format.format(new Date());

String basePath = System.getProperties().getProperty("user.dir")

+ File.separator + "Excel" + File.separator;

String exportFileName = "人员信息导出2003_" + now + ".xls";// 导出文件名

List<User> list = parseExcel(basePath + "user_2003.xls");

HSSFWorkbook workBook = null;

String[] cellTitle = { "序号", "姓名", "性别", "年龄" };

try {

workBook = new HSSFWorkbook();// 创建工作薄

HSSFSheet sheet = workBook.createSheet();

workBook.setSheetName(0, "人员信息");// 工作簿名称

HSSFFont font = workBook.createFont();

font.setColor(HSSFFont.COLOR_NORMAL);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

HSSFCellStyle cellStyle = workBook.createCellStyle();// 创建格式

cellStyle.setFont(font);

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 创建第一行标题

HSSFRow titleRow = sheet.createRow((short) 0);// 第一行标题

for (int i = 0, size = cellTitle.length; i < size; i++) {// 创建第1行标题单元格

switch (i) {

case 0:

sheet.setColumnWidth(0, 3000);

break;

case 1:

sheet.setColumnWidth(1, 4000);

break;

case 2:

sheet.setColumnWidth(2, 4000);

break;

case 3:

sheet.setColumnWidth(3, 2000);

break;

}

HSSFCell cell = titleRow.createCell(i, 0);

cell.setCellStyle(cellStyle);

cell.setCellValue(cellTitle[i]);

}

// 从第二行开始写入数据

// 注:此处如果数据过多,会抛出java.lang.IllegalStateException异常:The maximum

// number of cell styles was exceeded.

// You can define up to 4000 styles in a .xls workbook。这是是由于cell

// styles太多create造成,故一般可以把cellstyle设置放到循环外面

if (list != null && !list.isEmpty()) {

HSSFCellStyle style = workBook.createCellStyle();// 创建格式

for (int i = 0, size = list.size(); i < size; i++) {

User entity = list.get(i);

HSSFRow row = sheet.createRow((short) i + 1);

for (int j = 0, length = cellTitle.length; j < length; j++) {

HSSFCell cell = row.createCell(j, 0);// 在上面行索引0的位置创建单元格

cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型

switch (j) {// 在单元格中输入一些内容

case 0:// 序号

cell.setCellValue(i + 1);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

break;

case 1:// 姓名

cell.setCellValue(entity.getName());

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

break;

case 2:// 性别

cell.setCellValue(String.valueOf(entity.getSex()));

style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

cell.setCellStyle(style);

break;

case 3:// 年龄

cell.setCellValue(entity.getAge());

cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

cell.setCellStyle(style);

break;

}

}

}

}

// 通过文件输出流生成Excel文件

File file = new File(basePath + exportFileName);

FileOutputStream outStream = new FileOutputStream(file);

workBook.write(outStream);

outStream.flush();

outStream.close();

System.out.println("Excel 2003文件导出完成!导出文件路径:" + file.getPath());

/***

* Web形式输出Excel

*

*/

/**

* // 表示以附件的形式把文件发送到客户端 response.setHeader("Content-Disposition",

* "attachment;filename=" + new String((exportFileName).getBytes(),

* "ISO-8859-1"));//设定输出文件头 response.setContentType(

* "application/vnd.ms-excel;charset=UTF-8");// 定义输出类型 //

* 通过response的输出流把工作薄的流发送浏览器形成文件 OutputStream outStream =

* response.getOutputStream(); workBook.write(outStream);

* outStream.flush();

*/

} catch (IOException e) {

System.out.println("生成人员信息Excel发生IO 异常!" + e.getMessage());

e.printStackTrace();

} catch (Exception e) {

System.out.println("文件导出发生异常!异常原因:" + e.getMessage());

e.printStackTrace();

}

}

/**

* 生成2007 Excel

*/

public static void buildXSLXExcel() {

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");

String now = format.format(new Date());

String basePath = System.getProperties().getProperty("user.dir")

+ File.separator + "Excel" + File.separator;

String exportFileName = "人员信息导出2007_" + now + ".xlsx";// 导出文件名

List<User> list = parseExcel(basePath + "user_2003.xls");

XSSFWorkbook workBook = null;

String[] cellTitle = { "序号", "姓名", "性别", "年龄" };

try {

workBook = new XSSFWorkbook();// 创建工作薄

XSSFSheet sheet = workBook.createSheet();

workBook.setSheetName(0, "人员信息");// 工作簿名称

XSSFFont font = workBook.createFont();

font.setColor(XSSFFont.COLOR_NORMAL);

font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);

XSSFCellStyle cellStyle = workBook.createCellStyle();// 创建格式

cellStyle.setFont(font);

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 创建第一行标题

XSSFRow titleRow = sheet.createRow((short) 0);// 第一行标题

for (int i = 0, size = cellTitle.length; i < size; i++) {// 创建第1行标题单元格

switch (i) {

case 0:

sheet.setColumnWidth(0, 3000);

break;

case 1:

sheet.setColumnWidth(1, 4000);

break;

case 2:

sheet.setColumnWidth(2, 4000);

break;

case 3:

sheet.setColumnWidth(3, 2000);

break;

}

XSSFCell cell = titleRow.createCell(i, 0);

cell.setCellStyle(cellStyle);

cell.setCellType(XSSFCell.CELL_TYPE_STRING);

cell.setCellValue(cellTitle[i]);

}

// 从第二行开始写入数据

// 注:此处如果数据过多,会抛出java.lang.IllegalStateException异常:The maximum

// number of cell styles was exceeded.

// You can define up to 4000 styles in a .xls workbook。这是是由于cell

// styles太多create造成,故一般可以把cellstyle设置放到循环外面

if (list != null && !list.isEmpty()) {

XSSFCellStyle style = workBook.createCellStyle();// 创建格式

for (int i = 0, size = list.size(); i < size; i++) {

User entity = list.get(i);

XSSFRow row = sheet.createRow((short) i + 1);

for (int j = 0, length = cellTitle.length; j < length; j++) {

XSSFCell cell = row.createCell(j, 0);// 在上面行索引0的位置创建单元格

cell.setCellType(XSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型

switch (j) {// 在单元格中输入一些内容

case 0:// 序号

cell.setCellValue(i + 1);

style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

break;

case 1:// 姓名

cell.setCellValue(entity.getName());

style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

break;

case 2:// 性别

cell.setCellValue(String.valueOf(entity.getSex()));

style.setAlignment(XSSFCellStyle.ALIGN_LEFT);

cell.setCellStyle(style);

break;

case 3:// 年龄

cell.setCellValue(entity.getAge());

cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);

style.setAlignment(XSSFCellStyle.ALIGN_LEFT);

cell.setCellStyle(style);

break;

}

}

}

}

// 通过文件输出流生成Excel文件

File file = new File(basePath + exportFileName);

FileOutputStream outStream = new FileOutputStream(file);

workBook.write(outStream);

outStream.flush();

outStream.close();

System.out.println("Excel 2007文件导出完成!导出文件路径:" + file.getPath());

/***

* Web形式输出Excel

*

*/

/**

* // 表示以附件的形式把文件发送到客户端 response.setHeader("Content-Disposition",

* "attachment;filename=" + new String((exportFileName).getBytes(),

* "ISO-8859-1"));//设定输出文件头 response.setContentType(

* "application/vnd.ms-excel;charset=UTF-8");// 定义输出类型 //

* 通过response的输出流把工作薄的流发送浏览器形成文件 OutputStream outStream =

* response.getOutputStream(); workBook.write(outStream);

* outStream.flush();

*/

} catch (IOException e) {

System.out.println("生成人员信息Excel发生IO 异常!" + e.getMessage());

e.printStackTrace();

} catch (Exception e) {

System.out.println("文件导出发生异常!异常原因:" + e.getMessage());

e.printStackTrace();

}

}

}

class User {

private String name;

private String sex;

private int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

时间: 2024-11-03 02:44:34

poi excel的相关文章

poi excel批量导入 解决各类型的数据

String value=this.getValue((HSSFCell) row.getCell(position)) 数字类型  时间类型 字符串类型验证没问题, 数字类型如果是1234.0会将其变成1234  如果是1234.5保存仍然保持小数位1234.5,用来解决电话号码和普通数字问题 时间类型将其格式化成了 2015-12-09 12:13:12这种格式便于保存数据库 //解决类型问题,获得数值 public String getValue(HSSFCell cell) { Stri

poi excel自动转换成javabean 支持引用类型属性二级转换

最近项目需要使用excel导入功能,导入学生的时候需要指定所在班级,使用excel一次性导入! 将以前的代码改改支持属性内引用类的转换. 测试对象为User对象,javabean结构: private String username; private int id; private String name; private Date birthday; private long height; private double salary; private User user; 使用测试代码: M

poi excel导出,下载

poi.jar包 public void downExcel(HttpServletResponse response,Page<ShopApply> page) throws Exception{ // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet(&quo

poi excel导入

poi.jar包 import java.io.File;import java.io.FileInputStream;import java.io.IOException; 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.hs

POI Excel 合并数据相同的行

import java.io.Serializable; /** * POI Excel报表导出,列合并实体<br> * * @author WQ * */ public class PoiModel implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String content; private String oldContent; private int ro

POI/Excel/HTML单元格公式问题

一.问题描述 使用MyBatis从数据库中获取数据,然后用POI把数据填充到Excel模板中,生成最终的xls文件.把最终的xls文件转换为html文件,并返回给前台显示在Panel中. Excel模板中,除了数据点位符外,还有一些计算公式.由于这些计算公式引用的数据在模板中是点位符,所以计算单元显示为"#VALUE!".见下图: 生成Excel文件,在添加了重算的相关代码(见下文)后,计算单元格的值能够正常显示.转换为html后,这些计算单元格不会重新计算,仍然显示为"#V

poi excel文件的导入

使用poi来实现excel文件的导入导出.使用struts2来做处理. 首先看jsp页面: index.jsp: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC

POI Excel导入导出

一.导出到Excel 基本思路:点击导出后生成临时.xls文件,返回文件名,供用户下载,下载完后删除文件 带查询的导出(前端EasyUI),如下为导出界面图 下面为导出按钮绑定的函数: var exportCondition={};//导出条件//导出功能 function outputData(){ $.ajax({ type: "POST", url: path+"/main/inputAndOutput/output", data: exportConditi

java poi excel 导入导出数据

背景:1.pringmvc 框架下 的excel 导入导出   2.OI 操作office. 页面代码: <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true"> <div class=&quo