从服务器上导出excel文件到本地

系统 1968 0
说明:
--------------------------------------------------------------------------------------------

从服务器上导出excel文件到本地

在一个excel工作簿中,有5个工单表,每个工作表有1万行数据。

--------------------------------------------------------------------------------------------
使用的jar包在附件中。

效果如图:

从服务器上导出excel文件到本地

    
<%@ page language="java" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>导出excel</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    导出excel
    <form action="excel" method="post">
    	<input type="submit" name="" value="导出">
    </form>
  </body>
</html>

  


    
	<servlet>
	   <servlet-name>outPutExcel</servlet-name>
	   <servlet-class>output.OutputExcel</servlet-class>
	</servlet>
	<servlet-mapping>
	   <servlet-name>outPutExcel</servlet-name>
	   <url-pattern>/excel</url-pattern>
	</servlet-mapping>

  


    
package output;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.HSSFRichTextString;
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.HSSFColor;
import org.apache.poi.hssf.util.Region;

/**
* EXCEL报表工具类.
*
* @author sun
* @version 
*/ 
public class ExportExcel {	
	
	private HSSFWorkbook wb = null;
	private HSSFSheet sheet = null; 
	
	/**
	* @param wb
	* @param sheet
	*/
	public ExportExcel(HSSFWorkbook wb, HSSFSheet sheet) {
		//super();
		this.wb = wb;
		this.sheet = sheet;
	}
	
