Java使用POI导出Excel

package com.cjj.demo;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;

/**
 * @author cjj
 * @date 2017年5月23日 上午10:34:46
 */
public class Test{
	public void writeExcel(HttpServletResponse response) throws IOException{
		//创建excel工作簿
		Workbook wb = new HSSFWorkbook();
		
		//创建第一个sheet(页),命名为 new sheet
		Sheet sheet1 = wb.createSheet("第一个sheet1");
		
		//设置列的宽度,setColumnWidth(索引,字符数 * 256(Width的单位是1/256个字符宽度))
		sheet1.setColumnWidth(0, 30 * 256);
		sheet1.setColumnWidth(1, 30 * 256);
		sheet1.setColumnWidth(2, 30 * 256);
		sheet1.setColumnWidth(3, 20 * 256);
		
		//Row 行
		//Cell 方格
		// Row 和 Cell 都是从0开始计数的
		// 创建一行,在页sheet上
		Row row = sheet1.createRow((short) 0);
		
		//设置行的宽度,createRow(索引),setHeight(像素 * 20(Height的单位是1/20个像素点))
		//row.setHeight((short)(20 * 20));
		
		// 设置表头样式
		HSSFCellStyle headerStyle = (HSSFCellStyle) wb.createCellStyle();//创建样式
		//headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中
		//headerStyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//填充背景色
		//headerStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);//填充背景色
		//headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);//选择用户定义的填充模式
		HSSFFont headerFont = (HSSFFont) wb.createFont();//创建字体
		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
		//headerFont.setColor(HSSFColor.WHITE.index);//字体颜色
		headerStyle.setFont(headerFont);//将字体应用到当前的样式
		
		// 在row行上创建列
		Cell cell0 = row.createCell(0);
		Cell cell1 = row.createCell(1);
		Cell cell2 = row.createCell(2);
		Cell cell3 = row.createCell(3);
		
		//设置列的值
		cell0.setCellValue("单元格1");
		cell1.setCellValue("单元格2");
		cell2.setCellValue("单元格3");
		cell3.setCellValue("单元格4");
		
		//设置列的样式
		cell0.setCellStyle(headerStyle);
		cell1.setCellStyle(headerStyle);
		cell2.setCellStyle(headerStyle);
		cell3.setCellStyle(headerStyle);
		
		//这里list为查询出的数据集合,从第二行开始循环赋值
		for (int i = 0; i < list.size(); i++) {
			row = sheet1.createRow((short) (i+1));
			row.createCell(0).setCellValue(list.get(i).get...());//单元格1
			row.createCell(1).setCellValue(list.get(i).get...());//单元格2
			row.createCell(2).setCellValue(list.get(i).get...());
			row.createCell(3).setCellValue(list.get(i).get...());
		}
		
		//文件名
		String filename =123.xls;
		//输出excel文件
		response.reset();
		OutputStream os = response.getOutputStream();//创建输出流
		//下载中文名乱码解决办法
		response.setHeader("Content-Disposition", "attachment;fileName="+ new String(filename.getBytes("gbk"),"iso-8859-1"));
		response.setContentType("application/msexcel");
		wb.write(os);// 把上面创建的工作簿写入到输出流中
		os.close();//关闭流
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值