优秀开源项目:MyXls
生活随笔
收集整理的這篇文章主要介紹了
优秀开源项目: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 CompoundDocument (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 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
????????????????????????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 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的优秀开源项目:MyXls的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 避免showModalDialog打开的
- 下一篇: 解决m2eclipse需要jdk的错误