Allow pasting of results into Excel without formatting
Pasting query results into Excel includes HTML formatting which is messy - could this be stripped away to get a more Excel-friendly output?

It’s now available in V2
-
terry.aney commented
I've got a simliar DumpCSV extension along with some other possibly helpful ones at:
-
Jon Herrell commented
Here's some code I came up with. If you want to use it as a start, have at it:
public static string DumpTabDelimited(IEnumerable linqQuery, string filePath)
{
return DumpDelimited(linqQuery, filePath, "\t", true);
}public static string DumpTabDelimited(IEnumerable linqQuery)
{
return DumpDelimited(linqQuery, null, "\t", true);
}public static string DumpPipeDelimited(IEnumerable linqQuery, string filePath)
{
return DumpDelimited(linqQuery, filePath, "|", true);
}public static string DumpPipeDelimited(IEnumerable linqQuery)
{
return DumpDelimited(linqQuery, null, "|", true);
}public static string DumpDelimited(IEnumerable linqQuery, string filePath, string delim,
bool includeHeader)
{
StreamWriter wtr;
if (filePath == null || filePath == string.Empty)
{
wtr = new StreamWriter(new MemoryStream());
}
else
{
wtr = new StreamWriter(filePath, false);
}Type resultType = linqQuery.AsQueryable().ElementType;
bool isFirst = true;
if (includeHeader)
{
if (!resultType.IsGenericType)
{
wtr.Write(resultType.Name);
}
else
{
foreach (PropertyInfo p in resultType.GetProperties())
{
if (!isFirst)
{
wtr.Write(delim);
}
else
{
isFirst = false;
}
wtr.Write(p.Name);
}
}
wtr.WriteLine();
}foreach (var aRow in linqQuery)
{
isFirst = true;
if (!resultType.IsGenericType)
{
wtr.Write(aRow);
}
else
{
foreach (PropertyInfo p in resultType.GetProperties())
{
if (!isFirst)
{
wtr.Write(delim);
}
else
{
isFirst = false;
}
wtr.Write(p.GetValue(aRow, null));
}
}
wtr.WriteLine();
}if (filePath == null || filePath == string.Empty)
{
wtr.Flush();
wtr.BaseStream.Seek(0, SeekOrigin.Begin);StreamReader rdr = new StreamReader(wtr.BaseStream);
string tempOutput = rdr.ReadToEnd();wtr.Close();
rdr.Close();return tempOutput;
}
else
{
wtr.Flush();
wtr.Close();return "Output written to " + filePath;
}
} -
Jon Herrell commented
I'd like to see one of two things happen:
(1) Enhance the UI of LinqPad to support copy/paste of results better using right-click or a SaveOutputAs menu item.
(2) Enhance Dump to include output to a file. Maybe a DumpToFile method with parameters for formatting such as DumpToFile("CSV", "Filename");