myXLS

Quick Start Guide to Using MyXls

Modified: 2008/08/13 16:22 by tim.erickson - Categorized as: MyXls

Edit

Configuring Your Project

MyXls is easy to add to your project.
  1. Download the binaries.
  2. Add a reference to org.in2bits.MyXls.dll in your project
  3. Add a using statement to reference the MyXls namespace using org.in2bits.MyXls;

Edit

Writing an Excel Document

Edit

Basic Example

This example creates a new Excel file, adds a named worksheet, and puts a value into a cell in that worksheet. Very basic but it shows how in six lines of code you can use MyXls to create Excel files.

XlsDocument doc = new XlsDocument(); doc.FileName = "HelloWorld.xls"; Worksheet sheet = doc.Workbook.Worksheets.Add("Hello World Sheet"); Cell cell = sheet.Cells.Add(1, 1, "Hello!"); cell.Font.Weight = FontWeight.Bold;

doc.Save();


Edit

Web Application Support

MyXls has built in support for web applications. Using the XlsDocument.Send() method you can easily send an Excel spreadsheet back to the client.

// Send the document back as an attachement // Same as doc.Send(XlsDocument.SendMethods.Attachment); doc.Send();

// or use // send the document back within the page doc.Send(XlsDocument.SendMethods.Inline);


Edit

Reading an Excel Document

Read support is still relatively experimental. However, MyXls should be able to read all values (even formulas! though you can't write formulas with it yet!) and formatting. It will not read any OLE objects, Pivot Tables, Charts/Graphs, VBA modules, etc. -- it will simply ignore them. It supports only Excel versions 97 thru 2003/XP - 2007's .xlsx XML format is not supported. However, you can try the below to see if it will work for you, and please submit a bug if you have any problems beyond that.

Edit

Basic Example

This example reads various values (including formula results) from an example Excel file, BlankBudgetWorksheet.xls. This does does not show retrieving formatting (font size, borders, etc.), but you should get the basic idea of how to reference the cells on which you would read those other properties:

XlsDocument xls = new XlsDocument(@"c:\Path\To\BlankBudgetWorksheet.xls", null); //read in the Excel file

string worksheet1Name = xls.Workbook.Worksheets0.Name; //Worksheet 1 name : "Budget" string worksheet2Name = xls.Workbook.Worksheets1.Name; //Worksheet 2 name : "Income" string worksheet3Name = xls.Workbook.Worksheets2.Name; //Worksheet 3 name : "Expenses"

Worksheet sheet = xls.Workbook.Worksheets0; //get a reference to a Worksheet

string cellH6HyperlinkText = (string)sheet.Rows6.CellAtCol(8).Value; //Cell H6 hyperlink text : "See reverse for instructions and guidelines" string cellJ7Value = (string)sheet.Rows7.CellAtCol(10).Value; //Cell J7 value : "Budget Plan" string cellG28Value = (string)sheet.Rows28.CellAtCol(7).Value; //Cell G28 value : "Administrative Support (12% of Revenue)" long cellC10Value = (long)sheet.Rows10.CellAtCol(3).Value; //Cell C10 value : 6801 string cellF10FormulaResultValue = (string)sheet.Rows10.CellAtCol(6).Value; //Cell F10 Formula result value : "- 20"
Copyright © 2007-2008 Tim Erickson