public static string DB_ExportCSV(DataSet ds, string strFileName = "export.csv", string strSep = ";", string strNull = "(NULL)") { string strLine = ""; string strValue = ""; object objValue = null; StringBuilder sb = new StringBuilder(); if (strFileName == "") { strFileName = "export.csv"; } if (strSep == "") { strSep = ";"; } //Response.ContentType = "text/plain" //Response.AddHeader("content-disposition", "attachment;filename=" & strFileName) foreach (DataTable table in ds.Tables) { strLine = ""; foreach (DataColumn item in table.Columns) { if (!item.ColumnName.StartsWith("-")) { if (!string.IsNullOrEmpty(strLine)) { strLine += strSep; } strLine += item.ColumnName; } } sb.AppendLine(strLine); foreach (DataRow row in table.Rows) { strLine = ""; foreach (DataColumn dc in table.Columns) { if (!dc.ColumnName.StartsWith("-")) { objValue = row[dc.ColumnName]; if (DBNull.Value.Equals(row[dc.ColumnName])) { strValue = strNull; } else if (ReferenceEquals(objValue, null)) { strValue = strNull; } else if (ReferenceEquals(dc.DataType, System.Type.GetType("System.Guid"))) { strValue = objValue.ToString(); } else if (string.IsNullOrEmpty(System.Convert.ToString(objValue))) { strValue = strNull; } else if (Information.IsDate(objValue)) { strValue = Funktioner.ToSqlDateTime(objValue); //ElseIf IsNumeric(objValue) Then // strValue = ToDecimal(objValue, 0).ToString } else { strValue = ((objValue + "").Trim()).ToString(); strValue = strValue.Replace(strSep, "|"); strValue = strValue.Replace("'", "´"); strValue = strValue.Replace(Constants.vbNewLine, " "); strValue = strValue.Replace("\r\n", " "); strValue = strValue.Replace(Constants.vbLf, " "); strValue = strValue.Replace(Constants.vbCr, " "); } if (!string.IsNullOrEmpty(strLine)) { strLine += strSep; } strLine += strValue; } } sb.AppendLine(strLine); } } return(sb.ToString()); }