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 DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; using System.Linq; public partial class admin_accounting_index : MyWeb.config { private Model.ezEntities _db = new Model.ezEntities(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { InitEnumsOptions(s_category); //收支類別 BuildKind(); } } #region 分類 public void BuildKind() { s_kind.Items.Clear(); s_kind.Items.Add(new ListItem("請選擇", "")); buildMultiKind(s_kind, "accounting_kind", 0, "", 1, Model.accounting.KindLevelMax); s_kind2.Items.Clear(); s_kind2.Items.Add(new ListItem("請選擇", "")); buildMultiKind(s_kind2, "accounting_kind2", 0, "", 1, Model.accounting.KindLevelMax); } #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 = 1, Width = 15, CustomWidth = true }, new Column { Min = 2, Max = 4, Width = 10, CustomWidth = true }, new Column { Min = 5, Max = 5, Width = 20, CustomWidth = true }, new Column { Min = 6, Max = 7, Width = 10, 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 }, 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.accountings.AsEnumerable(); if (!isStrNull(s_category.SelectedValue)) qry = qry.Where(o => o.category == Convert.ToInt32(s_category.SelectedValue)); if (!isStrNull(s_kind.SelectedValue)) qry = qry.Where(o => o.kind == Convert.ToInt32(s_kind.SelectedValue)); if (!isStrNull(s_kind2.SelectedValue)) qry = qry.Where(o => o.kind2 == Convert.ToInt32( s_kind2.SelectedValue)); if (!isStrNull(s_uptime1.Text) && isDate(s_uptime1.Text)) qry = qry.Where(o => o.uptime >= Convert.ToDateTime(s_uptime1.Text)); if (!isStrNull(s_uptime2.Text) && isDate(s_uptime2.Text)) qry = qry.Where(o => o.uptime < Convert.ToDateTime(s_uptime2.Text).AddDays(1)); 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.uptime.HasValue? item.uptime.Value.ToString("yyyy/MM/dd"):""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.category.HasValue ? GetEnumsDescription((Model.accounting.type)Val(item.category.Value)):""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.kind2.HasValue ? item.accounting_kind2.kind : ""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.kind.HasValue ? item.accounting_kind.kind : ""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.price.HasValue ? ValMoney(item.price.Value) : "0"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.tax.HasValue ? ValMoney(item.tax.Value) : "0"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(ValMoney((item.price.HasValue ? item.price.Value : 0) +(item.tax.HasValue ? item.tax.Value : 0))), DataType = CellValues.String } ); sd.AppendChild(tr); } } //儲存文件(如果有要儲實體的檔案在upload才需要) //wb.Workbook.Save(); Model.admin_log admin_log = new Model.admin_log(); admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Accounting, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => (x.uptime.HasValue ? x.uptime.Value.ToString("yyyy/MM/dd") : "") + (x.kind2.HasValue ? x.accounting_kind2.kind : "")).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 }