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; using OfficeOpenXml.FormulaParsing.Excel.Functions.Information; using MyWeb; public partial class admin_supplier_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, "supplier_kind", 0, "", 1, Model.supplier.KindLevelMax); var qry1 = new TreeView().get_data2(_db.supplier_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())); } #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 = 25, CustomWidth = true }, new Column { Min = 5, Max = 5, Width = 15, 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("聯絡電話1"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("聯絡電話2"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("傳真"), DataType = CellValues.String }, new Cell() { CellValue = new CellValue("E-MAIL​"), 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.suppliers.AsEnumerable(); if (!string.IsNullOrEmpty(s_kind.SelectedValue)) qry = qry.Where(o => o.kind == Convert.ToInt32(s_kind.SelectedValue)); if (!string.IsNullOrEmpty(s_number.Value)) qry = qry.Where(o => (o.s_number ?? "").Contains(s_number.Value.Trim())); qry = qry.OrderByDescending(o => o.num); var list = qry.ToList(); if (list.Count > 0) { MyWeb.encrypt encrypt = new MyWeb.encrypt(); foreach (var item in list) { //新增資料列 tr = new Row(); tr.Append( new Cell() { CellValue = new CellValue(item.s_number ), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.c_num ), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.kind.HasValue ? item.supplier_kind.kind : ""), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.u_name), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.address), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.phone1)), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.phone2)), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.fax)), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.email), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.url), DataType = CellValues.String }, new Cell() { CellValue = new CellValue(item.demo), 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.Supplier, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => x.s_number + x.u_name).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 }