SSM的 excel导出(多sheet对应不同数据库表数据查询)

SSM的 excel导出(多sheet对应不同数据库表数据查询)一键导出多sheet的excel前端JSP页面前端JS页面Controller层Service层ServiceImpl层dao model xml导出excel效果一键导出多sheet的excel记录 基于项目需求,实现一键导出多数据库表中的数据到一个excel中,用多sheet存放不同表数据。代码中导出的excel文件存放9个sheet前端JSP页面<a class="btm3" href="javascript:expdat
摘要由CSDN通过智能技术生成

一键导出多sheet的excel

记录 基于项目需求,实现一键导出多数据库表中的数据到一个excel中,用多sheet存放不同表数据。
代码中导出的excel文件存放9个sheet

前端JSP页面

<a class="btm3" href="javascript:expdata1()">省份报表导出</a>

前端JS页面

var _controller = contextPath + '/plugins/Tsfpowerratio/lowcostinfo';
var _export2 = _controller + '/export2.ilf';
function expdata1() {
   
	var conditions = getSearchConditions();
					var download_file = $("#download_file");
					if (download_file.length == 0) {
   
						var i = '<iframe id="download_file" name="download_file"></iframe>';
						$("body").append(i);
						download_file = $("#download_file");
					}
					 
					var fileName = "省份报表.xls";
					var filetype = "xlsx";
					var url = _export2 + "?filename=" + fileName + "&filetype=" + filetype
							+ "&conditions=" + conditions;
					
					download_file.attr("src", encodeURI(url));
					download_file.css("display", "none");

};

Controller层

@RequestMapping(value = "/export2.ilf")
	@ResponseBody
	public void export2(HttpServletRequest request, HttpServletResponse response) throws Exception {
   
		String realName = request.getParameter("filename");
		String filetype = request.getParameter("filetype");
		String conditions = request.getParameter("conditions");
		Map<String, String> c = DataTableCondition.getCondition(conditions);
		String filePath = lowcostinfoService.exportByCondition3(filetype, c);
		FileOperateUtil.download(request, response, filePath, realName);
	}

Service层

public String exportByCondition3(String filetype, Map<String, String> c);

ServiceImpl层

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;

import org.apache.ibatis.session.RowBounds;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Override
	public String exportByCondition3(String filetype, Map<String, String> c) {
   
		SXSSFWorkbook wb = new SXSSFWorkbook();  
		String filePath = "";
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        Sheet sheet = null;
        sheet = wb.createSheet("转供电比例");  
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 
        Row row = null;
        row = sheet.createRow((int) 0); 
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        CellStyle style = null;
        style = wb.createCellStyle(); 
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        Cell cell = null;
        cell = row.createCell(0); 
        cell.setCellValue("地市");  
        cell.setCellStyle(style);  
        cell = row.createCell(1);
        cell.setCellValue("自有总站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(2);
        cell.setCellValue("自有转供电站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(3);
        cell.setCellValue("自有转供电占比");  
        cell.setCellStyle(style); 
        cell = row.createCell(4);
        cell.setCellValue("自有转供电平均单价");  
        cell.setCellStyle(style);  
        cell = row.createCell(5);
        cell.setCellValue("自有直供电站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(6);
        cell.setCellValue("自有直供电占比");  
        cell.setCellStyle(style);  
        cell = row.createCell(7);
        cell.setCellValue("自有直供电平均单价");  
        cell.setCellStyle(style);  
        cell = row.createCell(8);
        cell.setCellValue("铁塔总站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(9);
        cell.setCellValue("铁塔转供电站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(10);
        cell.setCellValue("铁塔转供电占比");  
        cell.setCellStyle(style);  
        cell = row.createCell(11);
        cell.setCellValue("铁塔转供电平均单价");  
        cell.setCellStyle(style);  
        cell = row.createCell(12);
        cell.setCellValue("铁塔直供电站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(13);
        cell.setCellValue("铁塔直供电占比");  
        cell.setCellStyle(style);  
        cell = row.createCell(14);
        cell.setCellValue("铁塔直供电平均单价");  
        cell.setCellStyle(style);  
        cell = row.createCell(15);
        cell.setCellValue("小计总站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(16);
        cell.setCellValue("小计转供电站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(17);
        cell.setCellValue("小计转供电占比");  
        cell.setCellStyle(style);  
        cell = row.createCell(18);
        cell.setCellValue("小计转供电平均单价");  
        cell.setCellStyle(style);  
        cell = row.createCell(19);
        cell.setCellValue("小计直供电站房数");  
        cell.setCellStyle(style);  
        cell = row.createCell(20);
        cell.setCellValue("小计直供电占比");  
        cell.setCellStyle(style);  
        cell = row.createCell(21);
        cell.setCellValue("小计直供电平均单价");  
        cell.setCellStyle(style);  
        
       
        //创建sheet2
        Sheet sheet2 = null;
        sheet2 = wb.createSheet("自动计提率明细");  
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 
        Row row2 = null;
        row2 = sheet2.createRow((int) 0); 
        // 创建单元格,并设置值表头 设置表头居中  
        CellStyle style2 = null;
        style2 = wb.createCellStyle(); 
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        Cell cell2 = null;
        cell2 = row2.createCell(0); 
        cell2.setCellValue("地市");  
        cell2.setCellStyle(style);  
        cell2 = row2.createCell(1);
        cell2.setCellValue("自动计提率");  
        cell2.setCellStyle(style);
        
       //创建sheet3
        Sheet sheet3 = null;
        sheet3 = wb.createSheet("计提支付进度");  
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 
        Row row3 = null;
        row3 = sheet3.createRow((int) 0); 
        // 创建单元格,并设置值表头 设置表头居中  
        CellStyle style3 = null;
        style3 =
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现SSM数据导出Excel,你可以按照以下步骤进行: 1.在Spring配置文件中配置POI相关依赖,例如: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> ``` 2.创建一个Controller处理导出Excel的请求,例如: ``` @RequestMapping("/export") public void export(HttpServletResponse response) throws Exception { // 设置response头信息 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=data.xls"); response.flushBuffer(); // 获取数据 List<Data> dataList = dataService.getDataList(); // 创建Workbook Workbook workbook = new HSSFWorkbook(); // 创建Sheet Sheet sheet = workbook.createSheet("数据"); // 创建头 Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("序号"); headerRow.createCell(1).setCellValue("名称"); headerRow.createCell(2).setCellValue("数值"); // 填充数据 int rowNum = 1; for (Data data : dataList) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(data.getId()); row.createCell(1).setCellValue(data.getName()); row.createCell(2).setCellValue(data.getValue()); } // 输出数据 workbook.write(response.getOutputStream()); workbook.close(); } ``` 3.在前端页面中添加导出Excel的链接,例如: ``` <a href="/export">导出Excel</a> ``` 这样就可以实现SSM数据导出Excel了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值