using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using Byecity2009.Erp.Data.Report;
using Byecity2009.Erp.BusinessFacade.Report;
using Byecity2009.Erp.BusinessFacade;
namespace Byecity2009.Erp.SharePoint
{
public class RAAllClientReport : UserControl
{
#region [全局變量]
Button btnExcel;
//private string strAddress = @"F:/Byecity Work/Byecity2009/Project Solution/Byecity2009.Erp/Byecity2009.Erp.WebApplication/WordModule";
private string strAddress = @"C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/LAYOUTS/ERPResources";//存放路徑(word模版以及生成的word)--服務器
#endregion
protected void Page_Load(object sender, EventArgs e)
{
btnExcel = (Button)FindControl("btnExcel");
#region [moss裏用Response生成Excel或word以後頁面按鈕失效問題,解決辦法]
string beforeSubmitJS = "/nvar exportRequested = false; /n";
beforeSubmitJS += "var beforeFormSubmitFunction = theForm.onsubmit;/n";
beforeSubmitJS += "theForm.onsubmit = function(){ /n";
beforeSubmitJS += "var returnVal = beforeFormSubmitFunction(); /n";
beforeSubmitJS += "if(exportRequested && returnVal) {_spFormOnSubmitCalled=false; exportRequested=false;} /n";
beforeSubmitJS += "return returnVal; /n";
beforeSubmitJS += "}; /n";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true);
this.btnExcel.Attributes["onclick"] = "javascript:exportRequested=true;";
#endregion
if (!Page.IsPostBack)
{
Page.DataBind();
}
}
#region [屬性]
public AchievementData.RPClientAllAchievementDataTable RpAllClientTable
{
get
{
DateTime dtCountDate = Convert.ToDateTime(string.Format("{0}-{1}-26", DateTime.Now.Year, DateTime.Now.Month));//財務結算時間 如:2010-09-26至2010-09-25
string strWhere = DateTime.Now.Day > 25 ? string.Format(" and CONVERT(varchar(10),OutTeamDate,120) between '{0}' and '{1}' ", dtCountDate.AddMonths(-1), dtCountDate.AddDays(-1)) : string.Format(" and OutTeamDate between '{0}' and '{1}' ", dtCountDate.AddMonths(-2),
dtCountDate.AddMonths(-1).AddDays(-1));
if (!string.IsNullOrEmpty(DateStart) && !string.IsNullOrEmpty(DateEnd))
{
strWhere = string.Format(" and CONVERT(varchar(10),OutTeamDate,120) between '{0}' and '{1}' ", DateStart, DateEnd);
}
else if (!string.IsNullOrEmpty(DateStart))
{
strWhere = string.Format(" and CONVERT(varchar(10),OutTeamDate,120) ='{0}' ", DateStart);
}
strWhere = BFAction.CreateUserDataAction_ETable("00298") + strWhere;
return new BFAchievement().GetClientAllAchievementByWhere(strWhere, " order by CustomType,ProvinceName,CityName, CompanyName,DepartmentName,ClientName");
}
}
#endregion
#region [獲取參數]
//開始時間
public string DateStart
{
get
{
return string.IsNullOrEmpty(Request.QueryString["start"]) ? string.Empty : Request.QueryString["start"];
}
}
//結束時間
public string DateEnd
{
get
{
return string.IsNullOrEmpty(Request.QueryString["end"]) ? string.Empty : Request.QueryString["end"];
}
}
//月
public string Month
{
get
{
return string.IsNullOrEmpty(Request.QueryString["m"]) ? "0" : Request.QueryString["m"];
}
}
//年
public string Year
{
get
{
return string.IsNullOrEmpty(Request.QueryString["y"]) ? "0" : Request.QueryString["y"];
}
}
#endregion
#region [創建Excel]
private void CreateExcel(string path)
{
#region excel表頭
List<string> listTitle = new List<string>();
listTitle.Add("序號");
listTitle.Add("客戶類型");
listTitle.Add("客戶省份");
listTitle.Add("客戶城市");
listTitle.Add("客戶公司");
listTitle.Add("客戶部門");
listTitle.Add("客戶姓名");
listTitle.Add("報名人數");
listTitle.Add("佔位人數");
listTitle.Add("費用人數");
listTitle.Add("實際金額");
listTitle.Add("預占人數");
listTitle.Add("預報人數");
listTitle.Add("取消人數");
listTitle.Add("轉團人數");
listTitle.Add("出籤人數");
listTitle.Add("不走人數");
listTitle.Add("拒籤人數");
#endregion
//請求一個Excel的類
Excel.ApplicationClass excel = null;
Excel._Workbook workbook = null; //工作薄
Excel._Worksheet worksheet = null; //Sheet頁
try
{
excel = new Excel.ApplicationClass();
//要保存的文件名
string FullFileName = path;
object missing = System.Reflection.Missing.Value;
try
{
workbook = excel.Workbooks.Add(true);
int titIndex = 0;
int colIndex = 1; //列
//激活
workbook.Activate();
worksheet = (Excel.Worksheet)workbook.Sheets[1];//指定操作第一個表
worksheet.Name = string.Format("客戶總報表" + "{0:yyyy-MM-dd}", DateTime.Now);
worksheet.Cells.Borders.LineStyle = 1;
worksheet.Columns.AutoFit(); //自動調整大小
worksheet.Cells.Font.Size = 10; //默認大小
worksheet.Cells.RowHeight = 16.5; //行高
//給主表添加數據
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, listTitle.Count]).Merge(missing);
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, listTitle.Count]).Font.Size = 22;
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, listTitle.Count]).RowHeight = 32.25;
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, listTitle.Count]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[1, 1] = "客戶總報表";
worksheet.get_Range(worksheet.Cells[2, 5], worksheet.Cells[2, listTitle.Count]).Merge(true);
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[2, 1]).Font.Bold = true;
worksheet.get_Range(worksheet.Cells[2, 3], worksheet.Cells[2, 3]).Font.Bold = true;
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[2, 1]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[2, 1] = "出團日期"; worksheet.Cells[2, 2] = DateStart;
worksheet.Cells[2, 3] = "至"; worksheet.Cells[2, 4] = DateEnd;
//需要顯示 Title
for (int title = 0; title < listTitle.Count; title++)
{
worksheet.get_Range(worksheet.Cells[3, colIndex], worksheet.Cells[3, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
worksheet.get_Range(worksheet.Cells[3, colIndex], worksheet.Cells[3, colIndex]).Font.Bold = true;
worksheet.Cells[3, colIndex] = listTitle[title].ToString();
colIndex++;
}
//顯示的數據行
for (int i = 0; i < RpAllClientTable.Rows.Count; i++)
{
worksheet.Cells[i + 4, 1] = i + 1;
worksheet.get_Range(worksheet.Cells[i + 4, 1], worksheet.Cells[i + 4, 1]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[i + 4, titIndex + 1] = i + 1; //序號
worksheet.Cells[i + 4, titIndex + 2] = RpAllClientTable[i]["CustomType"].ToString(); //客戶類型
worksheet.Cells[i + 4, titIndex + 3] = RpAllClientTable[i]["ProvinceName"].ToString(); //客戶省份
worksheet.Cells[i + 4, titIndex + 4] = RpAllClientTable[i]["CityName"].ToString(); //客戶城市
worksheet.Cells[i + 4, titIndex + 5] = RpAllClientTable[i]["CompanyName"].ToString(); //客戶公司
worksheet.Cells[i + 4, titIndex + 6] = RpAllClientTable[i]["DepartmentName"].ToString(); //客戶部門
worksheet.Cells[i + 4, titIndex + 7] = RpAllClientTable[i]["ClientName"].ToString(); //客戶名字
worksheet.Cells[i + 4, titIndex + 8] = RpAllClientTable[i]["GuestCount"].ToString(); //客人數量
worksheet.Cells[i + 4, titIndex + 9] = RpAllClientTable[i]["GZW_Count"].ToString(); //佔位人數
worksheet.Cells[i + 4, titIndex + 10] = RpAllClientTable[i]["GFY_Count"].ToString(); //費用人數
worksheet.Cells[i + 4, titIndex + 11] = RpAllClientTable[i]["TotalPrice"].ToString(); //實際金額
worksheet.Cells[i + 4, titIndex + 12] = RpAllClientTable[i]["GYZ_Count"].ToString(); //預占人數
worksheet.Cells[i + 4, titIndex + 13] = RpAllClientTable[i]["GYB_Count"].ToString(); //預報人數
worksheet.Cells[i + 4, titIndex + 14] = RpAllClientTable[i]["GQX_Count"].ToString(); //取消人數
worksheet.Cells[i + 4, titIndex + 15] = RpAllClientTable[i]["GZT_Count"].ToString(); //轉團人數
worksheet.Cells[i + 4, titIndex + 16] = RpAllClientTable[i]["CQ_Count"].ToString(); //出籤人數
worksheet.Cells[i + 4, titIndex + 17] = RpAllClientTable[i]["QCBZ_Count"].ToString(); //簽出不走人數
worksheet.Cells[i + 4, titIndex + 18] = RpAllClientTable[i]["JQ_Count"].ToString(); //拒籤人數
}
worksheet.Application.DisplayAlerts = false; //不顯示提示信息
workbook.SaveAs(FullFileName, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
}
finally
{
// 關閉,釋放
if (workbook != null)
{
workbook.Close(false, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
workbook = null;
worksheet = null;
}
}
}
finally
{
// 關閉,釋放
if (excel != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel.Quit();
ErpCommon.KillExcel(excel);
excel = null;
}
}
}
#endregion
#region [事件]
protected void Button_Command(object sender, CommandEventArgs e)
{
if (e.CommandName.ToUpper() == "EXCEL")//導出Excel
{
if (RpAllClientTable != null)
{
DateTime dateBegin = DateTime.Now;
string strWordPath = strAddress + @"/erpExcel/"; //存放路徑
string strSaveFileName = string.Format("客戶總報表{0:yyyyMMdd}.xls", DateTime.Now); //存放名稱
string strSourcePath = Request.PhysicalApplicationPath;
if (!Directory.Exists(strWordPath))
{
try
{
Directory.CreateDirectory(strWordPath);//word存放路徑
}
catch { }
}
try
{
string strDesFilePath = strWordPath + strSaveFileName;
//創建Excel
CreateExcel(strDesFilePath);
//下載文件
FileInfo DownloadFile = new FileInfo(strFileNewName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
DownloadFile.Delete();
Response.Flush();
Response.End();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
#endregion
}
}
殺掉Excel進程
private void KillExcel(Microsoft.Office.Interop.Excel.Application excel1)
{
//殺掉進程
try
{
excel1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
//釋放COM組件
foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
if (theProc.CloseMainWindow() == false)
{
theProc.Kill();
}
}
excel1 = null;
}
catch {
}
}