protected void button_Click(object sender, ImageClickEventArgs e)
{
string ndate, adate;
string addSql;
string sqlarea; //行政区
string sqlago; //对比以前的参数
int year;
if (ddl_qu.SelectedValue != "")
{
sqlarea = " and 行政区='" + ddl_qu.SelectedValue + "'";
}
else
{
sqlarea = "";
}
year = int.Parse(ddl_nian.Text) - 1;
switch (ddl_ji.Text)
{
case "1":
ndate = " 年份=" + int.Parse(ddl_nian.Text)+ " and 起始月份=1 and 结束月份=3 ";
break;
case "2":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=4 and 结束月份=6 ";
break;
case "3":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=7 and 结束月份=9 ";
break;
case "4":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=10 and 结束月份=12 ";
break;
default:
ndate = " 年份=" + int.Parse(ddl_nian.Text) + "";
break;
}
if (rb_kds.Checked = true)
{
switch (ddl_ji.Text)
{
case "1":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=1 and 结束月份=3 ";
addSql = "and 开单日期 >= '" + ddl_nian.Text + "-1-1' and 开单日期 <= '" + ddl_nian.Text + "-3-31'";
break;
case "2":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=4 and 结束月份=6 ";
addSql = "and 开单日期 >= '" + ddl_nian.Text + "-4-1' and 开单日期 <= '" + ddl_nian.Text + "-6-30'";
break;
case "3":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=7 and 结束月份=9 ";
addSql = "and 开单日期 >= '" + ddl_nian.Text + "-7-1' and 开单日期 <= '" + ddl_nian.Text + "-9-30'";
break;
case "4":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=10 and 结束月份=12 ";
addSql = "and 开单日期 >= '" + ddl_nian.Text + "-10-1' and 开单日期 <= '" + ddl_nian.Text + "-12-30'";
break;
default:
ndate = " 年份=" + int.Parse(ddl_nian.Text) + "";
addSql = "";
break;
}
}
else
{
switch (ddl_ji.Text)
{
case "1":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=1 and 结束月份=3 ";
addSql = "and 到款日期 >= '" + ddl_nian.Text + "-1-1' and 到款日期 <= '" + ddl_nian.Text + "-3-31'";
break;
case "2":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=4 and 结束月份=6 ";
addSql = "and 到款日期 >= '" + ddl_nian.Text + "-4-1' and 到款日期 <= '" + ddl_nian.Text + "-6-30'";
break;
case "3":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=7 and 结束月份=9 ";
addSql = "and 到款日期 >= '" + ddl_nian.Text + "-7-1' and 到款日期 <= '" + ddl_nian.Text + "-9-30'";
break;
case "4":
ndate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=10 and 结束月份=12 ";
addSql = "and 到款日期 >= '" + ddl_nian.Text + "-10-1' and 到款日期 <= '" + ddl_nian.Text + "-12-30'";
break;
default:
ndate = " 年份=" + int.Parse(ddl_nian.Text) + "";
addSql = "";
break;
}
}
if (rb_sqdb.Checked = false)
{
//上期对比
switch (ddl_ji.Text)
{
case "1":
adate = " 年份=" + year + " and 起始月份=10 and 结束月份=12 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + year + "-10-1' and 开单日期<='" + year + "-12-30'";
}
else
{
sqlago = " and 到款日期>='" + year + "-10-1' and 到款日期<='" + year + "-12-30'";
}
break;
case "2":
adate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=1 and 结束月份=3 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + int.Parse(ddl_nian.Text) + "-1-1' and 开单日期<='" + int.Parse(ddl_nian.Text) + "-3-31'";
}
else
{
sqlago = " and 到款日期>='" + int.Parse(ddl_nian.Text) + "-1-1' and 到款日期<='" + int.Parse(ddl_nian.Text) + "-3-31'";
}
break;
case "3":
adate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=4 and 结束月份=6 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + int.Parse(ddl_nian.Text) + "-4-1' and 开单日期<='" + int.Parse(ddl_nian.Text) + "-6-30'";
}
else
{
sqlago = " and 到款日期>='" + int.Parse(ddl_nian.Text) + "-4-1' and 到款日期<='" + int.Parse(ddl_nian.Text) + "-6-30'";
}
break;
case "4":
adate = " 年份=" + int.Parse(ddl_nian.Text) + " and 起始月份=7 and 结束月份=9 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + int.Parse(ddl_nian.Text) + "-7-1' and 开单日期<='" + int.Parse(ddl_nian.Text) + "-9-30'";
}
else
{
sqlago = " and 到款日期>='" + int.Parse(ddl_nian.Text) + "-7-1' and 到款日期<='" + int.Parse(ddl_nian.Text) + "-9-30'";
}
break;
default:
adate = " 年份=" + year + "";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期='" + year + "' '";
}
else
{
sqlago = " and 到款日期='" + year + "'";
}
break;
}
}
else
{
//同期对比
switch (ddl_ji.Text)
{
case "1":
adate = " 年份=" + year + " and 起始月份=1 and 结束月份=3 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + year + "-1-1' and 开单日期<='" + year + "-3-31'";
}
else
{
sqlago = " and 到款日期>=" + year + "-1-1 and 到款日期<='" + year + "-3-31'";
}
break;
case "2":
adate = " 年份=" + year + " and 起始月份=4 and 结束月份=6 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + year + "-4-1' and 开单日期<='" + year + "-6-30'";
}
else
{
sqlago = " and 到款日期>='" + year + "-4-1' and 到款日期<='" + year + "-6-30'";
}
break;
case "3":
adate = " 年份=" + year + " and 起始月份=7 and 结束月份=9 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + year + "-7-1' and 开单日期<='" + year + "-9-30'";
}
else
{
sqlago = " and 到款日期>='" + year + "-7-1' and 到款日期<='" + year + "-9-30'";
}
break;
case "4":
adate = " 年份=" + year + " and 起始月份=10 and 结束月份=12 ";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>='" + year + "-10-1' and 开单日期<='" + year + "-12-30'";
}
else
{
sqlago = " and 到款日期>='" + year + "-10-1' and 到款日期<='" + year + "-12-30'";
}
break;
default:
adate = " 年份=" + year + "";
if (rb_kds.Checked = true)
{
sqlago = " and 开单日期>=" + year + " and 开单日期<='" + year + "'";
}
else
{
sqlago = " and 到款日期>= " + year + " and 到款日期<='" + year + "'";
}
break;
}
}
//DHTDataGrid21.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection("server=(local);database=环保;UID=sa;PWD=adm");
conn.Open();
string strSql = "select 行政区,sum(总金额) as 金额 "
+ " into #now"
+ " from 计费 "
+ " where " + ndate + "" + sqlarea + "" + addSql + ""
+ " group by 行政区"
+ " select 行政区,sum(总金额) as 金额"
+ " into #ago"
+ " from 计费"
+ " where " + adate + "" + sqlarea + "" + sqlago + ""
+ " group by 行政区"
+ " select n.行政区,n.金额,增长百分比=case "
+ " when isnull(a.金额,0)=0 then '以前没值'"
+ " else"
+ " ltrim(cast(((isnull(n.金额,0)-isnull(a.金额,0))*100/isnull(a.金额,0))AS decimal(9,2)))+'%'"
+ " end"
+ " into #all"
+ " from #now as n left outer join #ago as a"
+ " on n.行政区=a.行政区"
+ " select 行政区=case"
+ " when 行政区='4404' then '1"
+ " when 行政区='4405' then '2"
+ " when 行政区='4406' then '3"
+ " when 行政区='4407' then '4"
+ " when 行政区='4408' then '5"
+ " else ''"
+ " end,金额,增长百分比,xh=0 from #all"
+ " union "
+ " select 行政区='合计',金额=sum(q.金额),增长百分比=case"
+ " when isnull(sum(a.金额),0)=0 then '以前没值'"
+ " else"
+ " ltrim(cast(((sum(n.金额)-sum(a.金额))*100/sum(a.金额))AS decimal(9,2)))+'%' "
+ " end,xh=1"
+ " from #all as q,#now as n,#ago as a"
+ " order by xh";
// SqlCommand com = new SqlCommand("au_contrast", conn);
//com.CommandType = CommandType.StoredProcedure;
//添加一个参数,并指出其类型
//com.Parameters.Add("@nyear", SqlDbType.Int);
//com.Parameters["@nyear"].Value = int.Parse(ddl_nian.Text).ToString(); //给参数@nyear赋值
//com.Parameters.Add("@area",SqlDbType.VarChar);
//com.Parameters["@area"].Value = ddl_qu.SelectedValue;
//com.Parameters.Add("@nsmonth",SqlDbType.Int);
//com.Parameters.Add("@nemonth",SqlDbType.Int);
//SqlDataAdapter sda = new SqlDataAdapter();
//sda.SelectCommand = com;
//com.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
conn.Close();
//DHTDataGrid21.QueryCommand = strSql;
//DHTDataGrid21.DCPageSize = 17;
//DHTDataGrid21.GridHeight = 598;
//string[,] ary = new string[,] {
// {"bind","行政区","行政区","23","left"},
// {"temp","金额","金额","10","left"},
// //{"temp","百分比","增长百分比","12","left"},
//};
//DHTDataGrid21.DCBindColumn = ary;
xlsGridview(ds, "data"); //调用xlsGridview方法生成Excel报表
}
/**/
/// <summary>
/// 绑定数据库生成XLS报表
/// </summary>
/// <param name="ds">获取DataSet数据集</param>
/// <param name="xlsName">报表表名</param>
private void xlsGridview(DataSet ds, string xlsName)
{
XlsDocument xls = new XlsDocument();
Response.Clear();
Response.Buffer = true;
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("排污费对比分析", System.Text.Encoding.UTF8) + ".xls\"");
Response.ContentType = "Application/ms-excel";
int rowIndex = 3;
int colIndex = 0;
string area;
if (ddl_qu.Text != "")
{
area = ddl_qu.SelectedItem +"区";
}
else
{
area = "++市";
}
System.Data.DataTable table = ds.Tables[0];
Worksheet sheet = xls.Workbook.Worksheets.AddNamed(""+ddl_nian.Text+"年"+ddl_ji.Text+"季度查");//状态栏标题名称
Cells cells = sheet.Cells;
//将一列的宽设为0
ColumnInfo info = new ColumnInfo(xls, sheet);
info.ColumnIndexStart = 3;
info.ColumnIndexEnd = 3;
info.Width = 0 * 256;
sheet.AddColumnInfo(info);
MergeArea meaA = new MergeArea(1, 1, 1, 4);//一个合并单元格实例(合并第一行 第二列 到 第一行 第四列)
sheet.AddMergeArea(meaA);//填加合并单元格
XF cellXF = xls.NewXF();
cellXF.VerticalAlignment = VerticalAlignments.Centered;
cellXF.Font.Height = 24 * 12;
cellXF.Font.Bold = true;
cellXF.Pattern = 1;//设定单元格填充风格。如果设定为0,则是纯色填充
cellXF.PatternBackgroundColor = Colors.EgaYellow;//填充的底色
cellXF.PatternColor = Colors.DarkGreen;//设定填充线条的颜色
Cell cell1 = cells.Add(1, 1, ""+area+" " + ddl_nian.Text + "年" + ddl_ji.Text + "季度排污对比分析", cellXF);
cell1.HorizontalAlignment = HorizontalAlignments.Centered;
foreach (DataColumn col in table.Columns)
{
colIndex++;
//sheet.Cells.AddValueCell(1,colIndex,col.ColumnName);//添加XLS标题行
Cell cell= cells.AddValueCell(3, colIndex, col.ColumnName);
cell.Font.Bold = true; //字体为粗体
cell.HorizontalAlignment = HorizontalAlignments.Centered;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
//sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里
//Cell cell = cells.AddValueCell(rowIndex, colIndex, Convert.ToDouble(row[col.ColumnName].ToString()));//转换为数字型
Cell cell = cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//转换为数字型
//如果你数据库里的数据都是数字的话 最好转换一下,不然导入到Excel里是以字符串形式显示。
cell.Font.FontFamily = FontFamilies.Roman; //字体
//cell.Font.Bold = true; //字体为粗体
}
}
xls.Send();
}
全都贴出来了。请大家看看有什么地方需要改的。。。多多指教!