java Excel 导出_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java Excel 导出

java Excel 导出

 2015/5/7 21:37:55  knight_black_bob  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.web.utils;importjava.io.File;importjava.util.List;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importjxl.Workbook;importjxl.format.UnderlineStyle;importjxl.write.Label;importjxl.write.WritableCellFormat
  • 标签:

?

class="java">package com.web.utils;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class XLSUtils {
	private static Log logger = LogFactory.getLog(XLSUtils.class);

	
	
	public static File createXLSFile(String title, String[] columnName, List<String[]> content) 
	{
		File tmpFile = null;
		try 
		{
			tmpFile = File.createTempFile(title, ".xls");
			WritableWorkbook book = Workbook.createWorkbook(tmpFile);
			// 创建工作表
			WritableSheet sheet = book.createSheet("Sheet1", 0);
			// 创建标题
			if(columnName != null) 
			{
				WritableFont wf_merge = new WritableFont(WritableFont.ARIAL,20,WritableFont.NO_BOLD,
	                    false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
				WritableCellFormat wff_merge = new WritableCellFormat(wf_merge);
				// 水平对齐
				wff_merge.setAlignment(jxl.format.Alignment.CENTRE);   
				// 垂直对齐
				wff_merge.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
				// 创建表头
				for(int i = 0; i < columnName.length; i ++) 
				{
					sheet.addCell(new Label(i, 0, columnName[i]));
				}
			}
			// 创建内容
			if(content != null) 
			{
				for(int i = 0; i < content.size(); i ++) 
				{
					String s[] = content.get(i);
					for(int j = 0; j < s.length; j ++) 
					{
						sheet.addCell(new Label(j, i + 1, s[j]));
					}
				}
			}
			book.write();
			book.close();
		} 
		catch (Exception e)
		{	
			logger.info("创建xls异常? create xls exeception", e);
			return null;
		}	
		return tmpFile;
	}
	
}

?

?

?

?

?

?

?

package com.web.utils;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class XLSUtils {
	private static Log logger = LogFactory.getLog(XLSUtils.class);

	
	
	public static File createXLSFile(String title, String[] columnName, List<String[]> content) 
	{
		File tmpFile = null;
		try 
		{
			tmpFile = File.createTempFile(title, ".xls");
			WritableWorkbook book = Workbook.createWorkbook(tmpFile);
			// 创建工作表
			WritableSheet sheet = book.createSheet("Sheet1", 0);
			// 创建标题
			if(columnName != null) 
			{
				WritableFont wf_merge = new WritableFont(WritableFont.ARIAL,20,WritableFont.NO_BOLD,
	                    false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
				WritableCellFormat wff_merge = new WritableCellFormat(wf_merge);
				// 水平对齐
				wff_merge.setAlignment(jxl.format.Alignment.CENTRE);   
				// 垂直对齐
				wff_merge.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
				// 创建表头
				for(int i = 0; i < columnName.length; i ++) 
				{
					sheet.addCell(new Label(i, 0, columnName[i]));
				}
			}
			// 创建内容
			if(content != null) 
			{
				for(int i = 0; i < content.size(); i ++) 
				{
					String s[] = content.get(i);
					for(int j = 0; j < s.length; j ++) 
					{
						sheet.addCell(new Label(j, i + 1, s[j]));
					}
				}
			}
			book.write();
			book.close();
		} 
		catch (Exception e)
		{	
			logger.info("创建xls异常? create xls exeception", e);
			return null;
		}	
		return tmpFile;
	}
	
}

?

?

?

?

package com.web.utils;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 

public class DownLoadExcel {
	public static String downLoadExcel(String title, String[] columnName, List<String[]> content, HttpServletRequest request, HttpServletResponse response) 
	{
		try{
			File file = XLSUtils.createXLSFile(title, columnName, content);
			if(file == null) 
			{
				return null;
			}
			FileServerUtil.downFile(file, request, response);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}
}

?

?

?

?

?

?

?

?

?

?

?

  • 相关文章
发表评论
用户名: 匿名