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

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

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

效果如图:


Jsp代码 复制代码  收藏代码
  1. <%@ page language="java" pageEncoding="GBK"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <title>导出excel</title>   
  11.     <meta http-equiv="pragma" content="no-cache">   
  12.     <meta http-equiv="cache-control" content="no-cache">   
  13.     <meta http-equiv="expires" content="0">       
  14.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  15.     <meta http-equiv="description" content="This is my page">   
  16.   </head>   
  17.      
  18.   <body>   
  19.     导出excel   
  20.     <form action="excel" method="post">   
  21.         <input type="submit" name="" value="导出">   
  22.     </form>   
  23.   </body>   
  24. </html>  


Xml代码 复制代码  收藏代码
  1. <servlet>  
  2.    <servlet-name>outPutExcel</servlet-name>  
  3.    <servlet-class>output.OutputExcel</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.    <servlet-name>outPutExcel</servlet-name>  
  7.    <url-pattern>/excel</url-pattern>  
  8. </servlet-mapping>  


Java代码 复制代码  收藏代码
  1. package output;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7.   
  8. import org.apache.poi.hssf.usermodel.HSSFCell;   
  9. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  10. import org.apache.poi.hssf.usermodel.HSSFFont;   
  11. import org.apache.poi.hssf.usermodel.HSSFRichTextString;   
  12. import org.apache.poi.hssf.usermodel.HSSFRow;   
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  15. import org.apache.poi.hssf.util.HSSFColor;   
  16. import org.apache.poi.hssf.util.Region;   
  17.   
  18. /**  
  19. * EXCEL报表工具类.  
  20. *  
  21. * @author sun  
  22. * @version   
  23. */    
  24. public class ExportExcel {     
  25.        
  26.     private HSSFWorkbook wb = null;   
  27.     private HSSFSheet sheet = null;    
  28.        
  29.     /**  
  30.     * @param wb  
  31.     * @param sheet  
  32.     */  
  33.     public ExportExcel(HSSFWorkbook wb, HSSFSheet sheet) {   
  34.         //super();   
  35.         this.wb = wb;   
  36.         this.sheet = sheet;   
  37.     }   
  38.        
  39.     /**  
  40.     * 创建通用EXCEL头部  
  41.     *  
  42.     * @param headString 头部显示的字符  
  43.     * @param colSum 该报表的列数  
  44.     */  
  45.     public void createNormalHead(String headString, int colSum) {   
  46.   
  47.         HSSFRow row = sheet.createRow(0);   
  48.        
  49.         // 设置第一行   
  50.         HSSFCell cell = row.createCell(0);   
  51.         //row.setHeight((short) 1000);   
  52.        
  53.         // 定义单元格为字符串类型   
  54.         cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理   
  55.         cell.setCellValue(new HSSFRichTextString(headString));   
  56.        
  57.         // 指定合并区域   
  58.         /**  
  59.          * public Region(int rowFrom,  
  60.          * short colFrom,  
  61.          * int rowTo,  
  62.          * short colTo)  
  63.          */  
  64.         sheet.addMergedRegion(new Region(0, (short00, (short) colSum));   
  65.            
  66.         //定义单元格格式,添加单元格表样式,并添加到工作簿   
  67.         HSSFCellStyle cellStyle = wb.createCellStyle();   
  68.         //设置单元格水平对齐类型   
  69.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐   
  70.         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐   
  71.         cellStyle.setWrapText(true);// 指定单元格自动换行   
  72.        
  73.         // 设置单元格字体   
  74.         HSSFFont font = wb.createFont();   
  75.         //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
  76.         //font.setFontName("宋体");   
  77.         //font.setFontHeight((short) 600);   
  78.         //cellStyle.setFont(font);     
  79.         cell.setCellStyle(cellStyle);   
  80.     }   
  81.        
  82.     /**  
  83.     * 创建通用报表第二行  
  84.     *  
  85.     * @param params 统计条件数组  
  86.     * @param colSum 需要合并到的列索引  
  87.     */  
  88.     public void createNormalTwoRow(String[] params, int colSum) {   
  89.         //创建第二行   
  90.         HSSFRow row1 = sheet.createRow(1);   
  91.            
  92.         row1.setHeight((short400);   
  93.        
  94.         HSSFCell cell2 = row1.createCell(0);   
  95.        
  96.         cell2.setCellType(HSSFCell.ENCODING_UTF_16);   
  97.         cell2.setCellValue(new HSSFRichTextString("时间:" + params[0] + "至" + params[1]));   
  98.        
  99.         // 指定合并区域   
  100.         /**  
  101.          *  public Region(int rowFrom,  
  102.             short colFrom,  
  103.             int rowTo,  
  104.             short colTo)  
  105.          */  
  106.         sheet.addMergedRegion(new Region(1, (short01, (short) colSum));   
  107.        
  108.         HSSFCellStyle cellStyle = wb.createCellStyle();   
  109.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐   
  110.         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐   
  111.         cellStyle.setWrapText(true);// 指定单元格自动换行   
  112.        
  113.         // 设置单元格字体   
  114.         HSSFFont font = wb.createFont();   
  115.         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
  116.         font.setFontName("宋体");   
  117.         font.setFontHeight((short250);   
  118.         cellStyle.setFont(font);   
  119.        
  120.         cell2.setCellStyle(cellStyle);   
  121.     }   
  122.     /**  
  123.     * 设置报表标题  
  124.     *  
  125.     * @param columHeader 标题字符串数组  
  126.     */  
  127.     public void createColumHeader(String[] columHeader) {   
  128.   
  129.         // 设置列头  在第三行   
  130.         HSSFRow row2 = sheet.createRow(2);   
  131.        
  132.         // 指定行高   
  133.         row2.setHeight((short600);   
  134.        
  135.         HSSFCellStyle cellStyle = wb.createCellStyle();   
  136.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐   
  137.         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐   
  138.         cellStyle.setWrapText(true);// 指定单元格自动换行   
  139.        
  140.         // 单元格字体   
  141.         HSSFFont font = wb.createFont();   
  142.         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
  143.         font.setFontName("宋体");   
  144.         font.setFontHeight((short250);   
  145.         cellStyle.setFont(font);   
  146.   
  147.         /*cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单无格的边框为粗体  
  148.         cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.  
  149.         cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  150.         cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);  
  151.         cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  152.         cellStyle.setRightBorderColor(HSSFColor.BLACK.index);  
  153.         cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  154.         cellStyle.setTopBorderColor(HSSFColor.BLACK.index);*/  
  155.        
  156.         // 设置单元格背景色   
  157.         cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);   
  158.         cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);   
  159.        
  160.         HSSFCell cell3 = null;   
  161.        
  162.         for (int i = 0; i < columHeader.length; i++) {   
  163.         cell3 = row2.createCell(i);   
  164.         cell3.setCellType(HSSFCell.ENCODING_UTF_16);   
  165.         cell3.setCellStyle(cellStyle);   
  166.         cell3.setCellValue(new HSSFRichTextString(columHeader[i]));   
  167.         }   
  168.     }   
  169.        
  170.     /**  
  171.     * 创建内容单元格  
  172.     *  
  173.     * @param wb HSSFWorkbook  
  174.     * @param row HSSFRow  
  175.     * @param col short型的列索引  
  176.     * @param align 对齐方式  
  177.     * @param val 列值  
  178.     */  
  179.     public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col,short align, String val) {   
  180.         HSSFCell cell = row.createCell(col);   
  181.         cell.setCellType(HSSFCell.ENCODING_UTF_16);   
  182.         cell.setCellValue(new HSSFRichTextString(val));   
  183.         HSSFCellStyle cellstyle = wb.createCellStyle();   
  184.         cellstyle.setAlignment(align);   
  185.         cell.setCellStyle(cellstyle);   
  186.     }    
  187.     /**  
  188.     * 创建合计行  
  189.     * @param colSum 需要合并到的列索引  
  190.     * @param cellValue  
  191.     */  
  192.     public void createLastSumRow(int colSum, String[] cellValue) {   
  193.   
  194.         HSSFCellStyle cellStyle = wb.createCellStyle();   
  195.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐   
  196.         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐   
  197.         cellStyle.setWrapText(true);// 指定单元格自动换行   
  198.        
  199.         // 单元格字体   
  200.         HSSFFont font = wb.createFont();   
  201.         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
  202.         font.setFontName("宋体");   
  203.         font.setFontHeight((short250);   
  204.         cellStyle.setFont(font);   
  205.         //获取工作表最后一行   
  206.         HSSFRow lastRow = sheet.createRow((short) (sheet.getLastRowNum() + 1));   
  207.         HSSFCell sumCell = lastRow.createCell(0);   
  208.        
  209.         sumCell.setCellValue(new HSSFRichTextString("合计"));   
  210.         sumCell.setCellStyle(cellStyle);   
  211.         //合并 最后一行的第零列-最后一行的第一列   
  212.         sheet.addMergedRegion(new Region(sheet.getLastRowNum(), (short0,sheet.getLastRowNum(), (short) colSum));// 指定合并区域   
  213.        
  214.         for (int i = 2; i < (cellValue.length + 2); i++) {   
  215.             //定义最后一行的第三列   
  216.             sumCell = lastRow.createCell(i);   
  217.             sumCell.setCellStyle(cellStyle);   
  218.             //定义数组 从0开始。   
  219.             sumCell.setCellValue(new HSSFRichTextString(cellValue[i-2]));   
  220.         }   
  221.     }   
  222.     /**  
  223.     * 输入EXCEL文件  
  224.     *  
  225.     * @param fileName 文件名  
  226.     */  
  227.     public void outputExcel(String fileName) {   
  228.         FileOutputStream fos = null;   
  229.         try {   
  230.             fos = new FileOutputStream(new File(fileName));   
  231.             wb.write(fos);   
  232.             fos.close();   
  233.         } catch (FileNotFoundException e) {   
  234.             e.printStackTrace();   
  235.         } catch (IOException e) {   
  236.             e.printStackTrace();   
  237.         }   
  238.     }   
  239.        
  240.        
  241.     //*****************************************************   
  242.     // set && get   
  243.     //*****************************************************   
  244.   
  245.     /**   
  246.     * @return the sheet   
  247.     */   
  248.     public HSSFSheet getSheet() {   
  249.         return sheet;   
  250.     }   
  251.   
  252.     /**  
  253.     * @param sheet the sheet to set  
  254.     */  
  255.     public void setSheet(HSSFSheet sheet) {   
  256.         this.sheet = sheet;   
  257.     }   
  258.   
  259.     /**  
  260.     * @return the wb  
  261.     */  
  262.     public HSSFWorkbook getWb() {   
  263.         return wb;   
  264.     }   
  265.   
  266.     /**  
  267.     * @param wb the wb to set  
  268.     */  
  269.     public void setWb(HSSFWorkbook wb) {   
  270.         this.wb = wb;   
  271.     }   
  272. }  
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;
	}
}


Java代码 复制代码  收藏代码
  1. package output;   
  2.   
  3. public class Domain {   
  4.     private String one;   
  5.     private String two;   
  6.     private String three;   
  7.     private String four;   
  8.     private String five;   
  9.     private String six;   
  10.     private String seven;   
  11.     private String eight;   
  12.     private String nine;   
  13.     private String ten;   
  14.     public String getOne() {   
  15.         return one;   
  16.     }   
  17.     public void setOne(String one) {   
  18.         this.one = one;   
  19.     }   
  20.     public String getTwo() {   
  21.         return two;   
  22.     }   
  23.     public void setTwo(String two) {   
  24.         this.two = two;   
  25.     }   
  26.     public String getThree() {   
  27.         return three;   
  28.     }   
  29.     public void setThree(String three) {   
  30.         this.three = three;   
  31.     }   
  32.     public String getFour() {   
  33.         return four;   
  34.     }   
  35.     public void setFour(String four) {   
  36.         this.four = four;   
  37.     }   
  38.     public String getFive() {   
  39.         return five;   
  40.     }   
  41.     public void setFive(String five) {   
  42.         this.five = five;   
  43.     }   
  44.     public String getSix() {   
  45.         return six;   
  46.     }   
  47.     public void setSix(String six) {   
  48.         this.six = six;   
  49.     }   
  50.     public String getSeven() {   
  51.         return seven;   
  52.     }   
  53.     public void setSeven(String seven) {   
  54.         this.seven = seven;   
  55.     }   
  56.     public String getEight() {   
  57.         return eight;   
  58.     }   
  59.     public void setEight(String eight) {   
  60.         this.eight = eight;   
  61.     }   
  62.     public String getNine() {   
  63.         return nine;   
  64.     }   
  65.     public void setNine(String nine) {   
  66.         this.nine = nine;   
  67.     }   
  68.     public String getTen() {   
  69.         return ten;   
  70.     }   
  71.     public void setTen(String ten) {   
  72.         this.ten = ten;   
  73.     }   
  74.        
  75. }  
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;
	}
	
}


Java代码 复制代码  收藏代码
  1. package output;   
  2.   
  3. import java.io.BufferedOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.OutputStream;   
  6. import java.util.ArrayList;   
  7. import java.util.List;   
  8.   
  9. import javax.servlet.ServletException;   
  10. import javax.servlet.http.HttpServlet;   
  11. import javax.servlet.http.HttpServletRequest;   
  12. import javax.servlet.http.HttpServletResponse;   
  13.   
  14. import org.apache.poi.hssf.usermodel.HSSFCell;   
  15. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  16. import org.apache.poi.hssf.usermodel.HSSFFont;   
  17. import org.apache.poi.hssf.usermodel.HSSFRichTextString;   
  18. import org.apache.poi.hssf.usermodel.HSSFRow;   
  19. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  21.   
  22. public class OutputExcel extends HttpServlet{   
  23.   
  24.     private static final long serialVersionUID = 1L;   
  25.   
  26.     protected void doGet(HttpServletRequest request, HttpServletResponse response)   
  27.             throws ServletException, IOException {   
  28.         this.doPost(request, response);   
  29.     }   
  30.   
  31.     protected void doPost(HttpServletRequest request, HttpServletResponse response)   
  32.             throws ServletException, IOException {   
  33.         System.out.println("helloworld");   
  34.         List<Domain> list = new ArrayList<Domain>();   
  35.         int max = 10000;   
  36.            
  37.         String str = "测试长度";   
  38.         for(int i=0; i<max; i++){   
  39.             Domain domain = new Domain();   
  40.             domain.setOne(str+"1");   
  41.             domain.setTwo(str+"2");   
  42.             domain.setThree(str+"3");   
  43.             domain.setFour(str+"4");   
  44.             domain.setFive(str+"5");   
  45.             domain.setSix(str+"6");   
  46.             domain.setSeven(str+"7");   
  47.             domain.setEight(str+"8");   
  48.             domain.setNine(str+"9");   
  49.             domain.setTen(str+"10");   
  50.             list.add(domain);   
  51.         }   
  52.            
  53.         String fileName = "导出Excel.xls";   
  54.         fileName = new String(fileName.getBytes("GBK"),"iso8859-1");   
  55.         response.reset();   
  56.         response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下载的文件名   
  57.         response.setContentType("application/vnd.ms-excel");   
  58.         response.setHeader("Pragma""no-cache");   
  59.         response.setHeader("Cache-Control""no-cache");   
  60.         response.setDateHeader("Expires"0);   
  61.         OutputStream output = response.getOutputStream();   
  62.         BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);   
  63.            
  64.         //定义单元格报头   
  65.         String worksheetTitle = "Excel导出";   
  66.            
  67.         HSSFWorkbook wb = new HSSFWorkbook();   
  68.            
  69.         //创建列标头LIST   
  70.         List<String> fialList = new ArrayList<String>();   
  71.         fialList.add("列1");   
  72.         fialList.add("列2");   
  73.         fialList.add("列3");   
  74.         fialList.add("列4");   
  75.         fialList.add("列5");   
  76.         fialList.add("列6");   
  77.         fialList.add("列7");   
  78.         fialList.add("列8");   
  79.         fialList.add("列9");   
  80.         fialList.add("列10");   
  81.   
  82.         // 计算该报表的列数   
  83.         int number = fialList.size()-1;   
  84.         //==================================================================   
  85.         // 创建单元格样式   
  86.         HSSFCellStyle cellStyleTitle = wb.createCellStyle();   
  87.         // 指定单元格居中对齐   
  88.         cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);   
  89.         // 指定单元格垂直居中对齐   
  90.         cellStyleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);   
  91.         // 指定当单元格内容显示不下时自动换行   
  92.         cellStyleTitle.setWrapText(true);   
  93.         //------------------------------------------------------------------   
  94.         HSSFCellStyle cellStyle = wb.createCellStyle();   
  95.         // 指定单元格居中对齐   
  96.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);   
  97.         // 指定单元格垂直居中对齐   
  98.         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);   
  99.         // 指定当单元格内容显示不下时自动换行   
  100.         cellStyle.setWrapText(true);   
  101.         //------------------------------------------------------------------   
  102.         // 设置单元格字体   
  103.         HSSFFont font = wb.createFont();   
  104.         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
  105.         font.setFontName("宋体");   
  106.         font.setFontHeight((short200);   
  107.         cellStyleTitle.setFont(font);   
  108.            
  109.         for(int z=0; z<5; z++){   
  110.             //工作表名   
  111.             String worksheet = "表"+(z+1);   
  112.                
  113.             HSSFSheet sheet = wb.createSheet(worksheet);   
  114.   
  115.             ExportExcel exportExcel = new ExportExcel(wb, sheet);   
  116.   
  117.             // 创建报表头部   
  118.             exportExcel.createNormalHead(worksheetTitle, number);   
  119.             //定义第一行   
  120.             HSSFRow row1 = sheet.createRow(1);   
  121.             HSSFCell cell1 = null;   
  122.             for(int i = 0; i < fialList.size(); i++) {   
  123.                 cell1 = row1.createCell(i);   
  124.                 cell1.setCellStyle(cellStyleTitle);   
  125.                 cell1.setCellValue(new HSSFRichTextString(worksheet+fialList.get(i).toString()));   
  126.             }   
  127.             HSSFRow row = sheet.createRow(2);   
  128.             HSSFCell cell = row.createCell(1);   
  129.             Domain domain = new Domain();   
  130.             for(int i=0; i<list.size(); i++){   
  131.                 domain = list.get(i);   
  132.                 row = sheet.createRow(i+2);   
  133.                 cell = row.createCell(0);   
  134.                 cell.setCellStyle(cellStyle);   
  135.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getOne()));   
  136.                 cell = row.createCell(1);   
  137.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getTwo()));   
  138.                 cell = row.createCell(2);   
  139.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getThree()));   
  140.                 cell = row.createCell(3);   
  141.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getFour()));   
  142.                 cell = row.createCell(4);   
  143.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getFive()));   
  144.                 cell = row.createCell(5);   
  145.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getSix()));   
  146.                 cell = row.createCell(6);   
  147.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getSeven()));   
  148.                 cell = row.createCell(7);   
  149.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getEight()));   
  150.                 cell = row.createCell(8);   
  151.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getNine()));   
  152.                 cell = row.createCell(9);   
  153.                 cell.setCellValue(new HSSFRichTextString(worksheet+domain.getTen()));   
  154.             }   
  155.         }   
  156.         try {   
  157.             bufferedOutPut.flush();   
  158.             wb.write(bufferedOutPut);   
  159.             bufferedOutPut.close();   
  160.         } catch (IOException e) {   
  161.             e.printStackTrace();   
  162.             System.out.println( "Output   is   closed ");   
  163.         } finally {   
  164.             list.clear();   
  165.         }   
  166.     }   
  167. }  
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值