poi 万级数据下载(excel文件导出)及简单的样式价值

org.apache.poi中的jar包准备

 

  1. poi-ooxml 3.14
  2. poi-ooxml-schemas 3.14(可以不用)
  3. poi 3.14
package tian;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.graphbuilder.struc.LinkedList;
//业务处理方法,更具业务需求自行封装
public class DownLoadExcel {
	public void downloadEecel() {
		SXSSFWorkbook sxssfworkbook=null;
		SXSSFRow nRow=null;
		Cell nCell=null;
		FileOutputStream fileO=null;
		String path=this.getClass().getClassLoader().getResource("").getPath();
		String fileName="tem.xlsx" ;
		File file=new File(path+fileName);
		if(file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		List list=(List) new LinkedList();
		String []strList= {};
		sxssfworkbook=new SXSSFWorkbook(100);
		CellStyle cellSstyle = sxssfworkbook.createCellStyle();
		cellSstyle.setAlignment(CellStyle.ALIGN_CENTER);// 设置居中
		for(int j=0;j<list.size();j++) {
			nRow=sxssfworkbook.createSheet().createRow(1);
			for(int i=0;i<strList.length;i++) {
				nCell.setCellValue("值");
			}
		}

	}

	// 还回给浏览器数据流用于下载
	public static void download(String fileName, String filePath, HttpServletResponse response) {

		File tempFile = new File(filePath + fileName);
		InputStream is = null;
		ServletOutputStream out=null;
		response.addHeader("Content-Disposition", "attachment:filename=" + fileName);
		response.addHeader("Connection", "close");
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		Cookie cook = new Cookie("filename", "true");
		cook.setPath("/");
		try {
			is = new FileInputStream(filePath + fileName);
			out = response.getOutputStream();
			byte[] content = new byte[1024];
			int length = 0;
			while ((length = is.read(content)) != -1) {// 请留意read里面的参数
				out.write(content, 0, length); // 将数据流给web
			}
			response.reset(); 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// 必要地清除response中的缓存信息response.setContType("applaication/vnd.openxmlformats-
							// officedocument.spreadsheetml.sheet;charset=utf-8")

		// java.net.URLEncoder.encode(fileName, "UTF-8"));

	}

}
 

关于样式问题:样式创建:CellStyle cellStyle=(new SXSSFWorkBook()).createCellStyle();

                                           cellStyle.setAlignment(CellStyle ."要加的水平方式");

边框:cellStyle.setBorder"边框位置"(CellStyle ."选择");

颜色:cellStyle.setBottonBorderColor(HSSFColor.“要选择的颜色”);自定义颜色用:Palatte。

最后一步:将设定好的样式放入到创建的单元格中:如:((new Row()).creatCell(1)).setCellStyle(cellStyle)

 

(主要还是流的应用,要去了解输入输出流才行,具体代码参见其它只提供思路和包路径,还要,要相信自己,

先写出下载再做更改很

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值