java Excel 导出

阅读更多

 

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 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 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 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;
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


java Excel 导出_第1张图片java Excel 导出_第2张图片java Excel 导出_第3张图片
 
 
 谢谢您的赞助,我会做的更好!

 

 

 

你可能感兴趣的:(java,excel,导出)