private void ExportData(List <SPhone_Chat> data, string targetFile) { string documentID = "Xlt101"; List <ChatMessage> allMsgs = new List <ChatMessage>(); // 保存文件 DataTable dt = AsposeHelper.GetTemplateDataTable(documentID); data.ForEach(chat => { allMsgs.AddRange(GetChatDetails(chat)); }); // lst 转 table allMsgs.ForEach(msg => { DataRow dr = dt.NewRow(); dr["ChatID"] = msg.ChatID; dr["Queue"] = msg.Queue; dr["CustomerID"] = msg.CustomerID; dr["CustomerName"] = msg.CustomerName; dr["Date"] = msg.Date; dr["Time"] = msg.Time; dr["NickName"] = msg.NickName; if (msg.Message != null && msg.Message.IndexOf("<img src=\"data:image/") != -1) { dr["Message"] = Tele.Common.Utils.CutStr(Tele.Common.Utils.NoHtml(msg.Message), 1000); } else if (msg.Message.Length > 1000) { dr["Message"] = Tele.Common.Utils.CutStr(Tele.Common.Utils.NoHtml(msg.Message), 1000); } else { dr["Message"] = msg.Message; } // dt.Rows.Add(dr); }); if (Request.QueryString["debug"] != null) { var qs = allMsgs.OrderByDescending(x => x.Message.Length).Take(10); foreach (var item in qs) { Response.Write(item.ChatID + "\r\n"); } return; } string templateFileName = AsposeHelper.GetTemplateFileName(documentID); //string newFileName = string.Format(@"{0}\Documents\TempFolder\{1}.xlsx", this.Request.PhysicalApplicationPath, dt.TableName + DateTime.Now.ToString("-yyMMdd")); BatchDownloadResponse response = AsposeHelper.DataTableToExcel(templateFileName, targetFile, dt); //AsposeHelper.DownloadFile(newFileName); }
private void ExportData(List <SPhone_Chat> data) { string documentID = "Xlt101"; List <ChatMessage> allMsgs = new List <ChatMessage>(); // 保存文件 DataTable dt = AsposeHelper.GetTemplateDataTable(documentID); data.ForEach(chat => { allMsgs.AddRange(GetChatDetails(chat)); }); // lst 转 table allMsgs.ForEach(msg => { DataRow dr = dt.NewRow(); dr["ChatID"] = msg.ChatID; dr["Queue"] = msg.Queue; dr["CustomerID"] = msg.CustomerID; dr["CustomerName"] = msg.CustomerName; dr["Date"] = msg.Date; dr["Time"] = msg.Time; dr["NickName"] = msg.NickName; if (msg.Message != null && msg.Message.IndexOf("<img src=\"data:image/") != -1) { dr["Message"] = Tele.Common.Utils.CutStr(Tele.Common.Utils.NoHtml(msg.Message), 1000);; } else if (msg.Message.Length > 1000) { dr["Message"] = Tele.Common.Utils.CutStr(Tele.Common.Utils.NoHtml(msg.Message), 1000); } else { dr["Message"] = msg.Message; } // dt.Rows.Add(dr); }); string templateFileName = AsposeHelper.GetTemplateFileName(documentID); string newFileName = string.Format(@"{0}\Documents\TempFolder\{1}.xlsx", this.Request.PhysicalApplicationPath, dt.TableName); BatchDownloadResponse response = AsposeHelper.DataTableToExcel(templateFileName, newFileName, dt); AsposeHelper.DownloadFile(newFileName); }
public static BatchDownloadResponse DataTableToExcel(string templateFileName, string fileName, DataTable dt) { if (!string.IsNullOrEmpty(templateFileName)) { templateFileName = templateFileName.Replace("\\\\", "\\"); } if (!string.IsNullOrEmpty(fileName)) { fileName = fileName.Replace("\\\\", "\\"); } BatchDownloadResponse response = new BatchDownloadResponse(fileName); if (dt == null) { response.AddError(0, "数据为空,请检查!"); return(response); } string licPath = System.Configuration.ConfigurationManager.AppSettings["AsposeLicPath"]; License lic = new License(); if (!string.IsNullOrEmpty(licPath)) { lic.SetLicense(licPath); } WorkbookDesigner wd = new WorkbookDesigner(); wd.Open(templateFileName); wd.SetDataSource(dt); wd.Process(); wd.Save(fileName, FileFormatType.Excel2007Xlsx); wd = null; FileInfo fi = new FileInfo(fileName); response.FileName = fi.Name; return(response); }