ExportExcel.java类
package com.pro.lottery.action; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; 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.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import com.pro.lottery.dao.DataSourceFactory; import com.pro.lottery.modle.ReqLottery; /** * * @author Caixu * */ public class ExportExcel extends BaseAction { /** * */ private static final long serialVersionUID = -3297347144890990614L; public static double totalCount = 0; //总共有多少条数据 public static double execCount = 0; //已经处理的数据 /** * 对list数据源将其里面的数据导入到excel表单 * * @param fieldName * [] 导出到excel文件里的表头名 * @param columnIt * [] 导出到excel文件里的表头NAME * @param sheetName * 工作表的名称 * @param sheetSize * 每个sheet中数据的行数,此数值必须小于65536 * @param output * java输出流 */ public static boolean exportExcel(List> list, String[] fieldName, Object[] columnIt, String sheetName, int sheetSize, OutputStream output) { HSSFWorkbook workbook = new HSSFWorkbook();// 产生工作薄对象 if (sheetSize >= 65536) { sheetSize = 65536; } double sheetNo = Math.ceil(list.size() / sheetSize); for (int index = 0; index <= sheetNo; index++) { HSSFSheet sheet = workbook.createSheet();// 产生工作表对象 workbook.setSheetName(index, sheetName+index);//设置工作表的名称. HSSFRow row = sheet.createRow(0);// 产生一行 HSSFCell cell;// 产生单元格 //表头样式 HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式 headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //设置垂直居中 headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置水平居中 HSSFFont headerFont = (HSSFFont) workbook.createFont(); //创建字体样式 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗 headerFont.setFontName("Times New Roman"); //设置字体类型 headerFont.setFontHeightInPoints((short) 10); //设置字体大小 headerStyle.setFont(headerFont); //为标题样式设置字体样式 headerStyle.setFillBackgroundColor((short) 2); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框 headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框 headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框 headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 headerStyle.setWrapText(true); // 设置为自动换行 headerStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置背景色 headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置前景色 headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //表body样式 HSSFCellStyle cellStyle = (HSSFCellStyle) workbook .createCellStyle(); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //设置垂直居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置水平居中 cellStyle.setWrapText(true); // 设置为自动换行 // 写入各个字段的名称 for (int i = 0; i < fieldName.length; i++) { cell = row.createCell(i); // 创建第一行各个字段名称的单元格 row.setHeight((short) 450); cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置单元格内容为字符串型 // cell.setEncoding(HSSFCell.ENCODING_UTF_16); // //为了能在单元格中输入中文,设置字符集为UTF_16 cell.setCellValue(fieldName[i]); // 给单元格内容赋值 cell.setCellStyle(headerStyle); } int startNo = index * sheetSize; int endNo = Math.min(startNo + sheetSize, list.size()); // 写入各条记录,每条记录对应excel表中的一行 for (int i = startNo; i < endNo; i++) { row = sheet.createRow(i + 1 - startNo); row.setHeight((short) 350); sheet.setColumnWidth(i, 4500); HashMap map = (HashMap) list.get(i); for (int j = 0; j < columnIt.length; j++) { cell = row.createCell(j); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellStyle(cellStyle); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); Object value = map.get(columnIt[j]); if (value != null) { cell.setCellValue(map.get(columnIt[j]).toString()); } else cell.setCellValue(""); } execCount = execCount + 1; } } try { //为了让百分比不显示100% execCount = execCount - (totalCount * 0.02); workbook.write(output); execCount = execCount + (totalCount * 0.02); output.flush(); output.close(); return true; } catch (IOException e) { e.printStackTrace(); System.out.println("Output is closed "); System.out.println("清除了。。。。。。。。。。。"); execCount = 0; return false; } } public String exportExcelAction(){ HttpServletResponse response = this.getResponse(); HttpServletRequest request = this.getRequest(); response.setCharacterEncoding("utf-8"); String fileName = request.getParameter("fileName"); // 初始化数据 /* List
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
效果图如下: