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; using static TreeView; public partial class admin_project_index : 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_kind.Items.Clear(); s_kind.Items.Add(new ListItem("請選擇", "")); //buildMultiKind(s_kind, "project_kind", 0, "", 1, Model.project.KindLevelMax); var qry1 = new TreeView().get_data2(_db.project_kind.Select(o => new TreeItem() { kind = o.kind, num = o.num, root = o.root, range = o.range, }).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0); if (qry1.Count() > 0) foreach (var qq in qry1) s_kind.Items.Add(new ListItem(new TreeView().RptDash(qq.Level) + qq.kind, qq.num.ToString())); //品項 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 = 2, Width = 15, CustomWidth = true }, new Column { Min = 3, Max = 3, Width = 25, 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 } ); sd.AppendChild(tr); //查詢要匯出的資料 var qry = _db.projects.AsEnumerable(); if (!string.IsNullOrEmpty(subject.Value)) qry = qry.Where(o => o.subject.Contains(subject.Value.Trim())); if (!string.IsNullOrEmpty(s_kind.SelectedValue)) qry = qry.Where(o => o.kind == Convert.ToInt32( s_kind.SelectedValue)); 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) { foreach (var item in list) { //新增資料列 tr = new Row(); tr.Append( new Cell() { CellValue = new CellValue(item.subject), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.kind.HasValue? item.project_kind.kind:""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.actItem_num.HasValue? item.actItem.subject:""), DataType = CellValues.String } ); sd.AppendChild(tr); } } Model.admin_log admin_log = new Model.admin_log(); admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Project, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => x.subject).ToList())); } 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 }