博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
优秀开源项目:MyXls
阅读量:6070 次
发布时间:2019-06-20

本文共 4847 字,大约阅读时间需要 16 分钟。

如果从快速生成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),    
发布过的俩个文档
Excel
的二进制格式做了一个比较详细的说明,MyXls的作者正是凭借这些信息把它开发而成的。
MyXls的下载地址:
 
下面通过2个例子来使用这个开源组件
生成单个Worksheet:
InBlock.gif                        XlsDocument doc = 
new XlsDocument(); 

InBlock.gif                        doc.FileName = 
"MyXlsWebAppDemo.xls"

InBlock.gif                        Worksheet sheet = doc.Workbook.Worksheets.Add(
"Hello World Sheet"); 

InBlock.gif                        Cell cell = sheet.Cells.Add(1, 1, 
"Hello,MyXls!"); 

InBlock.gif                        
for (
int i = 2; i <= 10; i++) 

InBlock.gif                        { 

InBlock.gif                                cell = sheet.Cells.Add(i, 1, 
"51CTO五岁了!"); 

InBlock.gif                                cell.Font.Weight = FontWeight.Bold; 

InBlock.gif                                cell.Font.ColorIndex =2;
//白 红 绿 蓝 黄 粉红等等颜色,可以通过源代码了解颜色 

InBlock.gif                        } 

InBlock.gif                         

InBlock.gif                        doc.Send();
 
生成的报表如下:
 
生成多个WorkSheet
InBlock.gif XlsDocument xls = 
new XlsDocument();
//新建一个xls文档 

InBlock.gif                        xls.FileName = 
"MyXlsDemo.xls";
//设定Excel文件名 

InBlock.gif 

InBlock.gif                        xls.SummaryInformation.Author = 
"Terry Li"
//填加Excel文件作者信息 

InBlock.gif                        xls.SummaryInformation.Subject = 
"MyXls Demo";
//填加文件主题信息 

InBlock.gif                        xls.DocumentSummaryInformation.Company = 
"in2bits.org";
//填加文件公司信息 

InBlock.gif 

InBlock.gif
string sheetName = 
"第一个Sheet Demo";
#region    
InBlock.gif 
InBlock.gif                        
string sheetName = 
"第一个Sheet Demo"

InBlock.gif                        Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName);
//填加名为"第一个Sheet Demo"的sheet页 

InBlock.gif                        Cells cells = sheet.Cells;
//Cells实例是sheet页中单元格(cell)集合 

InBlock.gif                        
//单元格1-base 

InBlock.gif                        Cell cell = cells.Add(2, 3, 
"三");
//设定第2行,第3例单元格的值 

InBlock.gif                        cell.HorizontalAlignment = HorizontalAlignments.Centered;
//设定文字居中 

InBlock.gif                        cell.Font.FontName = 
"行楷";
//设定字体 

InBlock.gif                        cell.Font.Height = 30 * 20;
//设定字大小(字体大小是以 1/20 point 为单位的) 

InBlock.gif                        cell.UseBorder = 
true;
//使用边框 

InBlock.gif                        cell.BottomLineStyle = 2;
//设定边框底线为粗线 

InBlock.gif                        cell.BottomLineColor = Colors.Red;
//设定颜色为红色 

InBlock.gif                        cell.RightLineStyle = 2; 

InBlock.gif                        cell.RightLineColor = Colors.Red; 

InBlock.gif                         

InBlock.gif                         

InBlock.gif 

InBlock.gif                        
//cell的格式还可以定义在一个xf对象中 

InBlock.gif                        XF cellXF = xls.NewXF();
//为xls生成一个XF实例(XF是cell格式对象) 

InBlock.gif                        cellXF.HorizontalAlignment = HorizontalAlignments.Centered;
//设定文字居中 

InBlock.gif                        cellXF.Font.FontName = 
"隶书";
//设定字体 

InBlock.gif                        cellXF.Font.Height = 30 * 20;
//设定字大小(字体大小是以 1/20 point 为单位的) 

InBlock.gif                        cellXF.UseBorder = 
true;
//使用边框 

InBlock.gif                        cellXF.BottomLineStyle = 2;
//设定边框底线为粗线 

InBlock.gif                        cellXF.BottomLineColor = Colors.Green;
//设定颜色为绿色 

InBlock.gif                        cellXF.LeftLineStyle = 2; 
//设定边框左线为粗线 

InBlock.gif                        cellXF.LeftLineColor = Colors.Green; 

InBlock.gif 

InBlock.gif                        cell = cells.Add(3, 3, 
"国", cellXF);
//以设定好的格式填加cell 

InBlock.gif 

InBlock.gif                        cellXF.Font.FontName = 
"仿宋_GB2312"

InBlock.gif                        cellXF.BottomLineStyle = 2; 
//设定边框底线为粗线 

InBlock.gif                        cellXF.BottomLineColor = Colors.Blue;
//设定颜色为蓝色 

InBlock.gif                        cellXF.RightLineStyle = 2;
//设定边框右线为粗线 

InBlock.gif                        cellXF.RightLineColor = Colors.Blue;
//设定颜色为蓝色 

InBlock.gif                        cellXF.LeftLineStyle = 0; 

InBlock.gif                        cell = cells.Add(4, 3, 
"志", cellXF);
//格式可以多次使用 

InBlock.gif 

InBlock.gif                        
//ColumnInfo colInfo = new ColumnInfo(xls, sheet);//生成列格式对象 

