CSVHelper.cs內(nèi)容:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Threading; 7 using System.IO; 8 using System.Data; 9 using System.Windows.Forms; 10 11 namespace IMSCommonFunction 12 { 13 public class CSVHelper 14 { 15 public static string FilterCSVCell(string cellContent) 16 { 17 bool isAddFlag = false; 18 if (cellContent.IndexOf("/"") != -1) 19 { 20 cellContent = cellContent.Replace("/"", "/"/""); 21 cellContent = "/"" + cellContent + "/""; 22 isAddFlag = true; 23 } 24 if (cellContent.IndexOf(",") != -1 && isAddFlag != true) 25 { 26 cellContent = "/"" + cellContent + "/""; 27 } 28 return cellContent; 29 } 30 31 public static void ExportCSVFile(HttPResponse response, string fullPath, string Content) 32 { 33 try 34 { 35 response.Buffer = true; 36 response.Clear(); 37 response.Charset = System.Text.Encoding.Default.BodyName; 38 response.ContentEncoding = System.Text.Encoding.UTF8;// System.Text.Encoding.GetEncoding("GB2312");//GB2312用Excel打開(kāi)時(shí),沒(méi)有亂碼。 39 response.AppendHeader("Content-Disposition", "attachment;filename=" + fullPath); 40 response.ContentType = "application/ms-excel"; 41 response.Output.Write(Content); 42 response.Flush(); 43 response.End(); 44 } 45 catch (ThreadAbortException) 46 { 47 } 48 catch (Exception ex) 49 { 50 throw new ApplicationException(string.Format("Export CSV file have a error: {0}", fullPath), ex); 51 } 52 } 53 54 public static void FileDownload(string FullFileName) 55 { 56 FileInfo DownloadFile = new FileInfo(FullFileName); 57 System.Web.HttpContext.Current.Response.Clear(); 58 System.Web.HttpContext.Current.Response.ClearHeaders(); 59 System.Web.HttpContext.Current.Response.Buffer = false; 60 string extension = Path.GetExtension(FullFileName); 61 extension = string.IsNullOrEmpty(extension) ? extension : extension.ToLower(); 62 switch (extension) 63 { 64 case ".xml": 65 System.Web.HttpContext.Current.Response.ContentType = "text/xml"; 66 break; 67 default: 68 System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; 69 break; 70 } 71 string browser = System.Web.HttpContext.Current.Request.UserAgent.ToUpper(); 72 bool isNeedEncode = !browser.Contains("Firefox"); 73 System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + 74 (isNeedEncode ? System.Web.HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8) : DownloadFile.Name)); 75 System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); 76 System.Web.HttpContext.Current.Response.Flush(); 77 if (System.Web.HttpContext.Current.Response.IsClientConnected) 78 System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName); 79 80 //出錯(cuò) 81 System.Web.HttpContext.Current.Response.End(); 82 System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); 83 } 84 85 public static void DataHtmlToExcel(HttpResponse response, DataTable dt, string strFileName) 86 { 87 string style = @"<style> .text { mso-number-format:/@; } </scr
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注