首页 技术 正文
技术 2022年11月13日
0 收藏 497 点赞 3,129 浏览 6457 个字

MyXls是一个操作Excel的开源类库,支持设置字体、列宽、行高(由BOSSMA实现)、合并单元格、边框、背景颜色、数据类型、自动换行、对齐方式等,通过众多项目的使用表现,证明MyXls对于创建简单格式的Excel文件十分快捷方便。

本文将通过实例的方式详细说明如何通过各种属性设置MyXls的样式,并附带示例程序的源代码。

// 准备测试数据
List<PersonInfo> list = new List<PersonInfo>();
for (int i = ; i <= ; i++)
{
PersonInfo person = new PersonInfo()
{
RealName = "张" + i,
Gender = (i % == ? "男" : "女"),
Age = + (i % )
}; list.Add(person);
}int recordCount = ; // 要导出的记录总数
int maxRecordCount = ; // 每个sheet表的最大记录数
int sheetCount = ; // Sheet表的数目XlsDocument xls = new XlsDocument();
xls.FileName = "MyXls-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";// 计算需要多少个sheet表显示数据
if (recordCount > maxRecordCount)
{
sheetCount = (int)Math.Ceiling((decimal)recordCount / (decimal)maxRecordCount);
}// Sheet标题样式
XF titleXF = xls.NewXF(); // 为xls生成一个XF实例,XF是单元格格式对象
titleXF.HorizontalAlignment = HorizontalAlignments.Centered; // 设定文字居中
titleXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中
titleXF.UseBorder = true; // 使用边框
titleXF.TopLineStyle = ; // 上边框样式
titleXF.TopLineColor = Colors.Black; // 上边框颜色
titleXF.LeftLineStyle = ; // 左边框样式
titleXF.LeftLineColor = Colors.Black; // 左边框颜色
titleXF.RightLineStyle = ; // 右边框样式
titleXF.RightLineColor = Colors.Black; // 右边框颜色
titleXF.Font.FontName = "宋体"; // 字体
titleXF.Font.Bold = true; // 是否加楚
titleXF.Font.Height = * ; // 字大小(字体大小是以 1/20 point 为单位的)// 列标题样式
XF columnTitleXF = xls.NewXF(); // 为xls生成一个XF实例,XF是单元格格式对象
columnTitleXF.HorizontalAlignment = HorizontalAlignments.Centered; // 设定文字居中
columnTitleXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中
columnTitleXF.UseBorder = true; // 使用边框
columnTitleXF.TopLineStyle = ; // 上边框样式
columnTitleXF.TopLineColor = Colors.Black; // 上边框颜色
columnTitleXF.BottomLineStyle = ; // 下边框样式
columnTitleXF.BottomLineColor = Colors.Black; // 下边框颜色
columnTitleXF.LeftLineStyle = ; // 左边框样式
columnTitleXF.LeftLineColor = Colors.Black; // 左边框颜色
columnTitleXF.Pattern = ; // 单元格填充风格。如果设定为0,则是纯色填充(无色),1代表没有间隙的实色
columnTitleXF.PatternBackgroundColor = Colors.Red; // 填充的底色
columnTitleXF.PatternColor = Colors.Default2F; // 填充背景色// 数据单元格样式
XF dataXF = xls.NewXF(); // 为xls生成一个XF实例,XF是单元格格式对象
dataXF.HorizontalAlignment = HorizontalAlignments.Centered; // 设定文字居中
dataXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中
dataXF.UseBorder = true; // 使用边框
dataXF.LeftLineStyle = ; // 左边框样式
dataXF.LeftLineColor = Colors.Black; // 左边框颜色
dataXF.BottomLineStyle = ; // 下边框样式
dataXF.BottomLineColor = Colors.Black; // 下边框颜色
dataXF.Font.FontName = "宋体";
dataXF.Font.Height = * ; // 设定字大小(字体大小是以 1/20 point 为单位的)
dataXF.UseProtection = false; // 默认的就是受保护的,导出后需要启用编辑才可修改
dataXF.TextWrapRight = true; // 自动换行// 遍历创建Sheet
for (int i = ; i <= sheetCount; i++)
{
// 根据计算出来的Sheet数量,一个个创建
// 行和列的设置需要添加到指定的Sheet中,且每个设置对象不能重用(因为可以设置起始和终止行或列,就没有太大必要重用了,这应是一个策略问题)
Worksheet sheet;
if (sheetCount == )
{
sheet = xls.Workbook.Worksheets.Add("人员信息表");
}
else
{
sheet = xls.Workbook.Worksheets.Add("人员信息表 - " + i);
} // 序号列设置
ColumnInfo col0 = new ColumnInfo(xls, sheet); // 列对象
col0.ColumnIndexStart = ; // 起始列为第1列,索引从0开始
col0.ColumnIndexEnd = ; // 终止列为第1列,索引从0开始
col0.Width = * ; // 列的宽度计量单位为 1/256 字符宽
sheet.AddColumnInfo(col0); // 把格式附加到sheet页上 // 姓名列设置
ColumnInfo col1 = new ColumnInfo(xls, sheet); // 列对象
col1.ColumnIndexStart = ; // 起始列为第2列,索引从0开始
col1.ColumnIndexEnd = ; // 终止列为第2列,索引从0开始
col1.Width = * ; // 列的宽度计量单位为 1/256 字符宽
sheet.AddColumnInfo(col1); // 把格式附加到sheet页上 // 性别列设置
ColumnInfo col2 = new ColumnInfo(xls, sheet); // 列对象
col2.ColumnIndexStart = ; // 起始列为第3列,索引从0开始
col2.ColumnIndexEnd = ; // 终止列为第3列,索引从0开始
col2.Width = * ; // 列的宽度计量单位为 1/256 字符宽
sheet.AddColumnInfo(col2); // 把格式附加到sheet页上 // 年龄列设置
ColumnInfo col3 = new ColumnInfo(xls, sheet); // 列对象
col3.ColumnIndexStart = ; // 起始列为第4列,索引从0开始
col3.ColumnIndexEnd = ; // 终止列为第4列,索引从0开始
col3.Width = * ; // 列的宽度计量单位为 1/256 字符宽
sheet.AddColumnInfo(col3); // 把格式附加到sheet页上 // 行设置
RowInfo rol1 = new RowInfo(); // 行对象
rol1.RowHeight = * ; // 行高
rol1.RowIndexStart = ; // 行设置起始列,索引从1开始
rol1.RowIndexEnd = (ushort)(maxRecordCount + ); //行设置结束列
sheet.AddRowInfo(rol1); // 把设置附加到sheet页上 // 合并单元格
//sheet.Cells.Merge(1, 1, 1, 4);
MergeArea titleArea = new MergeArea(, , , ); // 一个合并单元格实例(合并第1行、第1列 到 第1行、第4列)
sheet.AddMergeArea(titleArea); //填加合并单元格 // 开始填充数据到单元格
Cells cells = sheet.Cells; // Sheet标题行,行和列的索引都是从1开始的
Cell cell = cells.Add(, , "人员信息统计表", titleXF);
cells.Add(, , "", titleXF); // 合并单元格后仍需要设置每一个单元格,样式才有效
cells.Add(, , "", titleXF); // 合并单元格后仍需要设置每一个单元格,样式才有效
cells.Add(, , "", titleXF); // 合并单元格后仍需要设置每一个单元格,样式才有效
sheet.Rows[].RowHeight = * ; // 对指定的行设置行高 // 列标题行
cells.Add(, , "序号", columnTitleXF);
cells.Add(, , "姓名", columnTitleXF);
cells.Add(, , "性别", columnTitleXF); // 最右侧的列需要右边框,通过修改样式columnTitleXF的方式,还可以通过设置单元格属性的方式实现。
columnTitleXF.RightLineStyle = ;
columnTitleXF.RightLineColor = Colors.Black;
cells.Add(, , "年龄", columnTitleXF); sheet.Rows[].RowHeight = * ; // 对指定的行设置行高 // 行索引
int rowIndex = ; for (int j = ; j < maxRecordCount; j++)
{
// 当前记录在数据集合中的索引
int k = (i - ) * maxRecordCount + j; // 如果达到sheet最大记录数则跳出
if (k >= recordCount)
{
break;
} // 设置单元格的值
cells.Add(rowIndex, , k + , dataXF);
cells.Add(rowIndex, , list[k].RealName, dataXF);
cells.Add(rowIndex, , list[k].Gender, dataXF); // 最右侧的列需要右边框,通过给Cell设置属性的方式实现,因为并不是所有的单元格都需要设置,不能通过修改样式dataXF的方式
Cell lastCell = cells.Add(rowIndex, , list[k].Age, dataXF);
lastCell.RightLineStyle = ;
lastCell.RightLineColor = Colors.Black; // 行号递增
rowIndex++;
}
}// 在浏览器中输出Excel文件
xls.Send();

