using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; public partial class admin_project_list : MyWeb.config { private Model.ezEntities _db = new Model.ezEntities(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BuildKind(); } } #region 分類 public void BuildKind() { //品項 s_actItem_num.Items.Clear(); s_actItem_num.Items.Add(new ListItem("請選擇", "")); var qry = _db.actItems.AsEnumerable(); qry = qry.Where(o => (int?)o.category==(int)Model.activity.category.Patronize);//贊助項目 qry = qry.OrderByDescending(o => o.num).ToList(); if(qry.Count() > 0) foreach(var qq in qry) s_actItem_num.Items.Add(new ListItem(qq.subject, qq.num.ToString())); } #endregion #region 匯出Excel protected void excel_Click(object sender, EventArgs e) { var memoryStream = new MemoryStream(); using (var doc = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook)) { var wb = doc.AddWorkbookPart(); wb.Workbook = new Workbook(); var sheets = wb.Workbook.AppendChild(new Sheets()); //建立第一個頁籤 var ws = wb.AddNewPart(); ws.Worksheet = new Worksheet(); sheets.Append(new Sheet() { Id = wb.GetIdOfPart(ws), SheetId = 1, Name = "贊助報告​" }); //設定欄寬 var cu = new Columns(); cu.Append( new Column { Min = 1, Max = 3, Width = 15, CustomWidth = true }, new Column { Min = 4, Max = 4, Width = 30, CustomWidth = true } ); ws.Worksheet.Append(cu); //建立資料頁 var sd = new SheetData(); ws.Worksheet.AppendChild(sd); //第一列資料 var tr = new Row(); tr.Append( new Cell() { CellValue = new CellValue("品項"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("贊助人 "), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("金額"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("專案活動"), DataType = CellValues.String } ); sd.AppendChild(tr); //查詢要匯出的資料 var qry = _db.pro_order_detail.AsEnumerable(); qry = qry.Where(o => (int?)o.actItem.category == (int)Model.activity.category.Patronize); if (!string.IsNullOrEmpty(s_f_num.Value)) qry = qry.Where(o => o.f_num == Convert.ToInt32(s_f_num.Value)); if (!string.IsNullOrEmpty(s_actItem_num.SelectedValue)) qry = qry.Where(o => o.actItem_num == Convert.ToInt32(s_actItem_num.SelectedValue)); qry = qry.OrderByDescending(o => o.num); var list = qry.ToList(); if (list.Count > 0) { var projectDt = _db.projects.AsEnumerable(); //專案 foreach (var item in list) { var projects = from s in projectDt where s.actItem_num == item.actItem_num select new { subject = s.subject, num = s.num, }; //新增資料列 tr = new Row(); tr.Append( new Cell() { CellValue = new CellValue(item.f_num.HasValue ? item.follower.u_name : ""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.actItem_num.HasValue ? item.actItem.subject : ""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.price.HasValue ? ValMoney(item.price.Value) : ""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(string.Join(",", projects.Select(x => x.subject).ToList())), DataType = CellValues.String } ); sd.AppendChild(tr); } } } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=贊助報告.xlsx"); HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray()); HttpContext.Current.Response.End(); } #endregion }