当前位置: 移动技术网 > IT编程>开发语言>Java > java导出Excel通用方法实例

java导出Excel通用方法实例

2019年07月19日  | 移动技术网IT编程  | 我要评论

数据导出到excel几乎是所有客户都会提出的一个需求。下面我就分享一下我的代码。

首先需要引入的jar包:

然后就是正式代码了。

package lcy._41_50;
 
import java.io.fileoutputstream;
import java.io.outputstream;
import java.net.urlencoder;
 
import javax.servlet.http.httpservletresponse;
 
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.hssf.util.cellrangeaddress;
import org.apache.poi.hssf.util.hssfcolor;
 
@suppresswarnings( { "deprecation" })
public class test46 {
 
	public static void main(string[] args) throws exception {
 
		string sheetname = "用车统计表单";
		string titlename = "用车申请数据统计表";
		string filename = "用车申请统计表单";
		int columnnumber = 3;
		int[] columnwidth = { 10, 20, 30 };
		string[][] datalist = { { "001", "2015-01-01", "it" },
				{ "002", "2015-01-02", "市场部" }, { "003", "2015-01-03", "测试" } };
		string[] columnname = { "单号", "申请时间", "申请部门" };
		new test46().exportnoresponse(sheetname, titlename, filename,
				columnnumber, columnwidth, columnname, datalist);
	}
 
	public void exportwithresponse(string sheetname, string titlename,
			string filename, int columnnumber, int[] columnwidth,
			string[] columnname, string[][] datalist,
			httpservletresponse response) throws exception {
		if (columnnumber == columnwidth.length&& columnwidth.length == columnname.length) {
			// 第一步,创建一个webbook,对应一个excel文件
			hssfworkbook wb = new hssfworkbook();
			// 第二步,在webbook中添加一个sheet,对应excel文件中的sheet
			hssfsheet sheet = wb.createsheet(sheetname);
			// sheet.setdefaultcolumnwidth(15); //统一设置列宽
			for (int i = 0; i < columnnumber; i++) 
			{
				for (int j = 0; j <= i; j++) 
				{
					if (i == j) 
					{
						sheet.setcolumnwidth(i, columnwidth[j] * 256); // 单独设置每列的宽
					}
				}
			}
			// 创建第0行 也就是标题
			hssfrow row1 = sheet.createrow((int) 0);
			row1.setheightinpoints(50);// 设备标题的高度
			// 第三步创建标题的单元格样式style2以及字体样式headerfont1
			hssfcellstyle style2 = wb.createcellstyle();
			style2.setalignment(hssfcellstyle.align_center);
			style2.setverticalalignment(hssfcellstyle.vertical_center);
			style2.setfillforegroundcolor(hssfcolor.light_turquoise.index);
			style2.setfillpattern(hssfcellstyle.solid_foreground);
			hssffont headerfont1 = (hssffont) wb.createfont(); // 创建字体样式
			headerfont1.setboldweight(hssffont.boldweight_bold); // 字体加粗
			headerfont1.setfontname("黑体"); // 设置字体类型
			headerfont1.setfontheightinpoints((short) 15); // 设置字体大小
			style2.setfont(headerfont1); // 为标题样式设置字体样式
 
			hssfcell cell1 = row1.createcell(0);// 创建标题第一列
			sheet.addmergedregion(new cellrangeaddress(0, 0, 0,
					columnnumber - 1)); // 合并列标题
			cell1.setcellvalue(titlename); // 设置值标题
			cell1.setcellstyle(style2); // 设置标题样式
 
			// 创建第1行 也就是表头
			hssfrow row = sheet.createrow((int) 1);
			row.setheightinpoints(37);// 设置表头高度
 
			// 第四步,创建表头单元格样式 以及表头的字体样式
			hssfcellstyle style = wb.createcellstyle();
			style.setwraptext(true);// 设置自动换行
			style.setalignment(hssfcellstyle.align_center);
			style.setverticalalignment(hssfcellstyle.vertical_center); // 创建一个居中格式
 
			style.setbottombordercolor(hssfcolor.black.index);
			style.setborderbottom(hssfcellstyle.border_thin);
			style.setborderleft(hssfcellstyle.border_thin);
			style.setborderright(hssfcellstyle.border_thin);
			style.setbordertop(hssfcellstyle.border_thin);
 
			hssffont headerfont = (hssffont) wb.createfont(); // 创建字体样式
			headerfont.setboldweight(hssffont.boldweight_bold); // 字体加粗
			headerfont.setfontname("黑体"); // 设置字体类型
			headerfont.setfontheightinpoints((short) 10); // 设置字体大小
			style.setfont(headerfont); // 为标题样式设置字体样式
 
			// 第四.一步,创建表头的列
			for (int i = 0; i < columnnumber; i++) 
			{
				hssfcell cell = row.createcell(i);
				cell.setcellvalue(columnname[i]);
				cell.setcellstyle(style);
			}
 
			// 第五步,创建单元格,并设置值
			for (int i = 0; i < datalist.length; i++) 
			{
				row = sheet.createrow((int) i + 2);
				// 为数据内容设置特点新单元格样式1 自动换行 上下居中
				hssfcellstyle zidonghuanhang = wb.createcellstyle();
				zidonghuanhang.setwraptext(true);// 设置自动换行
				zidonghuanhang.setverticalalignment(hssfcellstyle.vertical_center); // 创建一个居中格式
 
				// 设置边框
				zidonghuanhang.setbottombordercolor(hssfcolor.black.index);
				zidonghuanhang.setborderbottom(hssfcellstyle.border_thin);
				zidonghuanhang.setborderleft(hssfcellstyle.border_thin);
				zidonghuanhang.setborderright(hssfcellstyle.border_thin);
				zidonghuanhang.setbordertop(hssfcellstyle.border_thin);
 
				// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
				hssfcellstyle zidonghuanhang2 = wb.createcellstyle();
				zidonghuanhang2.setwraptext(true);// 设置自动换行
				zidonghuanhang2
						.setverticalalignment(hssfcellstyle.vertical_center); // 创建一个上下居中格式
				zidonghuanhang2.setalignment(hssfcellstyle.align_center);// 左右居中
 
				// 设置边框
				zidonghuanhang2.setbottombordercolor(hssfcolor.black.index);
				zidonghuanhang2.setborderbottom(hssfcellstyle.border_thin);
				zidonghuanhang2.setborderleft(hssfcellstyle.border_thin);
				zidonghuanhang2.setborderright(hssfcellstyle.border_thin);
				zidonghuanhang2.setbordertop(hssfcellstyle.border_thin);
				hssfcell datacell = null;
				for (int j = 0; j < columnnumber; j++) 
				{
					datacell = row.createcell(j);
					datacell.setcellvalue(datalist[i][j]);
					datacell.setcellstyle(zidonghuanhang2);
				}
			}
 
			// 第六步,将文件存到浏览器设置的下载位置
			string filename = filename + ".xls";
			response.setcontenttype("application/ms-excel;charset=utf-8");
			response.setheader("content-disposition", "attachment;filename="
					.concat(string.valueof(urlencoder.encode(filename, "utf-8"))));
			outputstream out = response.getoutputstream();
			try {
				wb.write(out);// 将数据写出去
				string str = "导出" + filename + "成功!";
				system.out.println(str);
			} catch (exception e) {
				e.printstacktrace();
				string str1 = "导出" + filename + "失败!";
				system.out.println(str1);
			} finally {
				out.close();
			}
 
		} else {
			system.out.println("列数目长度名称三个数组长度要一致");
		}
 
	}
 
