您的位置:首页 > 编程语言 > ASP

Excel操作帮助类 (基于Aspose.Cells.dll)

2017-11-14 11:54 405 查看
基于Aspose.Cells.dll 封装了对于导出的Excel的各种样式设置,内容填充操作,目前支持边框样式,颜色,字体,合并单元格等操作,简化Aspose.Cells.dll的使用

调用示例

/// ---------->Clom Y
/// |
/// |
/// |
/// \/ Row X
static void Main(string[] args)
{
object[] clom = { "列名1", "列名2", "列名3" };
object[] row = { "行名1", "行名2", "行名3", "行名4" };
String filename = "text.xlsx";

//列标题样式
CellStyle Styleclom = new CellStyle();
Styleclom.AllBorder = Aspose.Cells.CellBorderType.Thin;
Styleclom.ForegroundColor = Color.Yellow;
Styleclom.IsBold = true;
//行标题样式
CellStyle Stylerow = new CellStyle();
Stylerow.AllBorder = Aspose.Cells.CellBorderType.Thin;
Stylerow.ForegroundColor = Color.ForestGreen;
Stylerow.IsBold = true;
//单元格样式
CellStyle Stylebody = new CellStyle();
Stylebody.AllBorder = Aspose.Cells.CellBorderType.Medium;
Stylebody.ForegroundColor = Color.LightBlue;
Stylebody.IsBold = true;
Stylebody.IsItalic = true;

//将样式和内容填充到模板中
ExcelFormat eformat = new ExcelFormat();
eformat.SavePath = filename;
eformat.ColumnsSize = 20;
eformat.RowsSize = 20;
//直接插入标题
//eformat.InsertTitle(clom.ToList(), Styleclom,  ExcelFormat.TitleType.列标题);
//eformat.InsertTitle(row.ToList(), Stylerow, ExcelFormat.TitleType.行标题);

eformat.InsertCellRow(new CellRow(1, 4, 0, clom.ToList()), Stylerow);
eformat.InsertCellColm(new CellColm(1, 5, 0, row.ToList()), Styleclom);

for (int i = 0; i < clom.Length; i++)
{
for (int j = 0; j < row.Length; j++)
{
SCell scell = new SCell();
scell.Txt_Obj = Convert.ToString(row[j]) + Convert.ToString(row[i]);
scell.X = j + 1;
scell.Y = i + 1;
scell.CStyle = Stylebody;
eformat.SCells.Add(scell);
}
}
//向Excel中写入数据
ExcelMethod.InsertData(eformat, true);

Console.WriteLine("完毕");
Console.ReadLine();
}


导出例子



GitHub地址

2017/11/15更新后 不再对所谓的标题行标题列作区分(在ExcelFormat对象中只保留SCells属性,即可配置样式的单元格集合。除此之外,新增了数据行,数据列,数据区块的概念,方便一组规则且具有相同样式的数据区块插入。为确保配置样式和插入的灵活性,所有的单元格最终汇总到SCells中等待写入)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel