package com.senlany.service.impl.k3;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlany.comm.ConnUtil;
import com.senlany.mapper.k3.FinancialReportMapper;
/**
* @author Amos E-mail : zhangxiaopeng@wlspjt.com
* @version 创建时间:2015年5月28日 下午2:36:37
* @description
* 因程序未按照标准java程序结构创建
* 现将程序逻辑放在service中
*/
@Service
public class FinancialReportService {
private static Log log = LogFactory.getLog(FinancialReportService.class);
@Autowired
private FinancialReportMapper financialReportMapper;
/**
* @param map
* @return
*/
public List<Map> getList(Map map){
return financialReportMapper.getList(map);
}
/**
* 导出财务报表
* @param begindate
* @param enddate
*/
public void exportExcel(HttpServletRequest request, HttpServletResponse response){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
conn = ConnUtil.getConn();
WritableWorkbook workbook = null;
WritableSheet sheet = null;
Label label = null;
Label label0 = null;
try {
/**
* 此程序块必须放在此处执行
* 因此处调用函数中有批量更新操作
*/
conn.setAutoCommit(false);
st = (Statement) conn.createStatement();
st.addBatch("dbo.WL_F_MAIN");
st.executeBatch();
conn.commit();
//查询全局临时表
CallableStatement cs = conn.prepareCall("SELECT * FROM ##financial_report_table");
rs = cs.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建Excel表
try {
/**
* 将工作薄输出至前台文件输出流
*/
String contentType = "application/vnd.ms-excel";
response.reset();
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String("休闲营销财务报表.xls".getBytes("gb2312"), "ISO8859-1") + "\"");
OutputStream out = response.getOutputStream();
workbook = Workbook.createWorkbook(out);
// 创建Excel表中的sheet
sheet = workbook.createSheet("First Sheet", 0);
// 向Excel中添加数据
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String colName = null;
int row = 2;
int income_count = 0;//收入计数
int inc_cat_xxd_ct = 0;//收入_休闲豆计数
int inc_cat_rsb_ct = 0;//收入_肉松饼计数
int inc_cat_dxm_ct = 0;//收入_点心面计数
int gift_count = 0;//搭赠计数
int gif_cat_xxd_ct = 0;//搭赠_休闲豆计数
int gif_cat_rsb_ct = 0;//搭赠_肉松饼计数
int gif_cat_dxm_ct = 0;//搭赠_点心面计数
int fee_count = 0;//运费计数
// 添加标题
for (int i = 0; i < columnCount; i++) {
colName = rsmd.getColumnName(i + 1);
if("FCustID".equals(colName)){
colName = "客户编号";
label = new Label(i, 0, colName);
}else if("BIGAREA".equals(colName)){
colName = "大区";
label = new Label(i, 0, colName);
}else if("AREAMANAGER".equals(colName)){
colName = "大区经理";
label = new Label(i, 0, colName);
}else if("PROVINCE".equals(colName)){
colName = "省区";
label = new Label(i, 0, colName);
}else if("PROVINCEMANAGER".equals(colName)){
colName = "省区经理";
label = new Label(i,0, colName);
}else if("CLIENT".equals(colName)){
colName = "客户";
label = new Label(i,0, colName);
}else if("SALESMAN".equals(colName)){
colName = "业务员";
label = new Label(i,0, colName);
} else if("DATE".equals(colName)){
colName = "日期";
label = new Label(i, 0, colName);
}else{
if(colName.contains("QUANTITY")){
label = new Label(i, row+1, "数量");
sheet.addCell(label);
}else if(colName.contains("PRICE")){
label = new Label(i, row+1, "单价");
sheet.addCell(label);
}else if(colName.contains("AMOUNT")){
label = new Label(i, row+1, "金额");
sheet.addCell(label);
}
label = new Label(i, row, colName);
}
if(colName.endsWith("0")){
income_count++;
if(colName.startsWith("xxd")){
inc_cat_xxd_ct ++;
}else if(colName.startsWith("rsb")){
inc_cat_rsb_ct ++;
}else{
inc_cat_dxm_ct ++;
}
}else if(colName.endsWith("1")){
gift_count++;
if(colName.startsWith("xxd")){
gif_cat_xxd_ct ++;
}else if(colName.startsWith("rsb")){
gif_cat_rsb_ct ++;
}else{
gif_cat_dxm_ct ++;
}
}else if(colName.endsWith("3")){
fee_count++;
}
log.debug("标题:"+i+"---"+row +"---"+ colName);
sheet.addCell(label);
}
/**
* 单元格合并各参数含义
* mergeCells(startColNum, startRowNum, endColNum, endRowNum);
*/
sheet.mergeCells(0, 0, 0, 3);
sheet.mergeCells(1, 0, 0, 3);
sheet.mergeCells(2, 0, 0, 3);
sheet.mergeCells(3, 0, 0, 3);
sheet.mergeCells(4, 0, 0, 3);
sheet.mergeCells(5 ,0, 0, 3);
sheet.mergeCells(6, 0, 0, 3);
sheet.mergeCells(7, 0, 0, 3);
sheet.mergeCells(8, 0,7+income_count, 0);
label = new Label(8, 0, "销售收入");
sheet.addCell(label);
sheet.mergeCells(8, 1,7+inc_cat_xxd_ct,1);
label = new Label(8,1, "休闲豆");
sheet.addCell(label);
sheet.mergeCells(8+inc_cat_xxd_ct, 1,7+inc_cat_xxd_ct+inc_cat_rsb_ct,1);
label = new Label(8+inc_cat_xxd_ct, 1, "肉松饼");
sheet.addCell(label);
sheet.mergeCells(8+inc_cat_xxd_ct+inc_cat_rsb_ct, 1,7+inc_cat_xxd_ct+inc_cat_rsb_ct+inc_cat_dxm_ct,1);
label = new Label(8+inc_cat_xxd_ct+inc_cat_rsb_ct, 1, "点心面");
sheet.addCell(label);
sheet.mergeCells(8+income_count, 0,7+income_count+gift_count,0);
label = new Label(8+income_count, 0, "搭赠");
sheet.addCell(label);
sheet.mergeCells(8+income_count, 1,7+income_count+gif_cat_xxd_ct, 1);
label = new Label(8+income_count, 1, "休闲豆");
sheet.addCell(label);
sheet.mergeCells(8+income_count+gif_cat_xxd_ct, 1,7+income_count+gif_cat_xxd_ct+gif_cat_rsb_ct,1);
label = new Label(8+income_count+gif_cat_xxd_ct, 1, "肉松饼");
sheet.addCell(label);
sheet.mergeCells(8+income_count+gif_cat_xxd_ct+gif_cat_rsb_ct, 1,7+income_count+gif_cat_xxd_ct+gif_cat_rsb_ct+gif_cat_dxm_ct,1);
label = new Label(8+income_count+gif_cat_xxd_ct+gif_cat_rsb_ct, 1, "点心面");
sheet.addCell(label);
sheet.mergeCells(8+income_count+gift_count, 0,7+income_count+gift_count+fee_count,0);
label = new Label(8+income_count+gift_count, 0, "运费");
sheet.addCell(label);
row += 2;
// row ++;
log.debug("写入标题成功");
while (rs.next()) {
for (int i = 0; i < columnCount; i++) {
label = new Label(i, row, rs.getString(i + 1));
log.debug("行:"+i+"---"+row +"---"+ rs.getString(i+1));
sheet.addCell(label);
}
row++;
}
log.debug("写入内容成功");
// 关闭文件
workbook.write();
workbook.close();
log.info("数据成功写入Excel");
} catch (SQLException e) {
log.debug(e.getMessage());
} catch (RowsExceededException e) {
log.debug(e.getMessage());
} catch (WriteException e) {
log.debug(e.getMessage());
} catch (IOException e) {
log.debug(e.getMessage());
} finally {
try {
workbook.close();
//删除全局临时表
st = (Statement) conn.createStatement();
st.addBatch("if object_id('tempdb..##financial_report_table') is not null DROP TABLE ##financial_report_table;");
st.executeBatch();
conn.commit();
//释放数据库相关链接
ConnUtil.free(null, st, conn);
} catch (Exception e) {
log.debug(e.getMessage());
}
}
}
}
- 浏览: 22587 次
- 来自: 杭州
文章分类
最新评论
发表评论
-
获取sqlserver数据库中临时表的方法
2015-06-02 11:20 837select name from tempdb.sys.col ... -
with as 及 update
2015-04-23 17:38 1228with a as(select a.EmplID, a.Wo ... -
java读写text 典例
2014-03-31 18:27 661import java.io.BufferedReader; ... -
JVM加载class文件的原理 (转)
2013-09-06 13:30 348当Java编译器编译好.class文件之后,我们需 ... -
Java dom4j api 使用集合 (转)
2013-09-06 13:25 602SAXReader reader = new SAXReade ... -
Apache POI HSSFWorkbook 操作excel
2013-07-18 13:46 1126public String exportFunctionAct ...
相关推荐
java调用sqlserver存储过程.pdf
1. 使用不带参数的存储过程 2. 使用带有输入参数的存储过程 3. 使用带有输出参数的存储过程 4. 使用带有返回状态的存储过程 5. 使用带有更新计数的存储过程
Java中调用SQL Server存储过程 Eclipse 3.3
主要介绍了Java中调用SQL Server存储过程详解,本文讲解了使用不带参数的存储过程、使用带有输入参数的存储过程、使用带有输出参数的存储过程、使用带有返回状态的存储过程、使用带有更新计数的存储过程等操作实例,...
Java调用带参数的Oracle 存储过程并返回集合,
SQL Server存储过程示例,在存储工程中Transaction使用示例,游标的使用示例,自定义异常的使用示例,以及Java调用SQL Server存储过程的示例代码。包括JDBC和Spring两种调用方式。
Java调用SQL_Server的存储过程详解 1使用不带参数的存储过程 2使用带有输入参数的存储过程 3使用带有输出参数的存储过程 4 使用带有返回状态的存储过程 5 使用带有更新计数的存储过程
sql server调用存储过程,sql server,存储,过程,存储过程,java,jsp,EL
java调用存储过程,支持获取return值,output返回值,以及查询的表数据,表数据允许有多个查询结果集
java连接SqlServer数据库调用存储过程代码.docx
收录了java调用各种存储过程
Java调用SQLServer的存储过程详解[归类].pdf
游标创建 存储过程创建 程序调用游标 程序调用存储过程
存储过程入门 java中调用sqlserver存储过程
PagingOperation数据库SQLServer2008分页存储过程(和java调用存储过程源代码): 调用存储过程进行分页查询速度快,尤其是数据量超过50万条的表非常重要,此代码是我们项目中使用的,实际使用情况稳定,也希望大家...
oracle sqlserver 共用存储过程
能不能写个动态的业务,只输入存储过程名称,自动获取存储过程参数,并且参数的数据从前台传递过来...只根据输入不同的存储过程名称、参数内容,自动调用不同的存储过程。 已经使用在多个项目中 全开源项目 请放心下载
用Java来调用server的存储过程例子,希望能帮上初学者的忙!
NULL 博文链接:https://sxpujs.iteye.com/blog/376972