[C#][WebForm] 將Calendar每日資料填入Office Word表格

  • 5068
  • 0

抓Calendar資料,連接Office Word

Word可先設計好畫面
利用程式把填資料入Word,做制式表單供工列印使用

Word 範本大概長這樣:
1.標題
2.子標題
  ______________
︱____XXXX年O月____︱
︱ S︱ M︱ T︱ W︱ T︱ F︱ S︱
︱_︱_︱_︱_︱_︱_︱_︱
︱_︱_︱_︱_︱_︱_︱_︱
︱_︱_︱_︱_︱_︱_︱_︱
︱_︱_︱_︱_︱_︱_︱_︱
︱_︱_︱_︱_︱_︱_︱_︱
          Table 1.圖表名稱

/*====Main.aspx====*/
using Novacode;

public static DataTable 各日事件表 { get; set;}

protected void Calendar1_PreRender(object sender, EventArgs e)
{
	//在Calendar自動給日期前,先清空各日事件表
	各日事件表 = new DataTable();
	各日事件表.Columns.Add(new DataColumn("日期", System.Type.GetType("System.String")));
	各日事件表.Columns.Add(new DataColumn("部門", System.Type.GetType("System.String")));
	各日事件表.Columns.Add(new DataColumn("件數", System.Type.GetType("System.String")));
	各日事件表.Columns.Add(new DataColumn("年", System.Type.GetType("System.String")));
	各日事件表.Columns.Add(new DataColumn("月", System.Type.GetType("System.String")));
}

protected void Calendar1_DayRender1(object sender, EventArgs e)
{
	ArrayList al = this.取得某日各單位案件數(e.Day.Date.ToShortDateString());
	DataRow dr = null;

	//Render時將資料一併加入至暫存表
	if (al.Count == 0)
	{
		dr = 各日事件表.NewRow();
		dr[0] = e.Day.Date.Day.ToString();//日期
		dr[1] = "";//名稱
		dr[2] = "";//件數
		dr[3] = e.Day.Date.Year.ToString() + "年";//年
		dr[4] = e.Day.Date.Month.ToString() + "月";//月
		各日事件表.Rows.Add(dr);
	}
	foreach (string[] objs in al)
	{
		dr = 各日事件表.NewRow();
		dr[0] = e.Day.Date.Day.ToString();//日期
		dr[1] = objs[0];//名稱
		dr[2] = objs[1];//件數
		dr[3] = e.Day.Date.Year.ToString() + "年";//年
		dr[4] = e.Day.Date.Month.ToString() + "月";//月
		//一天可能會有多單位有多案件,美讀取到一個就先加入至DataTable
		各日事件表.Rows.Add(dr);
	}
}

protected void brnPrint_Click(object sender, EventArgs e)
{
	string FilePath = @"E:\abc\Test.docx";
	using(DocX PrintDocx = DocX.Load(FilePath))
	{
		//====抓Word檔裡的Table
		Novacode.Table t = PrintDocx.Tables[0];

		//====寫入Title年月
		//因Calendar上無法抓月份標題,以該分頁20號之年月為該分頁期別
		DataRow[] 期別 = 各日事件表.Select("日期 = 20");
		t.Rows[0].Cells[0].Paragraphs.First().Append(期別[0][3].ToString()+期別[0][4].ToString());

		//====迴圈填入資料====
		//Calendar該分頁起始的第一天
		string 上比日期 = 各日事件表.Rows[0][0].ToString();
		int DayCount = 0;
		int 排列Count = 0;
		string 日期 = "";
		string 事件 = "";
		string 目前日期 = DateTime.Today.ToString("yyyy年MM月dd日");
		foreach ( DataRow row in 各日事件表.Rows )
		{
			//將日期為目前時間的Cell底色改成其他顏色
			string 月曆日期 = row[4].ToString() + 上筆日期 + "日";
			int MonIndex = 目前日期.IndexOf(月曆日期);
			if (MonIndex >= 0)
			{
				t.Rows[DayCount/7+2].Cells[DayCount % 7].FillColor = Color.LightYellow;
			}

			//資料判讀
			if (row[0].ToString() == 上筆日期)
			{
				日期 = row[0].ToString() + "\n";
				if ( row[1].ToString() !="" || row[2].ToString != "")
				{
					事件 = 事件 + row[1].ToString() + ":" + row[2].ToString() + "件\n";
					//Cell內若單筆資料不換列,雙筆再換列(刪除尾端"\n",改成", ")
					if ( 排列Count % 2 == 0)
					{
						事件 = 事件.TrimEnd("\n".ToCharArray()) + ", ";
					}
					排列Count++;
				}
				else
				{
					事件 = "";
				}				
			}
			else if (row[0].ToString() != 上筆日期)
			{
				排列Count = 0;
				//若偵測到不同日期,先將累積的資料填入對應的Cell
				事件 = 事件.TrimEnd(", ".ToCharArray());
				t.Rows[DayCount / 7 + 2].Cells[DayCount % 7].Paragraphs.First().Append(日期 + 事件.TrimEnd("\n".ToCharArray())).FontSize(8).Bold();

				//紀錄此筆資料日期、事件
				日期 = row[0].ToString() + "\n";
				if ( row[1].ToString() !="" || row[2].ToString != "")
				{
					事件 = 事件 + row[1].ToString() + ":" + row[2].ToString() + "件\n";					
					排列Count++;
				}
				else
				{
					事件 = "";
				}		
				//計數器標記天數+1
				DayCount++;
			}
			//將此筆日期取代上筆日期,供下筆資料對照
			上筆日期 = row[0].ToString();			
		}
		//迴圈跑完後寫入最後一筆的資料
		t.Rows[DayCount / 7 + 2].Cells[DayCount & 7].Paragraphs.First().Append(日期 + 事件.TrimEnd("\n".ToCharArray())).FontSize(8).Bold();

		//====另存新檔====
		string 暫存檔名稱 = Server.MapPath("~/Temp/" + "每日採購按鍵執行情況" + ".docx");
		//PrintDocx.SaveAs(@"D:\abc\test.docx");//test
		PrintDocx.DaveAs(暫存檔名稱);
		Response.Redirect("FileDownloader.ashx?File=/Temp/"+暫存檔名稱.Split('\\').Last());

	}
}


/*====FileDownloader.ashx====*/
using System.Web;
using System.IO;
using System.Text;

public class FileDownloader : IHttpHandler
{
	public void ProcessRequest(HttpContext context)
	{
		Encoding Big5 = Encoding.GetEncoding("big5");
		//先指定編碼,以免產生亂碼
		context.Response.ContentEncoding = Big5;
		context.Response.HeaderEncoding = Big5;
		context.Request.ContentEncoding = Big5;
		
		string[] temp = context.Request.Url.ToString().Split('=');
		string FileNameWithPath = temp[temp.Length -1];
		string FileFullPath = context.Server.MapPath(".") + FileNameWithPath;
		string[] temp1 = FileNameWithPath.Split('/');
		string FileNameWithoutPath = temp1[temp1.length -1];
		if (!File.Exists(FileFullPath))
		{
			context.Response.Write("檔案不存在於" + FileFullPath);
			return;
		}
		context.Response.ContentType = "application/octet-stream";
		context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileNameWithoutPath);
		context.Response.WriteFile(FileFullPath);
		//Flush才是真正將檔案丟到前端供下載
		context.Response.Flush();
		File.Delete(FileFullPath);
	}
	public bool IsReusable
	{
		get
		{
			return false;
		}
	}
}