这个世界并不缺少创意,而是缺少发现

今天发现一个比较神奇的Excel组件,纯.NET 代码做的。赞一个先

Overview

MyXLS is a .NET 2.0 library that writes and reads native Excel files quickly and easily, including formatting and multiple sheets. Generate Excel files for ASP.NET sites or .NET applications. Doesn't require Excel on the server or any licensing. Compatible with Excelversions 97 and up. Features
  • Pure .NET code - no P/Invokes, Interop assemblies, or Excel COM automation behind the scenes.
  • No other dependencies - add and reference the MyXls dll in your project and go!
  • Fast! (we will be adding some performance metrics)
  • Lightweight! The dll is only ~100 KB
  • Free to use, no licensing restrictions.
  • Open Source Project
  • The files generated are 100% compatible with Excel versions 97 and up. Files are BIFF8, which is Excel 97-2003's native format, and fully forward compatible with Excel 2007.
  • Create any number of Worksheets
  • Name Worksheets
  • Write values to any cell on any Worksheet - values don't have to be in contiguous ranges
  • Supports a good portion of Excel's formatting capabilities (bold, underline, italic, rotation, etc)
  • Supports writing Metadata - values displayed in Excel's File->Properties dialog box

http://myxls.in2bits.org/wiki/Default.aspx?Page=MainPage&AspxAutoDetectCookieSupport=1

image

 

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"
posted @ 2009-05-22 22:14  陈希章  阅读(863)  评论(0编辑  收藏  举报