// Export to CrystalReports PDF //public void ExportToPDF(ExportParameter.PDF ReportParameter) //{ // var report = new ReportDocument(); // try // { // if (ReportParameter == null) // { // throw new Exception("parameter to export is not null."); // } // HttpContext.Current.Response.Buffer = false; // HttpContext.Current.Response.ClearContent(); // HttpContext.Current.Response.ClearHeaders(); // report.Load(HttpContext.Current.Server.MapPath(ReportParameter.PathCrystalReport)); // report.SetDataSource(ReportParameter.DataSource); // foreach (var parameter in ReportParameter.Parameter) // { // report.SetParameterValue(parameter.Name, parameter.Value); // } // report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, ReportParameter.FileName); // } // catch (Exception ex) // { // throw ex; // } // finally // { // report.Close(); // report.Dispose(); // GC.Collect(); // } //} #endregion #region ----- Export Txtfile ------ // set parameter for export to txtfile public ExportParameter.TxtFile SetReportParameter(string filename, IList <string> listData) { try { if (string.IsNullOrEmpty(filename)) { throw new Exception("Filename to export is not null or empty."); } if (listData == null || listData.Count == 0) { throw new Exception("Data to export is not null or empty."); } var parameter = new ExportParameter.TxtFile() { FileName = filename, ListData = listData }; return(parameter); } catch (Exception ex) { throw ex; } }
// Export to txtfile public void ExportToTxtfile(ExportParameter.TxtFile parameter) { try { if (parameter == null) { throw new Exception("parameter to export is not null."); } string filename = parameter.FileName; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "text/plain"; HttpContext.Current.Response.Charset = "windows-874"; // ระบบปลายทางต้องการ format UNIX ANSI จึงต้องใช้ windows-874 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(874); HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", filename)); using (StreamWriter sw = new StreamWriter(HttpContext.Current.Response.OutputStream, Encoding.Default)) { foreach (string txt in parameter.ListData) { sw.WriteLine(txt); } } HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } catch (Exception ex) { throw ex; } }