	public void exportnoresponse(string sheetname, string titlename,
			string filename, int columnnumber, int[] columnwidth,
			string[] columnname, string[][] datalist) throws exception {
		if (columnnumber == columnwidth.length&& columnwidth.length == columnname.length) {
			// 第一步,创建一个webbook,对应一个excel文件
			hssfworkbook wb = new hssfworkbook();
			// 第二步,在webbook中添加一个sheet,对应excel文件中的sheet
			hssfsheet sheet = wb.createsheet(sheetname);
			// sheet.setdefaultcolumnwidth(15); //统一设置列宽
			for (int i = 0; i < columnnumber; i++) 
			{
				for (int j = 0; j <= i; j++) 
				{
					if (i == j) 
					{
						sheet.setcolumnwidth(i, columnwidth[j] * 256); // 单独设置每列的宽
					}
				}
			}
			// 创建第0行 也就是标题
			hssfrow row1 = sheet.createrow((int) 0);
			row1.setheightinpoints(50);// 设备标题的高度
			// 第三步创建标题的单元格样式style2以及字体样式headerfont1
			hssfcellstyle style2 = wb.createcellstyle();
			style2.setalignment(hssfcellstyle.align_center);
			style2.setverticalalignment(hssfcellstyle.vertical_center);
			style2.setfillforegroundcolor(hssfcolor.light_turquoise.index);
			style2.setfillpattern(hssfcellstyle.solid_foreground);
			hssffont headerfont1 = (hssffont) wb.createfont(); // 创建字体样式
			headerfont1.setboldweight(hssffont.boldweight_bold); // 字体加粗
			headerfont1.setfontname("黑体"); // 设置字体类型
			headerfont1.setfontheightinpoints((short) 15); // 设置字体大小
			style2.setfont(headerfont1); // 为标题样式设置字体样式
 
			hssfcell cell1 = row1.createcell(0);// 创建标题第一列
			sheet.addmergedregion(new cellrangeaddress(0, 0, 0,
					columnnumber - 1)); // 合并第0到第17列
			cell1.setcellvalue(titlename); // 设置值标题
			cell1.setcellstyle(style2); // 设置标题样式
 
			// 创建第1行 也就是表头
			hssfrow row = sheet.createrow((int) 1);
			row.setheightinpoints(37);// 设置表头高度
 
			// 第四步,创建表头单元格样式 以及表头的字体样式
			hssfcellstyle style = wb.createcellstyle();
			style.setwraptext(true);// 设置自动换行
			style.setalignment(hssfcellstyle.align_center);
			style.setverticalalignment(hssfcellstyle.vertical_center); // 创建一个居中格式
 
			style.setbottombordercolor(hssfcolor.black.index);
			style.setborderbottom(hssfcellstyle.border_thin);
			style.setborderleft(hssfcellstyle.border_thin);
			style.setborderright(hssfcellstyle.border_thin);
			style.setbordertop(hssfcellstyle.border_thin);
 
			hssffont headerfont = (hssffont) wb.createfont(); // 创建字体样式
			headerfont.setboldweight(hssffont.boldweight_bold); // 字体加粗
			headerfont.setfontname("黑体"); // 设置字体类型
			headerfont.setfontheightinpoints((short) 10); // 设置字体大小
			style.setfont(headerfont); // 为标题样式设置字体样式
 
			// 第四.一步,创建表头的列
			for (int i = 0; i < columnnumber; i++) 
			{
				hssfcell cell = row.createcell(i);
				cell.setcellvalue(columnname[i]);
				cell.setcellstyle(style);
			}
 
			// 第五步,创建单元格,并设置值
			for (int i = 0; i < datalist.length; i++) 
			{
				row = sheet.createrow((int) i + 2);
				// 为数据内容设置特点新单元格样式1 自动换行 上下居中
				hssfcellstyle zidonghuanhang = wb.createcellstyle();
				zidonghuanhang.setwraptext(true);// 设置自动换行
				zidonghuanhang
						.setverticalalignment(hssfcellstyle.vertical_center); // 创建一个居中格式
 
				// 设置边框
				zidonghuanhang.setbottombordercolor(hssfcolor.black.index);
				zidonghuanhang.setborderbottom(hssfcellstyle.border_thin);
				zidonghuanhang.setborderleft(hssfcellstyle.border_thin);
				zidonghuanhang.setborderright(hssfcellstyle.border_thin);
				zidonghuanhang.setbordertop(hssfcellstyle.border_thin);
 
				// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
				hssfcellstyle zidonghuanhang2 = wb.createcellstyle();
				zidonghuanhang2.setwraptext(true);// 设置自动换行
				zidonghuanhang2
						.setverticalalignment(hssfcellstyle.vertical_center); // 创建一个上下居中格式
				zidonghuanhang2.setalignment(hssfcellstyle.align_center);// 左右居中
 
				// 设置边框
				zidonghuanhang2.setbottombordercolor(hssfcolor.black.index);
				zidonghuanhang2.setborderbottom(hssfcellstyle.border_thin);
				zidonghuanhang2.setborderleft(hssfcellstyle.border_thin);
				zidonghuanhang2.setborderright(hssfcellstyle.border_thin);
				zidonghuanhang2.setbordertop(hssfcellstyle.border_thin);
				hssfcell datacell = null;
				for (int j = 0; j < columnnumber; j++) 
				{
					datacell = row.createcell(j);
					datacell.setcellvalue(datalist[i][j]);
					datacell.setcellstyle(zidonghuanhang2);
				}
			}
 
			// 第六步,将文件存到指定位置
			try {
				fileoutputstream fout = new fileoutputstream("d:students.xls");
				wb.write(fout);
				string str = "导出" + filename + "成功!";
				system.out.println(str);
				fout.close();
			} catch (exception e) {
				e.printstacktrace();
				string str1 = "导出" + filename + "失败!";
				system.out.println(str1);
			}
		} else {
			system.out.println("列数目长度名称三个数组长度要一致");
		}
 
	}
 
}

为了本地测试效果,单独写了一个无response参数的exportnoresponse方法,直接将文件保存到指定目录d盘。

两个方法的不同就在于第六步中,有response参数的方法可以将文件存到浏览器设置的下载位置。

下面是导出效果截图:

以上所述是小编给大家介绍的java导出excel方法详解整合,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网