the file you are trying to open is in a different format than specified by the file extension in Asp.Net

I have used CloseXML to solve the problem. public static void ExportToExcel(IEnumerable<dynamic> data, string sheetName) { XLWorkbook wb = new XLWorkbook(); var ws = wb.Worksheets.Add(sheetName); ws.Cell(2, 1).InsertTable(data); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”; HttpContext.Current.Response.AddHeader(“content-disposition”, String.Format(@”attachment;filename={0}.xlsx”,sheetName.Replace(” “,”_”))); using (MemoryStream memoryStream = new MemoryStream()) { wb.SaveAs(memoryStream); memoryStream.WriteTo(HttpContext.Current.Response.OutputStream); memoryStream.Close(); } HttpContext.Current.Response.End(); } Installed ClosedXML in my project using Nuget Package … Read more

Convert an int to bool with Json.Net [duplicate]

Ended up creating a converter: public class BoolConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(((bool)value) ? 1 : 0); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return reader.Value.ToString() == “1”; } public override bool CanConvert(Type objectType) { return objectType == typeof(bool); } … Read more

Serialize an object to XElement and Deserialize it in memory

You can use these two extension methods to serialize and deserialize between XElement and your objects. public static XElement ToXElement<T>(this object obj) { using (var memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) { var xmlSerializer = new XmlSerializer(typeof(T)); xmlSerializer.Serialize(streamWriter, obj); return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray())); } } } public static T FromXElement<T>(this XElement xElement) … Read more

Directly sending keystrokes to another process via hooking

This is a little code that allows you to send message to a backgrounded application. To send the “A” char for example, simply call sendKeystroke(Keys.A), and don’t forget to use namespace System.windows.forms to be able to use the Keys object. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace keybound { … Read more

ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew

If you’re going to start a long-running task with TPL, you should specify TaskCreationOptions.LongRunning, which will mean it doesn’t schedule it on the thread-pool. (EDIT: As noted in comments, this is a scheduler-specific decision, and isn’t a hard and fast guarantee, but I’d hope that any sensible production scheduler would avoid scheduling long-running tasks on … Read more