1.创建一个Excel文档:

XlsDocument xls = new XlsDocument();

2.创建一个WorkSheet:

Worksheet ws = xls.Workbook.Worksheets.Add(“WorkSheet1”);

3.指定列格式:

ColumnInfo colInfo = new ColumnInfo(xls, ws);
colInfo.ColumnIndexStart = ;
colInfo.ColumnIndexEnd = 17;
colInfo.Width = 15 * 256;
ws.AddColumnInfo(colInfo);

列格式必须每次都要重新定义,一个列格式不能重复使用。

4.指定单元格样式:

XF xf = xls.NewXF();
xf.HorizontalAlignment = HorizontalAlignments.Centered;
xf.VerticalAlignment = VerticalAlignments.Centered;
xf.Pattern = 1;
xf.PatternColor = Colors.Default30;
xf.UseBorder = true;
xf.TopLineStyle = 1;
xf.TopLineColor = Colors.Black;
xf.BottomLineStyle = 1;
xf.BottomLineColor = Colors.Black;
xf.LeftLineStyle = 1;
xf.LeftLineColor = Colors.Black;
xf.RightLineStyle = 1;
xf.RightLineColor = Colors.Black;
xf.Font.Bold = true;
xf.Font.Height = 11 * 20;
xf.Font.ColorIndex = 1;

