控制器:
@RequestMapping(value = "/ServiceDifferenceListExportExcel", method = { RequestMethod.GET, RequestMethod.POST }) public void ServiceDifferenceListExportExcel() throws Exception{ AdminInfo adminInfo = (AdminInfo) request.getSession().getAttribute(DtConstant.CurrentUserH); if (adminInfo == null) { return; } String BATCH_NO = request.getParameter("BATCH_NO"); String MERCHANT_NUMBER = request.getParameter("MERCHANT_NUMBER"); String CHANNEL = request.getParameter("CHANNEL"); String UPDATE_STATE = request.getParameter("UPDATE_STATE"); String BEGINTIME = request.getParameter("BEGINTIME"); String ENDTIME = request.getParameter("ENDTIME"); String pageNo = request.getParameter("page"); String pageSize = request.getParameter("rows"); int pageno = pageNo == null ? 1 : Integer.parseInt(pageNo); int pagesize = pageSize == null ? 10 : Integer.parseInt(pageSize); Map<String, Object> params = new HashMap<String, Object>(); if (StringUtils.isBlank(BATCH_NO) == false) { params.put("BATCH_NO", BATCH_NO); } if (StringUtils.isBlank(MERCHANT_NUMBER) == false) { params.put("MERCHANT_NUMBER", MERCHANT_NUMBER); } if (StringUtils.isBlank(CHANNEL) == false) { params.put("CHANNEL", CHANNEL); } if (StringUtils.isBlank(UPDATE_STATE) == false) { params.put("UPDATE_STATE", UPDATE_STATE); } if (StringUtils.isBlank(BEGINTIME) == false) { params.put("BEGINTIME", BEGINTIME); } if (StringUtils.isBlank(ENDTIME) == false) { params.put("ENDTIME", ENDTIME); } String title = "手续费差异列表"; String[] rowName = new String[] { "序号", "对账时间", "批次号", "商户号", "鉴权通道", "平台渠道", "交易流水号", "交易状态(平台)", "交易时间(平台)", "商户订单号", "交易状态(第三方)", "交易时间(第三方)", "差异", "更新状态","更新时间" }; List<Object[]> dataList = new ArrayList<Object[]>(); Object[] objs = null; List<Map<String, Object>> list =serviceDifferenceService.selectLists(params); for(int i=0;i<list.size();i++){ objs = new Object[rowName.length]; objs[0] = i; if (list.get(i).get("ACCOUNT_TIME") != null) { objs[1] = list.get(i).get("ACCOUNT_TIME"); } else { objs[1] = ""; } if (list.get(i).get("BATCH_NO") != null) { objs[2] = list.get(i).get("BATCH_NO"); } else { objs[2] = ""; } if (list.get(i).get("MERCHANT_NUMBER") != null) { objs[3] = list.get(i).get("MERCHANT_NUMBER"); } else { objs[3] = ""; } if (list.get(i).get("AUTHENTICATION_PASSAGEWAY") != null) { objs[4] = list.get(i).get("AUTHENTICATION_PASSAGEWAY"); } else { objs[4] = ""; } if (list.get(i).get("CHANNEL_NAME") != null) { objs[5] = list.get(i).get("CHANNEL_NAME"); } else { objs[5] = ""; } if (list.get(i).get("PAY_TRADE_ID") != null) { objs[6] = list.get(i).get("PAY_TRADE_ID"); } else { objs[6] = ""; } if (list.get(i).get("TRANSACTION_STATUS_PT_NAME") != null) { objs[7] = list.get(i).get("TRANSACTION_STATUS_PT_NAME"); } else { objs[7] = ""; } if (list.get(i).get("TRANSACTION_TIME_PT") != null) { objs[8] = list.get(i).get("TRANSACTION_TIME_PT"); } else { objs[8] = ""; } if (list.get(i).get("MERCHANT_ORDER_NUMBER") != null) { objs[9] = list.get(i).get("MERCHANT_ORDER_NUMBER"); } else { objs[9] = ""; } if (list.get(i).get("TRANSACTION_STATUS_ZF_NAME") != null) { objs[10] = list.get(i).get("TRANSACTION_STATUS_ZF_NAME"); } else { objs[10] = ""; } if (list.get(i).get("TRANSACTION_TIME_ZF") != null) { objs[11] = list.get(i).get("TRANSACTION_TIME_ZF"); } else { objs[11] = ""; } if (list.get(i).get("DIFFERENCE_NAME") != null) { objs[12] = list.get(i).get("DIFFERENCE_NAME"); } else { objs[12] = ""; } if (list.get(i).get("UPDATE_STATE_NAME") != null) { objs[13] = list.get(i).get("UPDATE_STATE_NAME"); } else { objs[13] = ""; } if (list.get(i).get("UPDATE_TIME") != null) { objs[14] = list.get(i).get("UPDATE_TIME"); } else { objs[14] = ""; } dataList.add(objs); } String fileName = title + "_" + DateUtils.formatDate(DateUtils.FMT_yyyyMMddHHmmss, new Date()) + ".xls"; String headStr = "attachment;filename="" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + """; response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", headStr); OutputStream out = response.getOutputStream(); ExportExcelUtil eeu = new ExportExcelUtil(title, rowName, dataList); eeu.export(response, out); financialExportLogService.insert(adminInfo, DtConstant.EXPORT_SERVICE_DIFFERENCE_10510, dictService.selectDtCodeByKey(DtConstant.EXPORT_LOG, DtConstant.EXPORT_SERVICE_DIFFERENCE_10510).getCodename(), list.size(), params.toString()); out.close(); }
工具类:
package com.ydly.utils; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; 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.CellRangeAddress; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; /** * 导出excel公共方法 */ public class ExportExcelUtil{ // 显示导出表的标题 private String title; // 导出表的列名 private String[] rowName; // 传入的list数组 private List<Object[]> dataList = new ArrayList<Object[]>(); public ExportExcelUtil(String title, String[] rowName, List<Object[]> dataList) { super(); this.title = title; this.rowName = rowName; this.dataList = dataList; } public ExportExcelUtil() { } /** * 导出excel */ @SuppressWarnings("deprecation") public void export(HttpServletResponse response,OutputStream out) throws Exception{ try{ HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<dataList.size();i++){ Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+3);//创建所需的行数 for(int j=0; j<obj.length; j++){ HSSFCell cell = null; //设置单元格的数据类型 if(j == 0){ cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(""); } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } if(workbook !=null){ try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } /** * 导出成本核算 * @param response * @param out * @throws Exception */ @SuppressWarnings("deprecation") public void exportCost(HttpServletResponse response,OutputStream out, String subtitle) throws Exception{ try{ HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 产生其他标题行 HSSFRow rowmOther = sheet.createRow(2); HSSFCell cellTiltleOther = rowmOther.createCell(0); HSSFCellStyle styleOther = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, (rowName.length-1))); cellTiltleOther.setCellStyle(columnTopStyle); cellTiltleOther.setCellValue(subtitle); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(4); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<dataList.size();i++){ Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+5);//创建所需的行数 for(int j=0; j<obj.length; j++){ HSSFCell cell = null; //设置单元格的数据类型 cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(""); } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } if(workbook !=null){ try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } /** * 应收核算导出excel 参数3 放款开始日期 参数4 放款结束日期 * @Title: exporReceivable * @Description: TODO * @param: @param response * @param: @param out * @param: @param date_start * @param: @param date_end * @param: @throws Exception * @return: void * @throws zs */ @SuppressWarnings("deprecation") public void exporReceivable(HttpServletResponse response,OutputStream out,String date_start,String date_end) throws Exception{ try{ HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); HSSFRow rowm1 = sheet.createRow(2); HSSFCell cellTiltle1 = rowm1.createCell(0); sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 3)); cellTiltle1.setCellStyle(columnTopStyle); if(false==StringUtils.isBlank(date_start)){ cellTiltle1.setCellValue("放款开始时间:"+date_start); }else{ cellTiltle1.setCellValue("放款日期-"); } if(false==StringUtils.isBlank(date_end)){ cellTiltle1.setCellValue("放款结束时间"+date_end); }else{ cellTiltle1.setCellValue("放款日期-"); } if(false==StringUtils.isBlank(date_start) && false==StringUtils.isBlank(date_end)){ cellTiltle1.setCellValue("放款时间"+date_start+"至"+date_end); }else{ cellTiltle1.setCellValue("放款日期-"); } rowm1.createCell(1); rowm1.createCell(2); rowm1.createCell(3); rowm1.createCell(4); for (Cell cell : rowm1) { cell.setCellStyle(columnTopStyle); } HSSFCell cellTiltle2 = rowm1.createCell(4); sheet.addMergedRegion(new CellRangeAddress(2, 3, 4, 5)); cellTiltle2.setCellStyle(columnTopStyle); cellTiltle2.setCellValue("币种RMB 单位:元"); cellTiltle2 = rowm1.createCell(5); cellTiltle2.setCellStyle(columnTopStyle); //设置边框样式 HSSFRow createRowsan = sheet.createRow(3); createRowsan.createCell(0); createRowsan.createCell(1); createRowsan.createCell(2); createRowsan.createCell(3); createRowsan.createCell(4); createRowsan.createCell(5); for (Cell cell : createRowsan) { cell.setCellStyle(columnTopStyle); } // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(4); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<dataList.size();i++){ Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+5);//创建所需的行数 for(int j=0; j<obj.length; j++){ HSSFCell cell = null; //设置单元格的数据类型 if(j == 0){ cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(""); } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } if(workbook !=null){ try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } /** * 实收核算导出excel 参数3放款开始日期,参数4放款结束日期,参数5实还开始日期,参数6实还结束日期 * @Title: exporNetReceipts * @Description: TODO * @param: @param response * @param: @param out * @param: @param LOAN_DATE_START * @param: @param LOAN_DATE_END * @param: @param ACT_DAY_START * @param: @param ACT_DAY_END * @param: @throws Exception * @return: void * @throws zs */ @SuppressWarnings("deprecation") public void exporNetReceipts(HttpServletResponse response,OutputStream out,String LOAN_DATE_START,String LOAN_DATE_END,String ACT_DAY_START,String ACT_DAY_END) throws Exception{ try{ HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); HSSFRow rowm1 = sheet.createRow(2); HSSFCell cellTiltle1 = rowm1.createCell(0); sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 3)); cellTiltle1.setCellStyle(columnTopStyle); if(false==StringUtils.isBlank(ACT_DAY_START)){ cellTiltle1.setCellValue("实还开始时间:"+ACT_DAY_START); }else{ cellTiltle1.setCellValue("实还日期-"); } if(false==StringUtils.isBlank(ACT_DAY_END)){ cellTiltle1.setCellValue("实还结束时间"+ACT_DAY_END); }else{ cellTiltle1.setCellValue("实还日期-"); } if(false==StringUtils.isBlank(ACT_DAY_START) && false==StringUtils.isBlank(ACT_DAY_END)){ cellTiltle1.setCellValue("实还时间"+ACT_DAY_START+"至"+ACT_DAY_END); }else{ cellTiltle1.setCellValue("实还日期-"); } rowm1.createCell(1); rowm1.createCell(2); rowm1.createCell(3); rowm1.createCell(4); rowm1.createCell(5); rowm1.createCell(6); rowm1.createCell(7); rowm1.createCell(9); rowm1.createCell(10); rowm1.createCell(11); for (Cell cell : rowm1) { cell.setCellStyle(columnTopStyle); } HSSFRow createRowsan = sheet.createRow(3); createRowsan.createCell(1); createRowsan.createCell(2); createRowsan.createCell(3); createRowsan.createCell(4); createRowsan.createCell(5); createRowsan.createCell(6); createRowsan.createCell(7); createRowsan.createCell(9); createRowsan.createCell(10); createRowsan.createCell(11); for (Cell cell : createRowsan) { cell.setCellStyle(columnTopStyle); } HSSFCell cellTiltle3 = rowm1.createCell(4); sheet.addMergedRegion(new CellRangeAddress(2, 3, 4, 7)); cellTiltle3.setCellStyle(columnTopStyle); if(false==StringUtils.isBlank(LOAN_DATE_START)){ cellTiltle3.setCellValue("放款开始时间:"+LOAN_DATE_START); }else{ cellTiltle3.setCellValue("放款日期-"); } if(false==StringUtils.isBlank(LOAN_DATE_END)){ cellTiltle3.setCellValue("放款结束时间"+LOAN_DATE_END); }else{ cellTiltle3.setCellValue("放款日期-"); } if(false==StringUtils.isBlank(LOAN_DATE_START) && false==StringUtils.isBlank(LOAN_DATE_END)){ cellTiltle3.setCellValue("放款时间"+LOAN_DATE_START+"至"+LOAN_DATE_END); }else{ cellTiltle3.setCellValue("放款日期-"); } for (Cell cell : rowm1) { cell.setCellStyle(columnTopStyle); } // HSSFRow rowm2 = sheet.createRow(2); HSSFCell cellTiltle2 = rowm1.createCell(8); sheet.addMergedRegion(new CellRangeAddress(2, 3, 8, 11)); cellTiltle2.setCellStyle(columnTopStyle); cellTiltle2.setCellValue("币种RMB 单位:元"); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(4); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<dataList.size();i++){ Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+5);//创建所需的行数 for(int j=0; j<obj.length; j++){ HSSFCell cell = null; //设置单元格的数据类型 if(j == 0){ cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(""); } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } if(workbook !=null){ try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } @SuppressWarnings("deprecation") public void exports(HSSFWorkbook workbook,HttpServletResponse response,String title, String[] rowName, int sheetNum, String sheetTitle,List<Object[]> dataList) throws Exception{ try{ HSSFSheet sheet = workbook.createSheet(); // 创建工作表 workbook.setSheetName(sheetNum, sheetTitle); // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<dataList.size();i++){ Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+3);//创建所需的行数 for(int j=0; j<obj.length; j++){ HSSFCell cell = null; //设置单元格的数据类型 if(j == 0){ cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(""); } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } }catch(Exception e){ e.printStackTrace(); } } /* * 列头单元格样式 */ public HSSFCellStyle getColumnStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 font.setFontHeightInPoints((short)11); //字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; // style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; // style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; // style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; // style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; // style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; // style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; // style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; // style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } /* * 列头单元格样式 */ public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 font.setFontHeightInPoints((short)11); //字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } /* * 列数据信息单元格样式 */ public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 //font.setFontHeightInPoints((short)10); //字体加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }