myxls简介

1、添加引用到你的网站或项目中:
添加Myxls引用到项目

  添加Myxls引用到项目

2、一个导出excel的测试程序[3]
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ExportBtn_Click(object sender, EventArgs e) {
XlsDocument xls = new XlsDocument();
xls.FileName = "TestList.xls";
int rowIndex = 1;
Worksheet sheet = xls.Workbook.Worksheets.Add("测试表");//Sheet名称
Cells cells = sheet.Cells;
Cell cell = cells.Add(1, 1, "编号");
cell.Font.Bold = true;
cell = cells.Add(1, 2, "名称");
cell.Font.Bold = true;
foreach (DataRow row in table.Rows) {
cells.Add(rowIndex, 1, rowIndex);
cells.Add(rowIndex, 2, "名称"+rowIndex);
rowIndex++;
}
xls.Send();
}

3详细设置编辑

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