Skip to content

Instantly share code, notes, and snippets.

@patriklindstrom
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patriklindstrom/d2c3f10d6cb0a6994dc5 to your computer and use it in GitHub Desktop.
Save patriklindstrom/d2c3f10d6cb0a6994dc5 to your computer and use it in GitHub Desktop.
Example of ClosedXML handling ÅÄÖ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// to use close xml use nuget Package Manager Console: Install-Package ClosedXML
// example from http://closedxml.codeplex.com/wikipage?title=Showcase&referringTitle=Documentation
// This example is to prove that closedXML has no problem with strings containing å ä ö swedish letters with umlauts and circles.
// it adds person with name Tösse Långbänk
// Its from question here : http://www.lcube.se/closedxml-det-enkla-sattet-att-exportera-till-excel/
using ClosedXML.Excel;
namespace HelloClosedXML
{
class Program
{
static void Main(string[] args)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Contacts");
//Title
ws.Cell("B2").Value = "Contacts";
//First Names
ws.Cell("B3").Value = "FName";
ws.Cell("B4").Value = "John";
ws.Cell("B5").Value = "Hank";
ws.Cell("B6").SetValue("Dagny"); // Another way to set the value
ws.Cell("B7").Value = "Tösse";
//Last Names
ws.Cell("C3").Value = "LName";
ws.Cell("C4").Value = "Galt";
ws.Cell("C5").Value = "Rearden";
ws.Cell("C6").SetValue("Taggart"); // Another way to set the value
ws.Cell("C7").Value = "Långbänk";
//Format the ÅÄÖ
var rngTable = ws.Range("B7:C7");
rngTable.Cells().Style
.Font.SetBold()
.Fill.SetBackgroundColor(XLColor.CornflowerBlue);
// Boolean
ws.Cell("D3").Value = "Outcast";
ws.Cell("D4").Value = true;
ws.Cell("D5").Value = false;
ws.Cell("D6").SetValue(false); // Another way to set the value
ws.Cell("D7").Value = true;
//DateTime
ws.Cell("E3").Value = "DOB";
ws.Cell("E4").Value = new DateTime(1919, 1, 21);
ws.Cell("E5").Value = new DateTime(1907, 3, 4);
ws.Cell("E6").SetValue(new DateTime(1921, 12, 15)); // Another way to set the value
ws.Cell("E7").Value = new DateTime(1912, 2, 29);
//Numeric
ws.Cell("F3").Value = "Income";
ws.Cell("F4").Value = 2000;
ws.Cell("F5").Value = 40000;
ws.Cell("F6").SetValue(10000); // Another way to set the value
ws.Cell("F7").SetValue(13000); // Another way to set the value
//Adjust column widths
ws.Columns().AdjustToContents();
//Save file on desktop
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string excelFilePath = System.IO.Path.Combine(path, "ShowcaseÅÄÖ.xlsx");
wb.SaveAs(excelFilePath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment