優秀開源項目:MyXls

若是從快速生成Excel報表,不調用Excel組件角度講,MyXls多是一種最好的選擇之一,固然使用Open Xml方式也是不錯的選擇。MyXls是一個用C#語言開發的生成Excel報表的優秀開源項目,在快速開發中我一直比較喜歡它。MyXls官方的解釋:
Writes and now Reads Excel files quickly and easily, including formatting. Generate Excel files for ASP.NET sites or .NET applications. Doesn't require Excel on the server or any licensing $. Compatible with Excel versions >= 97
 
MyXls能夠用在.NET平臺的諸如Windows Form,Asp.NET項目中,固然Sharepoint項目中也能夠使用,支持的Excel版本包裹2003,2007等等(Excel versions >= 97)。
大凡開源項目的做者,多半都是重口味者,MyXls開源組件基於的技術是Excel文件的二進制格式(BIFF),    OpenOffice.org 發佈過的倆個文檔 Excel File Format (BIFF8) Specification Microsoft Compound Document (OLE2) Format Specification Excel 的二進制格式作了一個比較詳細的說明,MyXls的做者正是憑藉這些信息把它開發而成的。
MyXls的下載地址:MyXls
 
下面經過2個例子來使用這個開源組件
生成單個Worksheet:
                        XlsDocument doc = new XlsDocument();
                        doc.FileName = "MyXlsWebAppDemo.xls";
                        Worksheet sheet = doc.Workbook.Worksheets.Add( "Hello World Sheet");
                        Cell cell = sheet.Cells.Add(1, 1, "Hello,MyXls!");
                         for ( int i = 2; i <= 10; i++)
                        {
                                cell = sheet.Cells.Add(i, 1, "51CTO五歲了!");
                                cell.Font.Weight = FontWeight.Bold;
                                cell.Font.ColorIndex =2; //白 紅 綠 藍 黃 粉紅等等顏色,能夠經過源代碼瞭解顏色
                        }
                        
                        doc.Send();
 
生成的報表以下:
 
生成多個WorkSheet
XlsDocument xls = new XlsDocument(); //新建一個xls文檔
                        xls.FileName = "MyXlsDemo.xls"; //設定Excel文件名

                        xls.SummaryInformation.Author = "Terry Li"; //填加Excel文件做者信息
                        xls.SummaryInformation.Subject = "MyXls Demo"; //填加文件主題信息
                        xls.DocumentSummaryInformation.Company = "in2bits.org"; //填加文件公司信息

string sheetName = "第一個Sheet Demo"; #region    

                        
string sheetName = "第一個Sheet Demo";
                        Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName); //填加名爲"第一個Sheet Demo"的sheet頁
                        Cells cells = sheet.Cells; //Cells實例是sheet頁中單元格(cell)集合
                         //單元格1-base
                        Cell cell = cells.Add(2, 3, "三"); //設定第2行,第3例單元格的值
                        cell.HorizontalAlignment = HorizontalAlignments.Centered; //設定文字居中
                        cell.Font.FontName = "行楷"; //設定字體
                        cell.Font.Height = 30 * 20; //設定字大小(字體大小是以 1/20 point 爲單位的)
                        cell.UseBorder = true; //使用邊框
                        cell.BottomLineStyle = 2; //設定邊框底線爲粗線
                        cell.BottomLineColor = Colors.Red; //設定顏色爲紅色
                        cell.RightLineStyle = 2;
                        cell.RightLineColor = Colors.Red;
                        
                        

                         //cell的格式還能夠定義在一個xf對象中
                        XF cellXF = xls.NewXF(); //爲xls生成一個XF實例(XF是cell格式對象)
                        cellXF.HorizontalAlignment = HorizontalAlignments.Centered; //設定文字居中
                        cellXF.Font.FontName = "隸書"; //設定字體
                        cellXF.Font.Height = 30 * 20; //設定字大小(字體大小是以 1/20 point 爲單位的)
                        cellXF.UseBorder = true; //使用邊框
                        cellXF.BottomLineStyle = 2; //設定邊框底線爲粗線
                        cellXF.BottomLineColor = Colors.Green; //設定顏色爲綠色
                        cellXF.LeftLineStyle = 2; //設定邊框左線爲粗線
                        cellXF.LeftLineColor = Colors.Green;

                        cell = cells.Add(3, 3, "國", cellXF); //以設定好的格式填加cell

                        cellXF.Font.FontName = "仿宋_GB2312";
                        cellXF.BottomLineStyle = 2; //設定邊框底線爲粗線
                        cellXF.BottomLineColor = Colors.Blue; //設定顏色爲藍色
                        cellXF.RightLineStyle = 2; //設定邊框右線爲粗線
                        cellXF.RightLineColor = Colors.Blue; //設定顏色爲藍色
                        cellXF.LeftLineStyle = 0;
                        cell = cells.Add(4, 3, "志", cellXF); //格式能夠屢次使用

                         //ColumnInfo colInfo = new ColumnInfo(xls, sheet);//生成列格式對象
                         ////設定colInfo格式的起做用的列爲第2列到第5列(列格式爲0-base)
                         //colInfo.ColumnIndexStart = 1;//起始列爲第二列
                         //colInfo.ColumnIndexEnd = 5;//終止列爲第六列
                         //colInfo.Width = 15 * 256;//列的寬度計量單位爲 1/256 字符寬
                         //sheet.AddColumnInfo(colInfo);//把格式附加到sheet頁上(注:AddColumnInfo方法有點小問題,不給把colInfo對象屢次附給sheet頁)
                         //colInfo.ColumnIndexEnd = 6;//能夠更改列對象的值
                         //ColumnInfo colInfo2 = new ColumnInfo(xls, sheet);//經過新生成一個列格式對象,纔到能設定其它列寬度
                         //colInfo2.ColumnIndexStart = 7;
                         //colInfo2.ColumnIndexEnd = 8;
                         //colInfo2.Width = 20 * 256;
                         //sheet.AddColumnInfo(colInfo2);

                        MergeArea meaA = new MergeArea(2, 3, 5, 7); //一個合併單元格實例(合併第2行、第5例 到 第3行、第7例)
                        sheet.AddMergeArea(meaA); //填加合併單元格
                        cellXF.VerticalAlignment = VerticalAlignments.Centered;
                        cellXF.Font.FontName = "隸書";
                         //cellXF.Font.Height = 48 * 20;
                         //cellXF.Font.Bold = true;
                        cellXF.Pattern = 1; //設定單元格填充風格。若是設定爲0,則是純色填充(無色),1表明沒有間隙的實色
                        cellXF.PatternBackgroundColor = Colors.Red; //填充的底色
                        cellXF.PatternColor = Colors.Green; //設定填充線條的顏色
                        cell = cells.Add(2, 5, "晉/陳壽", cellXF);
                        #endregion

                        sheet.Cells.Merge(7, 9, 1, 4);
                        cell = cells.Add(7, 1, "MyXls 合併單元格 Demo");
                        cell.HorizontalAlignment = HorizontalAlignments.Centered;
                        cell.VerticalAlignment = VerticalAlignments.Centered;

                         for ( int sheetNumber = 1; sheetNumber <= 4; sheetNumber++)
                        {
                                sheetName = "Sheet " + sheetNumber;
                                 int rowMin = sheetNumber;
                                 int rowCount = sheetNumber + 10;
                                 int colMin = sheetNumber;
                                 int colCount = sheetNumber + 10;
                                sheet = xls.Workbook.Worksheets.Add(sheetName);
                                cells = sheet.Cells;
                                 for ( int r = 0; r < rowCount; r++)
                                {
                                         if (r == 0)
                                        {
                                                 for ( int c = 0; c < colCount; c++)
                                                {
                                                        cells.Add(rowMin + r, colMin + c, "Column" + (c + 1)).Font.Bold = true;
                                                }
                                        }
                                         else
                                        {
                                                 for ( int c = 0; c < colCount; c++)
                                                {
                                                         int val = r + c;
                                                        cell = cells.Add(rowMin + r, colMin + c, val+ ":51CTO五歲了!");
                                                         if (val % 2 != 0)
                                                        {
                                                                cell.HorizontalAlignment = HorizontalAlignments.Centered;
                                                                cell.Font.FontName = "Times New Roman";
                                                                cell.Font.Underline = UnderlineTypes.Double;
                                                                cell.Font.ColorIndex = 2;
                                                                cell.Rotation = 45; //字符傾斜45度
                                                        }
                                                }
                                        }
                                }
                        }

                        xls.Send(); //XlsDocument.SendMethods.Inline
 
 
相關文章
相關標籤/搜索