InBlock.gif                        
设定colInfo格式的起作用的列为第2列到第5列(列格式为0-base

InBlock.gif                        
//colInfo.ColumnIndexStart = 1;//起始列为第二列 

InBlock.gif                        
//colInfo.ColumnIndexEnd = 5;//终止列为第六列 

InBlock.gif                        
//colInfo.Width = 15 * 256;//列的宽度计量单位为 1/256 字符宽 

InBlock.gif                        
//sheet.AddColumnInfo(colInfo);//把格式附加到sheet页上(注:AddColumnInfo方法有点小问题,不给把colInfo对象多次附给sheet页) 

InBlock.gif                        
//colInfo.ColumnIndexEnd = 6;//可以更改列对象的值 

InBlock.gif                        
//ColumnInfo colInfo2 = new ColumnInfo(xls, sheet);//通过新生成一个列格式对象,才到能设定其它列宽度 

InBlock.gif                        
//colInfo2.ColumnIndexStart = 7; 

InBlock.gif                        
//colInfo2.ColumnIndexEnd = 8; 

InBlock.gif                        
//colInfo2.Width = 20 * 256; 

InBlock.gif                        
//sheet.AddColumnInfo(colInfo2); 

InBlock.gif 

InBlock.gif                        MergeArea meaA = 
new MergeArea(2, 3, 5, 7);
//一个合并单元格实例(合并第2行、第5例 到 第3行、第7例) 

InBlock.gif                        sheet.AddMergeArea(meaA);
//填加合并单元格 

InBlock.gif                        cellXF.VerticalAlignment = VerticalAlignments.Centered; 

InBlock.gif                        cellXF.Font.FontName = 
"隶书"

InBlock.gif                        
//cellXF.Font.Height = 48 * 20; 

InBlock.gif                        
//cellXF.Font.Bold = true; 

InBlock.gif                        cellXF.Pattern = 1;
//设定单元格填充风格。如果设定为0,则是纯色填充(无色),1代表没有间隙的实色 

InBlock.gif                        cellXF.PatternBackgroundColor = Colors.Red;
//填充的底色 

InBlock.gif                        cellXF.PatternColor = Colors.Green;
//设定填充线条的颜色 

InBlock.gif                        cell = cells.Add(2, 5, 
"晋/陈寿", cellXF); 

InBlock.gif                        #endregion 

InBlock.gif 

InBlock.gif                        sheet.Cells.Merge(7, 9, 1, 4); 

InBlock.gif                        cell = cells.Add(7, 1, 
"MyXls 合并单元格 Demo"); 

InBlock.gif                        cell.HorizontalAlignment = HorizontalAlignments.Centered; 

InBlock.gif                        cell.VerticalAlignment = VerticalAlignments.Centered; 

InBlock.gif 

InBlock.gif                        
for (
int sheetNumber = 1; sheetNumber <= 4; sheetNumber++) 

InBlock.gif                        { 

InBlock.gif                                sheetName = 
"Sheet " + sheetNumber; 

InBlock.gif                                
int rowMin = sheetNumber; 

InBlock.gif                                
int rowCount = sheetNumber + 10; 

InBlock.gif                                
int colMin = sheetNumber; 

InBlock.gif                                
int colCount = sheetNumber + 10; 

InBlock.gif                                sheet = xls.Workbook.Worksheets.Add(sheetName); 

InBlock.gif                                cells = sheet.Cells; 

InBlock.gif                                
for (
int r = 0; r < rowCount; r++) 

InBlock.gif                                { 

InBlock.gif                                        
if (r == 0) 

InBlock.gif                                        { 

InBlock.gif                                                
for (
int c = 0; c < colCount; c++) 

InBlock.gif                                                { 

InBlock.gif                                                        cells.Add(rowMin + r, colMin + c, 
"Column" + (c + 1)).Font.Bold = 
true

InBlock.gif                                                } 

InBlock.gif                                        } 

InBlock.gif                                        
else 

InBlock.gif                                        { 

InBlock.gif                                                
for (
int c = 0; c < colCount; c++) 

InBlock.gif                                                { 

InBlock.gif                                                        
int val = r + c; 

InBlock.gif                                                        cell = cells.Add(rowMin + r, colMin + c, val+ 
":51CTO五岁了!"); 

InBlock.gif                                                        
if (val % 2 != 0) 

InBlock.gif                                                        { 

InBlock.gif                                                                cell.HorizontalAlignment = HorizontalAlignments.Centered; 

InBlock.gif                                                                cell.Font.FontName = 
"Times New Roman"

InBlock.gif                                                                cell.Font.Underline = UnderlineTypes.Double; 

InBlock.gif                                                                cell.Font.ColorIndex = 2; 

InBlock.gif                                                                cell.Rotation = 45; 
//字符倾斜45度 

InBlock.gif                                                        } 

InBlock.gif                                                } 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        xls.Send();
//XlsDocument.SendMethods.Inline
 
 
本文转自terryli51CTO博客,原文链接:http://blog.51cto.com/terryli/392125 ,如需转载请自行联系原作者
你可能感兴趣的文章
iostat 命令监控磁盘IO
查看>>
java反射详解
查看>>
android无法识别adb devices解决方法
查看>>
站长常用服务器软件总结
查看>>
CSS纯图片圆角Box的实现技巧
查看>>
Maven:简介(1)
查看>>
安卓SDK 安装问题
查看>>
Kubernetes 1.8.4 手动安装教程-安装Etcd(二)
查看>>
解析各大电子商务网站订单号的生成方式
查看>>
IOS开发-Foundation笔记
查看>>
[转]Mybatis Plus 插件注册机
查看>>
UTF8 与 UTF8 +BOM 区别
查看>>
Mac系统升级到10.9(mavericks)时安装php扩展问题解决
查看>>
搬家!
查看>>
CSS技巧收集——巧用滤镜
查看>>
IDEA Tomcat 内存溢出
查看>>
.svn 一括削除
查看>>
Javascript通过bind()掌控this
查看>>
临时设置jdk环境变量
查看>>
ext
查看>>