The developer community has gotten big on the Internet the last couple of years – very big. New blogs are constantly adding value to the blogosphere which seem to grow by the second. There is no doubt in my mind that this trend is a bonus for us developers, because we can now find multiple solutions to almost every problem we face in our daily professional life.

 

Being a .NET developer I subscribe to a lot of .NET related sites like DotNetKicks and ASP.NET, but also the more broad developer sites like dzone, Digg/programming etc. I really enjoy reading non .NET related things as well because it puts various topics in perspective.

 

However, today at Digg/programming, not one single topic involved .NET. There were a lot of PHP, Ruby and Java. So where is .NET? Why is .NET so under represented on the broader community sites? I don’t think it is because PHP or Java has a bigger user base or is better or worse than .NET. It must be something else. Maybe it is because .NET developers just don’t care about the other programming languages and platforms. That’s fair enough, but I don’t buy it.

 

Could it be because .NET developers don’t feel as strongly about their choice of platform as the rest? They are happy with Visual Studio, they can find what ever they need from the Internet and that’s it. They don’t feel the urge to go tell the world about all the wonderful features and possibilities of .NET. They just use it and feel happy about it.

 

Another reason could be that .NET devs are not as likely to jump on the Web 2.0 wave of social bookmarks, RSS and other such sharing technologies. They are maybe not aware of the possibilities in the new Internet and as such are more likely to be more passive users.

 

Either way, this is bad. Fellow .NET’ers go promote, go write blog posts and article on .NET related goodies and make a presens on the broad community sites. We can’t and won’t beat the rest of the community, but we can do better than we do now.

Back in February I wrote a post on how to export DataTables to XML and Excel and I still get a lot of search engine traffic to that post. People have been asking me to simplify the example and only concentrate on the Excel export, so that’s what I will do now.

All spreadsheet applications (Excel, Calc etc.) understand semicolon separated files natively, so everyone can use this method – you don’t even have to use Excel for it to work.

public static void ExportToSpreadsheet(DataTable table, string name)

{

  HttpContext context = HttpContext.Current;

  context.Response.Clear();

 

  foreach (DataColumn column in table.Columns)

  {

    context.Response.Write(column.ColumnName + ";");

  }

 

  context.Response.Write(Environment.NewLine);

 

  foreach (DataRow row in table.Rows)

  {

    for (int i = 0; i < table.Columns.Count; i++)

    {

      context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");

    }

    context.Response.Write(Environment.NewLine);

  }

 

  context.Response.ContentType = "text/csv";

  context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");

  context.Response.End();

}

>

Then just call this method and pass the DataTable and the filename as parameters.

ExportToSpreadsheet(table, "products");

The method is static so you can use it anywhere in a web application. Put it on a page, a HTTP Handler, add it to the App_Code folder or stick it in a separate assembly. As long as you call it from a web application it will work.