	/**
	* 创建通用EXCEL头部
	*
	* @param headString 头部显示的字符
	* @param colSum 该报表的列数
	*/
	public void createNormalHead(String headString, int colSum) {

		HSSFRow row = sheet.createRow(0);
	
		// 设置第一行
		HSSFCell cell = row.createCell(0);
		//row.setHeight((short) 1000);
	
		// 定义单元格为字符串类型
		cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理
		cell.setCellValue(new HSSFRichTextString(headString));
	
		// 指定合并区域
		/**
		 * public Region(int rowFrom,
		 * short colFrom,
		 * int rowTo,
		 * short colTo)
		 */
		sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) colSum));
		
		//定义单元格格式,添加单元格表样式,并添加到工作簿
		HSSFCellStyle cellStyle = wb.createCellStyle();
		//设置单元格水平对齐类型
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
		cellStyle.setWrapText(true);// 指定单元格自动换行
	
		// 设置单元格字体
		HSSFFont font = wb.createFont();
		//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		//font.setFontName("宋体");
		//font.setFontHeight((short) 600);
		//cellStyle.setFont(font);	
		cell.setCellStyle(cellStyle);
	}
	
	/**
	* 创建通用报表第二行
	*
	* @param params 统计条件数组
	* @param colSum 需要合并到的列索引
	*/
	public void createNormalTwoRow(String[] params, int colSum) {
		//创建第二行
		HSSFRow row1 = sheet.createRow(1);
		
		row1.setHeight((short) 400);
	
		HSSFCell cell2 = row1.createCell(0);
	
		cell2.setCellType(HSSFCell.ENCODING_UTF_16);
		cell2.setCellValue(new HSSFRichTextString("时间:" + params[0] + "至" + params[1]));
	
		// 指定合并区域
		/**
		 * 	public Region(int rowFrom,
			short colFrom,
			int rowTo,
			short colTo)
		 */
		sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) colSum));
	
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
		cellStyle.setWrapText(true);// 指定单元格自动换行
	
		// 设置单元格字体
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 250);
		cellStyle.setFont(font);
	
		cell2.setCellStyle(cellStyle);
	}
	/**
	* 设置报表标题
	*
	* @param columHeader 标题字符串数组
	*/
	public void createColumHeader(String[] columHeader) {

		// 设置列头  在第三行
		HSSFRow row2 = sheet.createRow(2);
	
		// 指定行高
		row2.setHeight((short) 600);
	
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
		cellStyle.setWrapText(true);// 指定单元格自动换行
	
		// 单元格字体
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 250);
		cellStyle.setFont(font);

		/*cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单无格的边框为粗体
		cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setTopBorderColor(HSSFColor.BLACK.index);*/
	
		// 设置单元格背景色
		cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
	
		HSSFCell cell3 = null;
	
		for (int i = 0; i < columHeader.length; i++) {
		cell3 = row2.createCell(i);
		cell3.setCellType(HSSFCell.ENCODING_UTF_16);
		cell3.setCellStyle(cellStyle);
		cell3.setCellValue(new HSSFRichTextString(columHeader[i]));
		}
	}
	
	/**
	* 创建内容单元格
	*
	* @param wb HSSFWorkbook
	* @param row HSSFRow
	* @param col short型的列索引
	* @param align 对齐方式
	* @param val 列值
	*/
	public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col,short align, String val) {
		HSSFCell cell = row.createCell(col);
		cell.setCellType(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue(new HSSFRichTextString(val));
		HSSFCellStyle cellstyle = wb.createCellStyle();
		cellstyle.setAlignment(align);
		cell.setCellStyle(cellstyle);
	} 
	/**
	* 创建合计行
	* @param colSum 需要合并到的列索引
	* @param cellValue
	*/
	public void createLastSumRow(int colSum, String[] cellValue) {

		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
		cellStyle.setWrapText(true);// 指定单元格自动换行
	
		// 单元格字体
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 250);
		cellStyle.setFont(font);
		//获取工作表最后一行
		HSSFRow lastRow = sheet.createRow((short) (sheet.getLastRowNum() + 1));
		HSSFCell sumCell = lastRow.createCell(0);
	
		sumCell.setCellValue(new HSSFRichTextString("合计"));
		sumCell.setCellStyle(cellStyle);
		//合并 最后一行的第零列-最后一行的第一列
		sheet.addMergedRegion(new Region(sheet.getLastRowNum(), (short) 0,sheet.getLastRowNum(), (short) colSum));// 指定合并区域
	
		for (int i = 2; i < (cellValue.length + 2); i++) {
			//定义最后一行的第三列
			sumCell = lastRow.createCell(i);
			sumCell.setCellStyle(cellStyle);
			//定义数组 从0开始。
			sumCell.setCellValue(new HSSFRichTextString(cellValue[i-2]));
		}
	}
	/**
	* 输入EXCEL文件
	*
	* @param fileName 文件名
	*/
	public void outputExcel(String fileName) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(fileName));
			wb.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	//*****************************************************
	// set && get
	//*****************************************************

	/**
	* @return the sheet
	*/
	public HSSFSheet getSheet() {
		return sheet;
	}

	/**
	* @param sheet the sheet to set
	*/
	public void setSheet(HSSFSheet sheet) {
		this.sheet = sheet;
	}

	/**
	* @return the wb
	*/
	public HSSFWorkbook getWb() {
		return wb;
	}

	/**
	* @param wb the wb to set
	*/
	public void setWb(HSSFWorkbook wb) {
		this.wb = wb;
	}
}

  


    
package output;

