protected void btnSave_Click(object sender, EventArgs e) { try { if (Session[SessionUploadLead] != null) { var dataList = Session[SessionUploadLead] as List <UploadLeadData>; UploadAllData allData = new UploadAllData { ChannelId = cmbChannel.SelectedItem.Value, CampaignId = cmbCampaign.SelectedItem.Value, UploadFileName = hdfFilename.Value, LeadDataList = dataList, CreateByUsername = HttpContext.Current.User.Identity.Name }; UploadLeadBiz biz = new UploadLeadBiz(); int uploadLeadId = 0; if (string.IsNullOrEmpty(hdfUploadLeadId.Value)) { uploadLeadId = biz.SaveNewUpload(allData); } else { uploadLeadId = int.Parse(hdfUploadLeadId.Value); var ret = biz.SaveUpdateUpload(uploadLeadId, allData); if (!ret) { if (biz.RedirectToView) { PrepareRedirectPage(); WriteLogFile(uploadLeadId.ToString(), biz.ErrorMessage); AppUtil.ClientAlertAndRedirect(Page, biz.ErrorMessage, GetViewPageUrl(hdfUploadLeadId.Value)); return; } else if (biz.RedirectToSearch) { PrepareRedirectPage(); WriteLogFile(uploadLeadId.ToString(), biz.ErrorMessage); AppUtil.ClientAlertAndRedirect(Page, biz.ErrorMessage, SearchPage); return; } else { WriteLogFile(uploadLeadId.ToString(), biz.ErrorMessage); AppUtil.ClientAlert(Page, biz.ErrorMessage); return; } } } PrepareRedirectPage(); AppUtil.ClientAlertAndRedirect(Page, "บันทึกข้อมูลเรียบร้อย", string.Format("SLM_SCR_056.aspx?uploadleadid={0}", uploadLeadId.ToString())); } else { PrepareRedirectPage(); WriteLogFile("None", "Session Expired, Page will be reloaded"); AppUtil.ClientAlertAndRedirect(Page, "Session Expired, Page will be reloaded", Request.Url.AbsoluteUri); } } catch (Exception ex) { string message = ex.InnerException != null ? ex.InnerException.Message : ex.Message; _log.Error(message); AppUtil.ClientAlert(Page, message); } }
//private void ExportExcelOpenXml(UploadAllData allData) //{ // try // { // string outputname = "DownloadLead_" + DateTime.Now.Year.ToString() + DateTime.Now.ToString("MMdd_HHmmss") + ".xlsx"; // string fileName = System.IO.Path.Combine(Server.MapPath("~/Upload"), outputname); // using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)) // { // WorkbookPart workbookPart = document.AddWorkbookPart(); // workbookPart.Workbook = new Workbook(); // WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); // worksheetPart.Worksheet = new Worksheet(); // Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); // Sheet sheet = new Sheet { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" }; // sheets.Append(sheet); // workbookPart.Workbook.Save(); // SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData()); // // Constructing header // Row row = new Row(); // row.Append( // ConstructCell("ชื่อลูกค้า", CellValues.String), // ConstructCell("นามสกุลลูกค้า", CellValues.String), // ConstructCell("ประเภทลูกค้า", CellValues.String), // ConstructCell("บัตรประชาชน/นิติบุคคล", CellValues.String), // ConstructCell("Owner Lead ID", CellValues.String), // ConstructCell("Delegate Lead ID", CellValues.String), // ConstructCell("เบอร์โทร#1", CellValues.String), // ConstructCell("เบอร์โทร#2", CellValues.String), // ConstructCell("รายละเอียด Lead", CellValues.String) // ); // // Insert the header row to the Sheet Data // sheetData.AppendChild(row); // // Inserting each employee // foreach (var data in allData.LeadDataList) // { // row = new Row(); // row.Append( // ConstructCell(data.Firstname, CellValues.String), // ConstructCell(data.Lastname, CellValues.String), // ConstructCell(data.CardTypeDesc, CellValues.String), // ConstructCell(data.CitizenId, CellValues.String), // ConstructCell(data.OwnerEmpCode, CellValues.String), // ConstructCell(data.DelegateEmpCode, CellValues.String), // ConstructCell(data.TelNo1, CellValues.String), // ConstructCell(data.TelNo2, CellValues.String), // ConstructCell(data.Detail, CellValues.String)); // sheetData.AppendChild(row); // } // worksheetPart.Worksheet.Save(); // } // //เก็บใส่ Session เพื่อไว้ใช้ในหน้า Export SLM_SCR_045.aspx // Session[excelfilepath] = fileName; // Session[outputfilename] = outputname; // Session[contenttype] = "xlsx"; // string script = "window.open('SLM_SCR_045.aspx', 'uploaddata', 'status=yes, toolbar=no, scrollbars=no, menubar=no, width=300, height=100, resizable=yes');"; // ScriptManager.RegisterStartupScript(Page, GetType(), "uploaddata", script, true); // } // catch // { // throw; // } //} /// <summary> /// Export Excel 2003 /// </summary> /// <param name="allData"></param> private void ExportExcel(UploadAllData allData) { try { string outputname = "DownloadLead_" + DateTime.Now.Year.ToString() + DateTime.Now.ToString("MMdd_HHmmss") + ".xls"; string fileName = System.IO.Path.Combine(Server.MapPath("~/Upload"), outputname); List <object[]> data = new List <object[]>(); allData.LeadDataList.ForEach(p => { data.Add(new object[] { p.Firstname, p.Lastname, p.CardTypeDesc, p.CitizenId, p.OwnerEmpCode , p.DelegateEmpCode, p.TelNo1, p.TelNo2, p.Detail, p.StatusDesc, p.Remark, p.TicketId }); }); var ebz = new ExcelExportBiz(); var list = new List <ExcelExportBiz.SheetItem> { new ExcelExportBiz.SheetItem { SheetName = "Sheet1", RowPerSheet = 0, Data = data, Columns = new List <ExcelExportBiz.ColumnItem> { new ExcelExportBiz.ColumnItem { ColumnName = "ชื่อลูกค้า", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "นามสกุลลูกค้า", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "ประเภทลูกค้า", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "บัตรประชาชน/นิติบุคคล", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "Owner Lead ID", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "Delegate Lead ID", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "เบอร์โทร#1", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "เบอร์โทร#2", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "รายละเอียด Lead", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "สถานะ", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "Remark", ColumnDataType = ExcelExportBiz.ColumnType.Text }, new ExcelExportBiz.ColumnItem { ColumnName = "Ticket Id", ColumnDataType = ExcelExportBiz.ColumnType.Text } } } }; if (ebz.CreateExcel(fileName, list)) { Session[excelfilepath] = fileName; Session[outputfilename] = outputname; string script = string.Format("window.open('SLM_SCR_045.aspx', '{0}', 'status=yes, toolbar=no, scrollbars=no, menubar=no, width=300, height=100, resizable=yes');", DateTime.Now.ToString("yyyyMMdd_HHmmss")); ScriptManager.RegisterStartupScript(Page, GetType(), "uploaddata", script, true); } else { AppUtil.ClientAlert(Page, ebz.ErrorMessage); } } catch { throw; } }