Excel操作幫助類 (基於Aspose.Cells.dll)
阿新 • • 發佈:2019-02-05
基於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();
}
匯出例子
2017/11/15更新後 不再對所謂的標題行標題列作區分(在ExcelFormat物件中只保留SCells屬性,即可配置樣式的單元格集合。除此之外,新增了資料行,資料列,資料區塊的概念,方便一組規則且具有相同樣式的資料區塊插入。為確保配置樣式和插入的靈活性,所有的單元格最終彙總到SCells中等待寫入)