using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using log4net;
using System.Web.Security;
using OT.Controls.Toolbar;
namespace OT.Web.Ap_Code
{
public class BasePage : System.Web.UI.Page
{
#region 定義全局變量
public static string gstrUserDptID; //登入者用戶部門
public static string gstrOrganizationID; //組織編號
public string gstrUserID; //登入者用戶ID
public static string gstrUserRoleId; //登入者所在的角色Id 一個用戶會對應多種角色
public string gstrUserGuid; //登入者的Guid
public string gstrGroupGuid; //登入者所在單位的Guid
public string gstrUserGroupId; //登入者所在單位(部門)
public string gstrUserDocOrCFO; //登入者如果是Doc:文管部門,CFO:財務部門
public string gstrProgramId; //程式代號
public string gstrProgramName; //程式名稱
public string gstrProgramTableName; //程式所使用的主TABLE (山富要求)
public string gstrUICulture; //頁面多語言
public string gstrLocalPath; //項目的物理路徑
public string gstrVirtualPath; //項目的虛擬路徑
public DateTime gdteUserLoginTime; //登入時間
public static DateTime gdteNowTime = DateTime.Now; //當前系統DB時間 new OT.BLL.CommonClass().GetDate()
public static string gstrUploadFilePath = "Upload"; //檔案上傳路徑
public static string gstrUploadFilePathPage; //頁面區塊上傳的文件夾,先放在DMImg內
public static int gintGirdViewPageSize = 15; //GridView分頁筆數
public static int gintGirdViewPageButtonSize = 10; //GridView分頁頁碼顯示數量
public static DataTable gdtProgramList; //系統所有程式列表
public static string gstrUploadType = "jpg|gif|bmp|tif|png"; //圖示上傳類型
public static string gstrDocNo; //文管部門EIP編號
public static string gstrCFONo; //財務部門EIP編號
public static int gintUploadSize = 2; //檔案上傳大小*MB
public static int gintUploadCloseFileCount = 30; //檔案上傳大小*MB
public static int gintUploadCloseImgSize = 5; //檔案上傳大小*MB
public static int gintUploadCloseImgCount = 30; //檔案上傳大小*MB
public static string gstrStartTime = "9";
public static string gstrEndTime = "22";
public bool gbl_AllowView; //檢視權限
public bool gbl_AllowAdd; //新增權限
public bool gbl_AllowReAdd; //儲存後新增權限 增加儲存後新增權限 Add by ted 2014/11/14
public bool gbl_AllowCopy; //複製權限 增加複製權限 Add by ted 2014/11/14
public bool gbl_AllowEdit; //修改權限
public bool gbl_AllowDel; //刪除權限
public bool gbl_AllowPrint; //列印權限
public bool gbl_AllowExport; //匯出權限
public bool gbl_AllowQuery; //查詢權限
public bool gbl_AllowSub; //送審權限
public bool gbl_AllowChk; //審核權限
public bool gbl_AllowUpd; //查詢權限
public bool gbl_AllowMail; //發送Mail的權限
public bool gbl_AllowRpt; //報表的最高權限
public string gbl_ProgramStatus; //程式的狀態
public static string[] garyAllowRight = new string[]{ "Add", "ReAdd", "Save", "Del"
, "Clear", "Upd", "Qry", "Exp"
, "Imp", "Leave", "Cpy"
, "Sub", "Chk", "Tmp", "Print"
,"View","RptAd"}; //系統所有可用權限
public static DataTable dtAllowRightText = new DataTable();
public static readonly ILog mo_Log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//記錄異常
#endregion
#region 定義頁面變量
#endregion
#region 定義屬性區域
#endregion
#region 公有方法區域
#region DateDiff
///
/// 取得時間差(小時),超過y
///
///
///
///
public int fnDateDiff(DateTime DateTime1, DateTime DateTime2)
{
int dateDiff = 0;
TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime1.ToString("yyyy/MM/dd")).Ticks);
TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(DateTime2.ToString("yyyy/MM/dd")).Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
dateDiff = ts.Days + 1;
return dateDiff;
}
#endregion
#region GroupRows
///
/// 合併GridView欄位相同的行
///
/// GridView物件
/// 需要合併欄位的索引
public void fnGroupRows(GridView GridView1, int cellNum)
{
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr = GridView1.Rows[i];
for (++i; i < GridView1.Rows.Count; i++)
{
GridViewRow gvrNext = GridView1.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}
if (i == GridView1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
#endregion
#region fnGetRightText
public string fnGetRightText(string strRightValue)
{
string strReturn = string.Empty;
switch (strRightValue.Trim())
{
case "Add":
strReturn = "新增";
break;
case "ReAdd":
strReturn = "儲存後新增";
break;
case "Save":
strReturn = "儲存";
break;
case "Del":
strReturn = "刪除";
break;
case "Clear":
strReturn = "清除";
break;
case "Upd":
strReturn = "修改";
break;
case "Qry":
strReturn = "查詢";
break;
case "Exp":
strReturn = "匯出";
break;
case "Imp":
strReturn = "匯入";
break;
case "Leave":
strReturn = "離開";
break;
case "Cpy":
strReturn = "複製";
break;
case "Sub":
strReturn = "送審";
break;
case "Chk":
strReturn = "審核";
break;
case "Tmp":
strReturn = "新增範例";
break;
case "Print":
strReturn = "列印";
break;
case "View":
strReturn = "檢視";
break;
case "RptAd":
strReturn = "報表管理";
break;
}
return strReturn;
}
#endregion
#region fnDataSetToDataTable
///
/// 返回DataSet中第一個DataTable
///
/// 需要轉換的DataSet
///
public static DataTable fnDataSetToDataTable(DataSet ds)
{
DataTable dt = new DataTable();
if (ds != null && ds.Tables.Count > 0)
{
dt = ds.Tables[0];
}
return dt;
}
#endregion
#region fnDataSetToDataTable
///
/// 返回DataSet中第一個DataTable
///
/// 需要轉換的DataSet
/// Table的名稱
///
public static DataTable fnDataSetToDataTable(DataSet ds, string strTableName)
{
DataTable dt = new DataTable();
if (ds != null && ds.Tables.Count > 0 && ds.Tables[strTableName] != null)
{
dt = ds.Tables[strTableName];
}
return dt;
}
#endregion
#region fnExportToOfficeFile
///
/// 將GridView內容以HTML存成Excel、Word檔案,儲存後內容含格式資訊
/// 須設定頁面EnableEventValidation="false"
///
/// GridView物件名稱
/// 包含Excel標題的物件名稱,註:不能含有Button
/// 存檔檔名
/// 來源Page頁面(ex:Page)
///
/// 程式做法:
/// 利用GridView自行Render出與網頁上格式相同的HTML,
/// 配合application/vnd.xls MIME Type,讓資料可以直接在Excel、Word中重現
/// 註:
/// 使用此 Function 須 using System.Web
///
public void fnExportToOfficeFile(System.Web.UI.WebControls.GridView vGridView, System.Web.UI.HtmlControls.HtmlTable vHtmlTable, string vFileName, System.Web.UI.Page vPage)
{
if (vGridView.Rows.Count == 0)
{
ShowArtAlert("A00072"); //無資料匯出
return;
}
//關閉換頁跟排序
//說明:若GridView無分頁功能、或有啟用分頁但只有1頁的資料時,則直接將資料Render出來
//若有啟用分頁功能且頁數有2頁以上,當DataSource為null時,則不進行轉換為Excel的動作
//if (vGridView.AllowPaging && vGridView.DataSource != null)
//{
//vGridView.AllowSorting = false;
//vGridView.AllowPaging = false;
//vGridView.DataBind();
//}
//瀏覽器為IE時將檔名編碼,避免存檔後中文檔名會變亂碼
string strFileName = string.Empty;
if (HttpContext.Current.Request.Browser.Browser == "IE")
{
strFileName = HttpUtility.UrlPathEncode(vFileName);
}
else
{
strFileName = vFileName;
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=utf-8");
//註:若中文字會變亂碼,則替換下面的程式試試
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Write("
")
//HttpContext.Current.Response.Charset = "big5";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//建立假HtmlForm避免以下錯誤:
//Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
//另一種做法是建立 override VerifyRenderingInServerForm function,在function內不寫程式碼
//這樣就可以直接執行 GridView1.RenderControl(htw)
//出現上面的錯誤在Page添加 EnableEventValidation="false"
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
vPage.Controls.Add(hf);
vHtmlTable.RenderControl(htw);
hf.Controls.Add(vGridView);
hf.RenderControl(htw);
string strHtml = sw.ToString().Replace("
", "").Replace("
", "");
int iS = strHtml.IndexOf("", iS);
strHtml = strHtml.Substring(0, iS) + strHtml.Substring(iE + 2, strHtml.Length - iE - 2);
}
//System.Web.UI.HtmlControls.HtmlMeta meta = new System.Web.UI.HtmlControls.HtmlMeta();
//meta. = "";
string strMeta = "";
strHtml = strMeta + strHtml;
HttpContext.Current.Response.Write(strHtml);
HttpContext.Current.Response.End();
}
#endregion
#region fnExportToOfficeFile
///
/// 將GridView內容以HTML存成Excel、Word檔案,儲存後內容含格式資訊
/// 須設定頁面EnableEventValidation="false"
///
/// GridView物件名稱
/// 包含Excel標題的物件名稱,註:不能含有Button
/// 存檔檔名
/// 來源Page頁面(ex:Page)
///
/// 程式做法:
/// 利用GridView自行Render出與網頁上格式相同的HTML,
/// 配合application/vnd.xls MIME Type,讓資料可以直接在Excel、Word中重現
/// 註:
/// 使用此 Function 須 using System.Web
///
public void fnExportToOfficeFile(System.Web.UI.WebControls.GridView vGridView, System.Web.UI.HtmlControls.HtmlTable vHtmlTableHead, System.Web.UI.HtmlControls.HtmlTable vHtmlTableFoot, string vFileName, System.Web.UI.Page vPage)
{
if (vGridView.Rows.Count == 0)
{
ShowArtAlert("A00072"); //無資料匯出
return;
}
//關閉換頁跟排序
//說明:若GridView無分頁功能、或有啟用分頁但只有1頁的資料時,則直接將資料Render出來
//若有啟用分頁功能且頁數有2頁以上,當DataSource為null時,則不進行轉換為Excel的動作
if (vGridView.AllowPaging && vGridView.DataSource != null)
{
vGridView.AllowSorting = false;
vGridView.AllowPaging = false;
vGridView.DataBind();
}
//瀏覽器為IE時將檔名編碼,避免存檔後中文檔名會變亂碼
string strFileName = string.Empty;
if (HttpContext.Current.Request.Browser.Browser == "IE")
{
strFileName = HttpUtility.UrlPathEncode(vFileName);
}
else
{
strFileName = vFileName;
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=big5");
//註:若中文字會變亂碼,則替換下面的程式試試
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Charset = "big5";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//建立假HtmlForm避免以下錯誤:
//Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
//另一種做法是建立 override VerifyRenderingInServerForm function,在function內不寫程式碼
//這樣就可以直接執行 GridView1.RenderControl(htw)
//出現上面的錯誤在Page添加 EnableEventValidation="false"
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
vPage.Controls.Add(hf);
vHtmlTableHead.RenderControl(htw);
hf.Controls.Add(vGridView);
hf.RenderControl(htw);
vHtmlTableFoot.RenderControl(htw);
string strHtml = sw.ToString().Replace("
", "").Replace("
", "");
int iS = strHtml.IndexOf("", iS);
strHtml = strHtml.Substring(0, iS) + strHtml.Substring(iE + 2, strHtml.Length - iE - 2);
}
HttpContext.Current.Response.Write(strHtml);
HttpContext.Current.Response.End();
}
#endregion
#region fnExportToOfficeFile
///
/// 將GridView內容以HTML存成Excel、Word檔案,儲存後內容含格式資訊
/// 須設定頁面EnableEventValidation="false"
///
/// GridView物件名稱
/// 包含Excel標題的物件名稱,註:不能含有Button
/// 存檔檔名
/// 來源Page頁面(ex:Page)
///
/// 程式做法:
/// 利用GridView自行Render出與網頁上格式相同的HTML,
/// 配合application/vnd.xls MIME Type,讓資料可以直接在Excel、Word中重現
/// 註:
/// 使用此 Function 須 using System.Web
///
public void fnExportToOfficeFile(System.Web.UI.WebControls.GridView vGridView, string vFileName, System.Web.UI.Page vPage)
{
if (vGridView.Rows.Count == 0)
{
ShowArtAlert("A00072"); //無資料匯出
return;
}
//關閉換頁跟排序
//說明:若GridView無分頁功能、或有啟用分頁但只有1頁的資料時,則直接將資料Render出來
//若有啟用分頁功能且頁數有2頁以上,當DataSource為null時,則不進行轉換為Excel的動作
if (vGridView.AllowSorting && vGridView.DataSource != null)
{
vGridView.AllowSorting = false;
vGridView.DataBind();
}
//瀏覽器為IE時將檔名編碼,避免存檔後中文檔名會變亂碼
string strFileName = string.Empty;
if (HttpContext.Current.Request.Browser.Browser == "IE")
{
strFileName = HttpUtility.UrlPathEncode(vFileName);
}
else
{
strFileName = vFileName;
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=big5");
//HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=utf-8");
//註:若中文字會變亂碼,則替換下面的程式試試
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
//HttpContext.Current.Response.Write("")
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Charset = "big5";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//建立假HtmlForm避免以下錯誤:
//Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
//另一種做法是建立 override VerifyRenderingInServerForm function,在function內不寫程式碼
//這樣就可以直接執行 GridView1.RenderControl(htw)
//出現上面的錯誤在Page添加 EnableEventValidation="false"
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
vPage.Controls.Add(hf);
hf.Controls.Add(vGridView);
hf.RenderControl(htw);
string strHtml = sw.ToString().Replace("
", "").Replace("
", "");
int iS = strHtml.IndexOf("", iS);
strHtml = strHtml.Substring(0, iS) + strHtml.Substring(iE + 2, strHtml.Length - iE - 2);
}
HttpContext.Current.Response.Write(strHtml);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
#endregion
#region fnExportToOfficeFile
///
/// 將GridView內容以HTML存成Excel、Word檔案,儲存後內容含格式資訊
/// 須設定頁面EnableEventValidation="false"
///
/// GridView物件名稱
/// 包含Excel標題的物件名稱,註:不能含有Button
/// 存檔檔名
/// 來源Page頁面(ex:Page)
///
/// 程式做法:
/// 利用GridView自行Render出與網頁上格式相同的HTML,
/// 配合application/vnd.xls MIME Type,讓資料可以直接在Excel、Word中重現
/// 註:
/// 使用此 Function 須 using System.Web
///
public void fnExportToOfficeFile(System.Web.UI.HtmlControls.HtmlTable vHtmlTable, string vFileName, System.Web.UI.Page vPage)
{
if (vHtmlTable.Rows.Count == 0)
{
ShowArtAlert("A00072"); //無資料匯出
return;
}
//瀏覽器為IE時將檔名編碼,避免存檔後中文檔名會變亂碼
string strFileName = string.Empty;
if (HttpContext.Current.Request.Browser.Browser == "IE")
{
strFileName = HttpUtility.UrlPathEncode(vFileName);
}
else
{
strFileName = vFileName;
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=big5");
//註:若中文字會變亂碼,則替換下面的程式試試
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Charset = "big5";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//建立假HtmlForm避免以下錯誤:
//Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
//另一種做法是建立 override VerifyRenderingInServerForm function,在function內不寫程式碼
//這樣就可以直接執行 GridView1.RenderControl(htw)
//出現上面的錯誤在Page添加 EnableEventValidation="false"
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
vPage.Controls.Add(hf);
hf.Controls.Add(vHtmlTable);
//vHtmlTable.RenderControl(htw);
hf.RenderControl(htw);
//string strCSSUrl = GetRootURI() + "/App_Themes/Default/ReportStyle.css";
//string strCss = @"";
//strCss = fnReadFully(strCSSUrl);
//HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write(sw.ToString().Replace("
", "").Replace("
", ""));
HttpContext.Current.Response.End();
}
#endregion
#region fnExportToOfficeFile
///
/// 將GridView內容以HTML存成Excel、Word檔案,儲存後內容含格式資訊
/// 須設定頁面EnableEventValidation="false"
///
/// GridView物件名稱
/// 包含Excel標題的物件名稱,註:不能含有Button
/// 存檔檔名
/// 來源Page頁面(ex:Page)
/// 文字欄位(ex:例如報表第一列和第四列為文字類型,則傳"0;3;")
///
/// 程式做法:
/// 利用GridView自行Render出與網頁上格式相同的HTML,
/// 配合application/vnd.xls MIME Type,讓資料可以直接在Excel、Word中重現
/// 註:
/// 使用此 Function 須 using System.Web
///
public void fnExportToOfficeFile(System.Web.UI.WebControls.GridView vGridView, string vFileName, System.Web.UI.Page vPage, string strStringColIndexs)
{
if (vGridView.Rows.Count == 0)
{
ShowArtAlert("A00072"); //無資料匯出
return;
}
//關閉換頁跟排序
//說明:若GridView無分頁功能、或有啟用分頁但只有1頁的資料時,則直接將資料Render出來
//若有啟用分頁功能且頁數有2頁以上,當DataSource為null時,則不進行轉換為Excel的動作
if (vGridView.AllowPaging && vGridView.DataSource != null)
{
vGridView.Attributes.Add("StringColIndexs", strStringColIndexs);
vGridView.RowDataBound += new GridViewRowEventHandler(vGridView_RowDataBound);
vGridView.AllowSorting = false;
vGridView.AllowPaging = false;
vGridView.DataBind();
}
//瀏覽器為IE時將檔名編碼,避免存檔後中文檔名會變亂碼
string strFileName = string.Empty;
if (HttpContext.Current.Request.Browser.Browser == "IE")
{
strFileName = HttpUtility.UrlPathEncode(vFileName);
}
else
{
strFileName = vFileName;
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=big5");
//註:若中文字會變亂碼,則替換下面的程式試試
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Charset = "big5";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//建立假HtmlForm避免以下錯誤:
//Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
//另一種做法是建立 override VerifyRenderingInServerForm function,在function內不寫程式碼
//這樣就可以直接執行 GridView1.RenderControl(htw)
//出現上面的錯誤在Page添加 EnableEventValidation="false"
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
vPage.Controls.Add(hf);
hf.Controls.Add(vGridView);
hf.RenderControl(htw);
string strHtml = sw.ToString().Replace("
", "").Replace("
", "");
int iS = strHtml.IndexOf("", iS);
strHtml = strHtml.Substring(0, iS) + strHtml.Substring(iE + 2, strHtml.Length - iE - 2);
}
HttpContext.Current.Response.Write(strHtml);
HttpContext.Current.Response.End();
}
#endregion
#region fnExportToOfficeFile
///
/// 將GridView內容以HTML存成Excel、Word檔案,儲存後內容含格式資訊
/// 須設定頁面EnableEventValidation="false"
///
/// GridView物件名稱
/// 包含Excel標題的物件名稱,註:不能含有Button
/// 存檔檔名
/// 來源Page頁面(ex:Page)
/// 文字欄位(ex:例如報表第一列和第四列為文字類型,則傳"0;3;")
///
/// 程式做法:
/// 利用GridView自行Render出與網頁上格式相同的HTML,
/// 配合application/vnd.xls MIME Type,讓資料可以直接在Excel、Word中重現
/// 註:
/// 使用此 Function 須 using System.Web
///
public void fnExportToOfficeFile(System.Web.UI.WebControls.GridView vGridView, System.Web.UI.HtmlControls.HtmlTable vHtmlTable, string vFileName, System.Web.UI.Page vPage, string strStringColIndexs)
{
if (vGridView.Rows.Count == 0)
{
ShowArtAlert("A00072"); //無資料匯出
return;
}
//關閉換頁跟排序
//說明:若GridView無分頁功能、或有啟用分頁但只有1頁的資料時,則直接將資料Render出來
//若有啟用分頁功能且頁數有2頁以上,當DataSource為null時,則不進行轉換為Excel的動作
//if (vGridView.AllowPaging && vGridView.DataSource != null)
//{
//vGridView.AllowSorting = false;
//vGridView.AllowPaging = false;
//vGridView.DataBind();
//}
if (vGridView.DataSource != null)
{
vGridView.Attributes.Add("StringColIndexs", strStringColIndexs);
vGridView.RowDataBound += new GridViewRowEventHandler(vGridView_RowDataBound);
vGridView.AllowSorting = false;
vGridView.AllowPaging = false;
vGridView.DataBind();
}
//瀏覽器為IE時將檔名編碼,避免存檔後中文檔名會變亂碼
string strFileName = string.Empty;
//新增判斷 by Gary 2013/12/30
if (HttpContext.Current.Request.Browser.Browser == "IE" || HttpContext.Current.Request.Browser.Browser == "InternetExplorer")
{
strFileName = HttpUtility.UrlPathEncode(vFileName);
}
else
{
strFileName = vFileName;
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.AddHeader("mata", "http-equiv=Content-Type content=text/html;charset=utf-8");
//註:若中文字會變亂碼,則替換下面的程式試試
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Write("")
//HttpContext.Current.Response.Charset = "big5";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//建立假HtmlForm避免以下錯誤:
//Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
//另一種做法是建立 override VerifyRenderingInServerForm function,在function內不寫程式碼
//這樣就可以直接執行 GridView1.RenderControl(htw)
//出現上面的錯誤在Page添加 EnableEventValidation="false"
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
vPage.Controls.Add(hf);
vHtmlTable.RenderControl(htw);
hf.Controls.Add(vGridView);
hf.RenderControl(htw);
string strHtml = sw.ToString().Replace("