Exemplo n.º 1
0
        // 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;
            }
        }
Exemplo n.º 2
0
        // 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;
            }
        }