5.给单元格赋值:

ws.Cells.Add(2, 3, “金额(万元)”, xf);

6.合并单元格:

ws.Cells.Merge(1, 2, 2, 2);
//或者
ws.AddMergeArea(new MergeArea(1, 2, 1, 1));

7.MyXls合并单元格有个bug,就是合并后只是第一个单元格有样式,其余的样式丢失。所以写了个函数来合并:

MergeRegion(ref ws, xf, “机构”, 1, 1, 2, 1);

public void MergeRegion(ref Worksheet ws, XF xf, string title, int startRow, int startCol, intendRow, int endCol)
{
      for (int i = startCol; i <= endCol; i++)
       {
            for (int j = startRow; j <= endRow; j++)
             {
                 ws.Cells.Add(j, i, title, xf);
             }
       }
       ws.Cells.Merge(startRow, endRow, startCol, endCol);
}

虽然效率不怎么样,但是对于出Excel报表,还OK。

8.指定单元格格式

cell.Format = StandardFormats.Decimal_1;

具体更多请参考源代码的StandardFormats类。

9.保存或者发送Excel:

xls.Send();
//或者
xls.Save();

MyXls下载地址:(我下载的版本是0.63,很多地方下载的版本是0.51,老版本没有单元格合并等功能)

http://sourceforge.net/projects/myxls/files/

本博客所有文章波斯马,原文地址《MyXls导出Excel的各种设置

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297