public class Domain {
	private String one;
	private String two;
	private String three;
	private String four;
	private String five;
	private String six;
	private String seven;
	private String eight;
	private String nine;
	private String ten;
	public String getOne() {
		return one;
	}
	public void setOne(String one) {
		this.one = one;
	}
	public String getTwo() {
		return two;
	}
	public void setTwo(String two) {
		this.two = two;
	}
	public String getThree() {
		return three;
	}
	public void setThree(String three) {
		this.three = three;
	}
	public String getFour() {
		return four;
	}
	public void setFour(String four) {
		this.four = four;
	}
	public String getFive() {
		return five;
	}
	public void setFive(String five) {
		this.five = five;
	}
	public String getSix() {
		return six;
	}
	public void setSix(String six) {
		this.six = six;
	}
	public String getSeven() {
		return seven;
	}
	public void setSeven(String seven) {
		this.seven = seven;
	}
	public String getEight() {
		return eight;
	}
	public void setEight(String eight) {
		this.eight = eight;
	}
	public String getNine() {
		return nine;
	}
	public void setNine(String nine) {
		this.nine = nine;
	}
	public String getTen() {
		return ten;
	}
	public void setTen(String ten) {
		this.ten = ten;
	}
	
}

  


    
package output;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
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.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class OutputExcel extends HttpServlet{

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("helloworld");
		List<Domain> list = new ArrayList<Domain>();
		int max = 10000;
		
		String str = "测试长度";
		for(int i=0; i<max; i++){
			Domain domain = new Domain();
			domain.setOne(str+"1");
			domain.setTwo(str+"2");
			domain.setThree(str+"3");
			domain.setFour(str+"4");
			domain.setFive(str+"5");
			domain.setSix(str+"6");
			domain.setSeven(str+"7");
			domain.setEight(str+"8");
			domain.setNine(str+"9");
			domain.setTen(str+"10");
			list.add(domain);
		}
		
		String fileName = "导出Excel.xls";
		fileName = new String(fileName.getBytes("GBK"),"iso8859-1");
		response.reset();
		response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下载的文件名
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		OutputStream output = response.getOutputStream();
		BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
		
		//定义单元格报头
		String worksheetTitle = "Excel导出";
		
		HSSFWorkbook wb = new HSSFWorkbook();
		
		//创建列标头LIST
		List<String> fialList = new ArrayList<String>();
		fialList.add("列1");
		fialList.add("列2");
		fialList.add("列3");
		fialList.add("列4");
		fialList.add("列5");
		fialList.add("列6");
		fialList.add("列7");
		fialList.add("列8");
		fialList.add("列9");
		fialList.add("列10");

		// 计算该报表的列数
		int number = fialList.size()-1;
		//==================================================================
		// 创建单元格样式
		HSSFCellStyle cellStyleTitle = wb.createCellStyle();
		// 指定单元格居中对齐
		cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 指定单元格垂直居中对齐
		cellStyleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 指定当单元格内容显示不下时自动换行
		cellStyleTitle.setWrapText(true);
		//------------------------------------------------------------------
		HSSFCellStyle cellStyle = wb.createCellStyle();
		// 指定单元格居中对齐
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 指定单元格垂直居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 指定当单元格内容显示不下时自动换行
		cellStyle.setWrapText(true);
		//------------------------------------------------------------------
		// 设置单元格字体
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 200);
		cellStyleTitle.setFont(font);
		
		for(int z=0; z<5; z++){
			//工作表名
			String worksheet = "表"+(z+1);
			
			HSSFSheet sheet = wb.createSheet(worksheet);

			ExportExcel exportExcel = new ExportExcel(wb, sheet);

			// 创建报表头部
			exportExcel.createNormalHead(worksheetTitle, number);
			//定义第一行
			HSSFRow row1 = sheet.createRow(1);
			HSSFCell cell1 = null;
			for(int i = 0; i < fialList.size(); i++) {
				cell1 = row1.createCell(i);
				cell1.setCellStyle(cellStyleTitle);
				cell1.setCellValue(new HSSFRichTextString(worksheet+fialList.get(i).toString()));
			}
			HSSFRow row = sheet.createRow(2);
			HSSFCell cell = row.createCell(1);
			Domain domain = new Domain();
			for(int i=0; i<list.size(); i++){
				domain = list.get(i);
				row = sheet.createRow(i+2);
				cell = row.createCell(0);
				cell.setCellStyle(cellStyle);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getOne()));
				cell = row.createCell(1);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getTwo()));
				cell = row.createCell(2);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getThree()));
				cell = row.createCell(3);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getFour()));
				cell = row.createCell(4);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getFive()));
				cell = row.createCell(5);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getSix()));
				cell = row.createCell(6);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getSeven()));
				cell = row.createCell(7);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getEight()));
				cell = row.createCell(8);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getNine()));
				cell = row.createCell(9);
				cell.setCellValue(new HSSFRichTextString(worksheet+domain.getTen()));
			}
		}
		try {
			bufferedOutPut.flush();
			wb.write(bufferedOutPut);
			bufferedOutPut.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println( "Output   is   closed ");
		} finally {
			list.clear();
		}
	}
}

  

从服务器上导出excel文件到本地


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论