`
sshzhangwg
  • 浏览: 69219 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

使用JXL导出和解析EXCEL文件

    博客分类:
  • Java
阅读更多
   前些日子在做一个项目时,经常需要将一些数据导出到EXCEL文件中,而且操作很类似--一个标题,然后是子标题接着是一个列表。于是对其进行抽象,写了另一个类,完成了关键的处理。
package cn.com.tweb.common.excel;

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

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import cn.com.tweb.common.Constant;
import cn.com.tweb.common.StringUtil;
/**
 *  生成EXCEL文件
 * 
 * @author ZHANGWEIGUO
 * @version Revision: 1.0  Date: 2008-10-24  
 * @see 
 * @see
 */
public class ExportExcel {

	/**
	 * 生成EXCEL文件到本地目录,最简的参数列表
	 * 
	 * @param data
	 * @param path
	 * @return
	 */
	public static int exportExcelToFileSystem(List<String[]> data, String path) {
		int result = 1;
        
		int colsNum=0;
		// 先判断是否传入原始数据
		if (data == null || data.size() == 0) {
			result = Constant.EXPORT_EXCEL_NO_DATA;			
		} else {						
			// 根据传入的文件名路径,先生成一个文件
			File exportFile = null;
			try {
				exportFile = new File(path + "/" + "test.xls");
				if (exportFile == null) {
					result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
				}
			} catch (Exception ex) {
				result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
				ex.printStackTrace();
			}

			// 如果能生成文件,就进行生成EXCEL文件的操作
			if (exportFile != null) {
				// 生成EXCEL文件
				WritableWorkbook wwb = null;
				try {
					wwb = Workbook.createWorkbook(exportFile);
					Label lable = new Label(0, 0, "学生信息列表", ExcelProperties.getHeader());				
					// 创建EXCEL工作表
					WritableSheet ws = wwb.createSheet("默认", 0);
					ws.addCell(lable);
					// 取得LIST中的数组大小
					colsNum=data.get(0).length;
					// 参数的含义为:左列,左行,右列,右行 合并成一个单元格
					ws.mergeCells(0, 0, colsNum-1, 0);
					int flag = 0;
					for (String temp[] : data) {
						if (colsNum == temp.length) {
							for (int i = 0; i < temp.length; i++) {
								lable = new Label(i, flag + 1, temp[i],
										ExcelProperties.getNormolCell());
								ws.addCell(lable);

								System.out.println("flag" + flag + " --  "
										+ temp[i]);
							}
							flag++;
						}else{
							result=Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION;
						}
					}
					// 列宽的处理还需要动态的来设置,属性数据从参数总获取
					ws.setColumnView(0, 40);
					ws.setColumnView(1, 35);
					ws.setColumnView(2, 20);
					ws.setColumnView(3, 80);
					ws.setColumnView(4, 30);
					
					// 写EXCEL
					wwb.write();
					// 关闭资源
					wwb.close();
				} catch (IOException e) {
					result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
					e.printStackTrace();
				} catch (RowsExceededException e) {
					result = Constant.EXPORT_EXCEL_SYSTEM_EXCEPTION;
					e.printStackTrace();
				} catch (WriteException e) {
					result = Constant.EXPORT_EXCEL_SYSTEM_EXCEPTION;
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	/**
	 * 生成EXCEL文件到本地目录,带比较全面的参数列表
	 * 
	 * @param title 标题
	 * @param hasInnerTitle 子标题,如果传入NULL,就不生成子标题。
	 * @param colsSize 每列的宽度,如果传入的列数和LIST中的数组长度不一致,将会按默认的宽度处理
	 * @param data EXCEL列表数据源
	 * @param path 文件存放路径
	 * @return
	 */
	public static int exportExcelToFileSystem(String title,
			String innerTitle, int[] colsSize, List<String[]> data,
			String path) {
		int result = 1;
        
		int colsNum=0;
		// 先判断是否传入原始数据
		if (data == null || data.size() == 0) {
			result = Constant.EXPORT_EXCEL_NO_DATA;			
		} else {						
			// 根据传入的文件名路径,先生成一个文件
			File exportFile = null;
			try {
				exportFile = new File(path + "/" + "document.xls");
				if (exportFile == null) {
					result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
				}
			} catch (Exception ex) {
				result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
				ex.printStackTrace();
			}

			// 如果能生成文件,就进行生成EXCEL文件的操作
			if (exportFile != null) {
				// 生成EXCEL文件
				WritableWorkbook wwb = null;
				try {
					wwb = Workbook.createWorkbook(exportFile);
					Label lable = new Label(0, 0, "学生信息列表", ExcelProperties.getHeader());				
					// 创建EXCEL工作表
					WritableSheet ws = wwb.createSheet("默认", 0);
					ws.addCell(lable);
					// 取得LIST中的数组大小
					colsNum=data.get(0).length;
					// 参数的含义为:左列,左行,右列,右行 合并成一个单元格
					ws.mergeCells(0, 0, colsNum-1, 0);
					// 处理内标题
					if(!StringUtil.isNullOrEmpty(innerTitle)){
						lable = new Label(0, 1, innerTitle, ExcelProperties.getHeaderInner());
						ws.addCell(lable);
						ws.mergeCells(0, 1, colsNum-1, 1);
					}
										
					int flag = 0;
					for (String temp[] : data) {
						if (colsNum == temp.length) {
							for (int i = 0; i < temp.length; i++) {
								lable = new Label(i, flag + ExcelProperties.dataRowBeginSize(!StringUtil.isNullOrEmpty(innerTitle)), temp[i],
										ExcelProperties.getNormolCell());
								ws.addCell(lable);
							}
							flag++;
						}else{
							result=Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION;
						}
					}
					
					// 
					if (result != Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION) {
						// 设置列宽
						if (colsSize.length == colsNum) {
							for (int i = 0; i < colsSize.length; i++) {
								ws.setColumnView(i, colsSize[i]);
							}
						} else {
							// 设置默认的宽度
							for (int i = 0; i < colsNum; i++) {
								ws.setColumnView(i, 20);
							}
							result = Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION;
						}
					}				
					// 写EXCEL
					wwb.write();
					// 关闭资源
					wwb.close();
				} catch (IOException e) {
					result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
					e.printStackTrace();
				} catch (RowsExceededException e) {
					result = Constant.EXPORT_EXCEL_SYSTEM_EXCEPTION;
					e.printStackTrace();
				} catch (WriteException e) {
					result = Constant.EXPORT_EXCEL_SYSTEM_EXCEPTION;
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	/**
	 * 生成EXCEL通过浏览器导出到客户端,带比较全面的参数列表,在WEB项目中使用
	 * 
	 * @param title 标题
	 * @param hasInnerTitle 子标题,如果传入NULL,就不生成子标题。
	 * @param colsSize 每列的宽度,如果传入的列数和LIST中的数组长度不一致,将会按默认的宽度处理
	 * @param data EXCEL列表数据源
	 * @param path 文件存放路径
	 * @return
	 */
	public static int exportExcelInWeb(String title,
			String innerTitle, int[] colsSize, List<String[]> data,
			OutputStream os) {
		int result = 1;
        
		int colsNum=0;
		// 先判断是否传入原始数据
		if (data == null || data.size() == 0) {
			result = Constant.EXPORT_EXCEL_NO_DATA;			
		} else {						
			// 判断传入的流			
			if (os != null) {
				// 生成EXCEL文件
				WritableWorkbook wwb = null;
				try {
					wwb = Workbook.createWorkbook(os);
					Label lable = new Label(0, 0, "学生信息列表", ExcelProperties.getHeader());				
					// 创建EXCEL工作表
					WritableSheet ws = wwb.createSheet("默认", 0);
					ws.addCell(lable);
					// 取得LIST中的数组大小
					colsNum=data.get(0).length;
					// 参数的含义为:左列,左行,右列,右行 合并成一个单元格
					ws.mergeCells(0, 0, colsNum-1, 0);
					// 处理内标题
					if(!StringUtil.isNullOrEmpty(innerTitle)){
						lable = new Label(0, 1, innerTitle, ExcelProperties.getHeaderInner());
						ws.addCell(lable);
						ws.mergeCells(0, 1, colsNum-1, 1);
					}
										
					int flag = 0;
					for (String temp[] : data) {
						if (colsNum == temp.length) {
							for (int i = 0; i < temp.length; i++) {
								lable = new Label(i, flag + ExcelProperties.dataRowBeginSize(!StringUtil.isNullOrEmpty(innerTitle)), temp[i],
										ExcelProperties.getNormolCell());
								ws.addCell(lable);
							}
							flag++;
						}else{
							result=Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION;
						}
					}
					
					// 
					if (result != Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION) {
						// 设置列宽
						if (colsSize.length == colsNum) {
							for (int i = 0; i < colsSize.length; i++) {
								ws.setColumnView(i, colsSize[i]);
							}
						} else {
							// 设置默认的宽度
							for (int i = 0; i < colsNum; i++) {
								ws.setColumnView(i, 20);
							}
							result = Constant.EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION;
						}
					}				
					
					wwb.write();
					wwb.close();
					os.close();
				} catch (IOException e) {
					result = Constant.EXPORT_EXCEL_NOFILE_EXCEPTION;
					e.printStackTrace();
				} catch (RowsExceededException e) {
					result = Constant.EXPORT_EXCEL_SYSTEM_EXCEPTION;
					e.printStackTrace();
				} catch (WriteException e) {
					result = Constant.EXPORT_EXCEL_SYSTEM_EXCEPTION;
					e.printStackTrace();
				}
			}else{
				result = Constant.EXPORT_EXCEL_NULL_OUTPUTSTREAM_EXCEPTION;
			}
		}
		return result;
	}
}


使用模拟数据的ArrayList对象,在main()方法中进行测试可以生成下面的EXCE文件,代码如下:
package test;

import java.util.ArrayList;
import java.util.List;

public class DataMock {

	/**
	 * 模拟数据
	 * 
	 * @return
	 */
	public static List<String[]> getListData() {
		List<String[]> list = new ArrayList<String[]>();
		String[] temp=null;
		temp=new String[5];
		temp[0]="姓名";
		temp[1]="年龄";
		temp[2]="性别";
		temp[3]="身高";
		temp[4]="爱好";
		for (int i = 0; i < 50; i++) {
			temp=new String[5];
			temp[0]="学生"+i;
			temp[1]="12";
			temp[2]=i%2==0?"男":"女";
			temp[3]="1.60"+(i/10*3);
			temp[4]="篮球,乒乓球";
			list.add(temp);
		}
		return list;
	}

}


调用的代码:
int result=ExportExcel.exportExcelToFileSystem("学生信息管理","(TWEB集团附属小学)",new int[]{24,24,18,16,20},DataMock.getListData(), "d:\\");
		System.out.println("result:"+result);

运行以后就可以在D盘生成一个EXCEL。

在C/S项目中,通过输出流直接向客户端输出,JSP代码里或Web框架的Action里调用代码为:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.OutputStream"%>
<%@page import="cn.com.tweb.common.excel.ExportExcel"%>
<%@page import="test.DataMock"%>
<%@page import="cn.com.tweb.common.StringUtil"%>
<%  

  // 设定输出文件头
  response.setCharacterEncoding("GBK");
  // 当文件名为中文时请用StringUtil类的toUtf8String()方法进行转码
  response.setHeader("Content-disposition",
			"attachment;  filename="+StringUtil.toUtf8String("学生信息管理列表.xls"));
  // 定义输出类型
  response.setContentType("application/vnd.ms-excel");               
  OutputStream os = response.getOutputStream();
      
  int result=ExportExcel.exportExcelInWeb("学生信息管理","(拓旗集团附属小学)",
            new int[]{24,24,18,16,20},DataMock.getListData(),os);
  System.out.println("-------------(result:"+result+")-------------");
%>


常量接口类的代码为:
package cn.com.tweb.common;
/**
 * 常量接口,以EXPORT_EXCEL_开头的变量,值大于0的都是导出EXCEL成功
 * 
 * @author ZHANGWEIGUO
 * @version Revision: 1001  Date: 2006-09-03 05:00:21 +0800 
 * @see 
 * @see
 */
public interface Constant {
	// 成功导出EXCEL文件
	public static final int EXPORT_EXCEL_SUCCESS=1;
	
	// 没有数据
	public static final int EXPORT_EXCEL_NO_DATA=-1;

	// 系统异常
	public static final int EXPORT_EXCEL_SYSTEM_EXCEPTION=-2;
	
	// 其他异常
	public static final int EXPORT_EXCEL_OTHER_EXCEPTION=-3;
	
	// 文件路径和文件名无效
	public static final int EXPORT_EXCEL_NOFILE_EXCEPTION=-4;
	
	// 集合对象中的数组存放的数据个数不一致
	public static final int EXPORT_EXCEL_ARRAYNOTHESAME_EXCEPTION=-5;
	
	// 传入的OUTPUTSTREAM为空
	public static final int EXPORT_EXCEL_NULL_OUTPUTSTREAM_EXCEPTION=-6;
	
	// 宽度设置参数有误,按默认值设定宽度
	public static final int EXPORT_EXCEL_COLSNUM_NOTRIGHT=2;
	
}

解析EXCEL文件的代码如下:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.fileupload.FileItem;

public class ParseExcel {
	
	/**
	 *  用COMMON UPLOAD进行EXCEL文件上传,得到fileItem对象,这里
	 * 进行解析,返回集合对象。该方法适合在JAVA工程中使用。
	 * 
	 * @param fileItem
	 * @param beginIndex 正式数据的起始行 例如EXCEL文件
	 *  有大标题和小标题和列标题,那么该参数应为 4
	 * @return
	 * @throws BiffException
	 * @throws IOException
	 */
	public static List<String[]> redExcel(FileItem fileItem,int beginIndex){
		// 保存结果集
		List<String[]> result=null;
		// 保存EXCEL每行的所有单元格中的数据
		String[] temp=null;
		try {
			if (fileItem != null) {
				Workbook workBook = Workbook.getWorkbook(fileItem
						.getInputStream());
				Sheet sheet = workBook.getSheet(0);
				Cell cell = null;
				int rowSize = sheet.getRows();
				int colSize = sheet.getColumns();

				result = new ArrayList<String[]>();
				for (int i = beginIndex - 1; i < rowSize; i++) {
					temp = new String[colSize];
					for (int t = 0; t < colSize; t++) {						
						// 保存EXCEL每行的所有单元格中的数据,在内循环外面进行定义
						cell = sheet.getCell(t, i);
						String content = "";
						if (cell.getContents() != null) {
							// 去空格,特殊字符和回车键盘
							content = cell.getContents().replace("%", "")
									.replace("|", "").replace(" ", "")
									.replaceAll("\\n", "")
									.replaceAll("\\r", "").trim();

						}
						temp[t] = content;
					}
					// 将每列的的数据存入结果集中
					result.add(temp);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
	/**
	 *  用COMMON UPLOAD进行EXCEL文件上传,得到fileItem对象,这里
	 * 进行解析,返回集合对象。该方法适合在WEB项目中使用。
	 * 
	 * @param fileItem
	 * @param beginIndex 正式数据的起始行 例如EXCEL文件
	 *  有大标题和小标题和列标题,那么该参数应为 4
	 * @return
	 * @throws BiffException
	 * @throws IOException
	 */
	public static List<String[]> redExcel(File file,int beginIndex){
		// 保存结果集
		List<String[]> result=null;
		// 保存EXCEL每行的所有单元格中的数据
		String[] temp=null;
		try {
			Workbook workBook = Workbook.getWorkbook(file);
			Sheet sheet = workBook.getSheet(0);
			Cell cell = null;
			int rowSize = sheet.getRows();
			int colSize = sheet.getColumns();
			
			result=new ArrayList<String[]>();
			for (int i = beginIndex-1; i < rowSize; i++) {
				// 保存EXCEL每行的所有单元格中的数据
				temp=new String[colSize];
				for (int t = 0; t < colSize; t++) {				    
					cell = sheet.getCell(t, i);
					String content="";
					if (cell.getContents()!=null) {
						// 去空格,特殊字符和回车键盘
						content = cell.getContents().replace("%", "")
								.replace("|", "")
								.replace(" ", "")
								.replaceAll("\\n", "")
								.replaceAll("\\r", "").trim();
						
					}
					temp[t]=content;					
				}
				// 将每列的的数据存入结果集中
				result.add(temp);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}	
}
分享到:
评论
5 楼 贝塔ZQ 2016-09-14  
导出和解析excel可以用PageOffice插件试试看,挺简单的
4 楼 sshzhangwg 2015-08-30  
ExcelProperties 是jexcel 1.0 时的类。
3 楼 zqb666kkk 2012-07-13  
写的挺好 但是上面几个疑问 不清楚的话 没法用
2 楼 zqb666kkk 2012-07-13  
   public static int exportExcelInWeb(String title,  
.            String innerTitle, int[] colsSize, List<String[]> data,  
.            OutputStream os) {  
这个方法里 的

OutputStream os
这个参数怎么传过来 的 能不能完整的例子整出来看看 怎么实现客户端通过浏览器  把excel文件放到 自定义的目录
1 楼 zqb666kkk 2012-07-13  
ExcelProperties是什么类

相关推荐

    JXL解析和生成Excel文件

    NULL 博文链接:https://wankunde.iteye.com/blog/869907

    jxl方式生成excel表格.zip

    资源包含:(1)一个txt转为excel Demo(2)将解析出的数据写入excel表格里所需要架包jxl。

    JXL操作EXCEL的各个类的解析

    Excel的生成方式其实很简单,主要包括几个大的类,首先最重要的几个类就是WritableWorkbook,这个类代表的就是一个Excel文档,使用过Excel文档的都应该知道Excel文档的组成,都是由一个一个的sheet组成的,那么这个...

    自定义Excel的上传下载(解析Excel文件)

    实现Excel的上传和下载 存在两种格式的上传和下载,分别对应POI和JXL 可以实现根据Excel模板导出,自定义Excel样式导入 形成自定义的Excel报表等功能。项目为Maven项目,JDK采用1.8

    JAVA 解析 Excel 工具 Java 解析、生成 Excel 比较有名的框架有 Apache poi、jxl.zip

    JAVA 解析 Excel 工具 Java 解析、生成 Excel 比较有名的框架有 Apache poi、jxl.zip

    JAVA 解析 Excel 工具 Java 解析、生成 Excel 比较有名的框架.rar

    JAVA解析Excel工具EasyExcel Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存&#xff0c;poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题&#xff0c;但...

    jxl.jar及api 解析excel很好的工具

    ● 支持Excel 95-2000的所有版本 ● 生成Excel 2000标准格式 ● 支持字体、数字、日期操作 ● 能够修饰单元格属性 ● 支持图像和图表

    Android 自动化生成多语言strings文件

    基于jxl.jar解析Excel xls文件自动化生成android多语言strings.xml

    Java处理100万行超大Excel文件秒级响应

    由于项目需要对大量Excel数据进行输入输出处理,在使用JXL,POI后发现很容易出现OOM,最后在网上找到阿里的开源项目EasyExcel能很快速的读取写入超大Excel文件。经过大量的调试优化,现通过JAVA生成104万行20列的...

    Excel导入导及动态验证

    使用jxl实现poi简单的功能,通过反射原理动态验证内容及导入导出excel文件

    EasyExcel JAVA解析Excel工具 v3.3.4

    Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及...

    JXL教程详解

    JXL生成EXCEL的各个类的解析: Excel的生成方式其实很简单,主要包括几个大的类 首先最重要的几个类就是WritableWorkbook,这个类代表的就是一个Excel文档,使用过Excel文档的都应该知道Excel文档的组成,都是由一个...

    EasyExcel JAVA解析Excel工具.rar

    Java解析、生成Excel比较有名的框架有Apache poi、jxl,但他们都存在一个严重的问题就是非常的耗内存。 EasyExcel 重写了poi,使一个3M的excel只需要几M内存,并且再大的excel不会出现内存溢出。 64M内存1分钟内...

    jxl架包下载

    jxl架包下载, 用户解析excel,导入数据库,从数据库导出excel.

    excel-txt文件解析生成组件

    将 内容 导入到txt 以及 excel中 ,进行抽象封装,对外提供简单的api ,并且将 excel文件映射到list对象中去 ,不需要自己进行解析,实现通用化,这里使用了 annoation 感觉 挺好! 可以下载看看 ,写的不好的可以 跟我 交流...

    jxl相关jar和api

    java用于解析生成Excel的工具的jar包和api~

    根据jxl,对html中的table进行导出excel

    工具类中有两个方法,一个是根据前台传入的table的html代码进行导出excel(可以只自定义表格名称/sheet页名称)。还一种是根据html文件,代码自动解析出table然后进行excel导出

    java解析excel信息

    最近工作中遇到需要用java解析excel,并生成表格,在网上找了些资料,写了些小例子,供参考

    Chat2DB 智能数据库客户端,数据报表工具,自然语言生成SQL,生成报表

    JAVA解析Excel工具;Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如...

Global site tag (gtag.js) - Google Analytics