Find out how to make your life easier with SpreadsheetLight (often with a handful of lines of code). Obviously I can’t show you everything that SpreadsheetLight is capable of. If you can’t find any sample code here to do what you want, and you’re on a tight schedule, just contact me and I’ll show you how to accomplish your specific requirement.
You can read and modify existing Excel spreadsheets, even though SpreadsheetLight is optimised for creating brand new spreadsheets. In any case, your server will probably register nary a blip in performance and memory use. Micro-optimisation, down that path thou shalt not go.
Download it now, or check out the speed/memory benchmark code if you’re not ready yet.
Possibly the shallowest learning curve ever.
Because you’re worth it.
- The Hello World. This takes you through the basics of setting cell values. [Excel file]
- How to save to a web stream (useful for ASP.NET applications). [Excel file]
- How to modify an existing spreadsheet. [Original Excel file] [Modified Excel file]
- How to set document properties (or metadata). [Excel file]
- How to work with multiple spreadsheets at the same time (or at one go anyway). [First file] [Second file] [Modified second file] [Resulting file]
- How to open a spreadsheet that’s in a Stream object. [Original Excel file] [Modified Excel file]
- How to set cell comments. [Excel file]
- How to do LINQ stuff with cell data, get worksheet statistics and more. [Excel file]
- How to export to a DataTable. [Source Excel file] [1st DataTable XML] [2nd DataTable XML]
I found SpreadsheetLight when I did a Google search for “Free Export to Excel Libraries” — and Vincent Tan referenced it in a post he did on StackOverflow. I am the administrator of a small intranet site which uses a lot of legacy ASP Classic, and I’m trying to migrate the site into a modern .NET foundation. One of the things the site must do on many of its pages is display database data in a tabular format, and support downloading that data to Excel spreadsheets.
As it turns out, it’s pretty difficult to do that, so I’ve been searching for a long time for an efficient way to do that. The only solution I discovered, which fit most of our needs, involved actually downloading the ASPX page’s GridView control as an HTML object onto an Excel spreadsheet, and all the fields were then changed to text — including dates and currency values, which meant the user had to convert those data types before doing any kind of calculations with them.
Dissatisfied with that solution, I did the aforementioned Google search and hit upon Vincent Tan’s SpreadsheetLight library. I took a look at the licensing (free), took a look at the capabilities (creates an XML document from scratch using our data), and offered both sample code AND source code. I decided to download it.
At the same time I tried a few other really terrific sounding libraries, but no matter how much I replicated the sample code, I couldn’t get them to work. In desperation, I went back and tried SpreadsheetLight instead. And in less than ten minutes I had my web pages downloading full Excel XLSX files, created from the datatables used to create those web pages, and downloading them in native Excel format — not a series of strings in all columns. I could do calculations, I could use the dates AS dates, it held onto all the leading zeroes in critical fields — just like magic, it worked immediately, out of the box, no muss, no fuss, and no headache.
I’ve since deployed SpreadsheetLight to several other solutions in our intranet, and no one’s even noticed the switch. It was simple to deploy, works immediately, and Vincent even offered me a couple of words of advice (though he didn’t have to).
I can’t speak highly enough of SpreadsheetLight. If you’re looking for a quick and easy solution to get your data out as real Excel spreadsheets, without a lot of hoop-jumping, look no further than SpreadsheetLight.
Josh Hill
Tip: You might find the SLConvert class to be helpful in doing miscellaneous conversions, such as getting cell references from a row/column index, as well as converting centimetres to inches (Excel by default follows the imperial system instead of using the metric system. Unlike most civilised countries *cough* 🙂
Tip: You can find out basic worksheet statistics with SLWorksheetStatistics. For example, the number of rows/columns/cells used. Use the GetWorksheetStatistics() of the SLDocument class.
And just for kicks, here’s how to do it in ASP.NET MVC.
public FileStreamResult GenerateReport() { MemoryStream ms = new MemoryStream(); using (SLDocument sl = new SLDocument()) { sl.SetCellValue("B3", "I love ASP.NET MVC"); sl.SaveAs(ms); } // this is important. Otherwise you get an empty file // (because you'd be at EOF after the stream is written to, I think...). ms.Position = 0; return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx"); }
You’ve got style
- How to format numbers and dates. [Excel file]
- How to handle historic dates. For our purposes, any date before the year 1900 is considered “historic”. [Excel file]
- How to set text alignment. [Excel file]
- How to set font settings. [Excel file]
- How to set borders. [Excel file]
- How to set pattern fills and gradients (even fancy ones!). [Excel file]
- How to style your rows, columns and cells. And even copy styles around! [Excel file]
- How to use rich text. [Excel file]
- How to modify existing styles (of rows, columns and cells). [Excel file]
- How to use named cell styles (such as Good, Bad, Neutral, Check Cell, Heading 1 and more). [Excel file]
- How to set multiple lines of text in one cell (or how to wrap text). [Excel file]
- How to create new themes to style your spreadsheet! Or use one of 53 built-in themes! [Built-in themed Excel file] [Newly themed Excel file]
- How to copy cells (cut-and-paste, copy-and-paste, copy only values, copy only style formatting, copy from another worksheet). [Excel file]
- How to merge and unmerge cells. [Excel file]
- How to insert and delete rows and columns. [Excel file]
- How to copy entire rows and columns. [Excel file]
- How to hide rows and columns. [Excel file]
- How to set row heights and column widths. [Excel file]
- How to autofit row heights and column widths. NOTE: The provided Excel file is generated with a screen monitor at 120 DPI. If you read it at 96 DPI, the result might look different. [Excel file]
- How to group and ungroup rows and columns (and how to collapse and expand grouped rows and columns). [Excel file]
- How to add, copy, rename, move and delete worksheets. [Excel file]
- How to freeze and split worksheet panes. [Excel file]
- How to insert hyperlinks. [Excel file]
- How to insert pictures (with tons of fancy styling. We have 3D settings!). [Excel file]
- How to insert pictures with the image data in a byte array. [Excel file]
- How to add/delete background images. [Original Excel file] [Modified Excel file]
- How to set defined names. Defined names are what used to be called named cells and named ranges. [Excel file]
- How to add data validations. [Excel file]
- How to filter data. [Excel file]
- How to sort data. [Excel file]
- How to insert and remove page breaks. [Excel file]
- How to set worksheet protection (no password). [Excel file]
- How to insert tables. [Excel file]
- How to import data from a DataTable (for when you have a &%#*-ton of data to load). [Excel file]
- How to import text data from a file (such as a tab-delimited data file). [Excel file] [Delimited data] [Fixed width data]
- How to set the tab color of a sheet. [Excel file]
- How to set page margins. [Excel file]
- How to set simple page header and footer text. [Excel file]
- How to set page header and footer text in a more complicated manner (but highly flexible). [Excel file]
- How to set odd- and even-numbered page header and footer. [Excel file]
- How to set first page header and footer. [Excel file]
- How to set page settings such as orientation, paper size, draft quality and so on. [Excel file]
- How to set page layout (such as Normal, Page Layout and Page Break Preview). [Excel file]
- How to get the existing page settings of a worksheet (and then set it on another worksheet). [Existing Excel file] [Modified Excel file]
- How to set data bar conditional formats. [Excel file]
- How to set color scale conditional formats. [Excel file]
- How to set icon set conditional formats. [Excel file]
- How to set conditional formatting highlights. [Excel file]
- How to set Excel 2010 specific data bar conditional formats. [Excel file]
- How to set Excel 2010 specific icon set conditional formats (Look mom, no icons!). [Excel file]
- How to insert an area chart. [Excel file]
- How to insert a bar chart. [Excel file]
- How to insert a bubble chart. [Excel file]
- How to insert a column chart. [Excel file]
- How to insert a doughnut chart. [Excel file]
- How to insert a line chart. [Excel file]
- How to insert a pie chart. [Excel file]
- How to insert a radar chart. [Excel file]
- How to insert a scatter chart. [Excel file]
- How to insert a surface chart. [Excel file]
- How to insert a stock chart (High-Low-Close, Open-High-Low-Close, Volume-High-Low-Close and Volume-Open-High-Low-Close). [Excel file]
- How to insert a chart in a chartsheet. [Excel file]
- How to set and style the chart title. [Excel file]
- How to set and style the chart legend. [Excel file]
- How to set and style the chart area. [Excel file]
- How to set and style the chart plot area. [Excel file]
- How to set and style the floor, side wall and back wall of 3D charts. [Excel file]
- How to set and style the axis title of chart (works for primary, secondary and depth axis). [Excel file]
- How to set and style individual data series of a column chart. The column chart is used, but the concept works similarly for other kinds of charts. [Excel file]
- How to set and style the individual data series of a line chart. The line chart is used, but the concept works similarly for other kinds of charts. [Excel file]
- How to create a column-line-area combination chart. The concept works similarly for other types of combination charts. [Excel file]
- How to insert a data table in a chart. [Excel file]
- How to set data labels in a chart. [Excel file]
- How to set data point options in a chart. Yes, you can customise individual data points in a data series. [Excel file]
- How to create a thermometer chart. [Excel file]
- How to create a gauge chart. [Excel file]
- How to create a comparative histogram chart. [Excel file]
- How to create a Gantt chart. [Excel file]
- How to create sparklines. [Excel file]
- How to create column sparklines. [Excel file]
- How to create win/loss sparklines. [Excel file]
Work those worksheets
Data
Tip: You might find it useful to apply a table using InsertTable() after importing from either a DataTable or CSV/Text file. Importing data just imports uh data. Inserting a table applies styling.
Page settings
Conditional formatting
How come there’s no Excel 2010 specific color scale conditional formats? Because Microsoft hasn’t seen fit to make my life miserable. Yet.
Charting
Note: If you find the text in your charts looking a little weird, try setting the IsStylish property (under the SLCreateChartOptions when creating charts) to false. When true, SpreadsheetLight tries to follow the look and feel of the latest Excel version (but no guarantees), and this might make the chart look weird when viewed from an earlier version of Excel.
Novelty charts
Disclaimer: I’m not an Excel expert. Go check with your local spreadsheet geek on the details.
Sparklines
Note: You’ll probably need Excel 2010 and later to view the sparklines.