Aspose.cells常用用法1
代码:
var execl_path = @"G:\zhyue\backup\项目修改-工作日常\2018-11-12 区域楼盘中心点和放大比例计算\a.xlsx"; Workbook wb = new Workbook(); Worksheet sheet = wb.Worksheets[0]; //添加表头 sheet.Cells[0, 0].SetCell("区域", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 1].SetCell("商圈", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 2].SetCell("经度", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 3].SetCell("纬度", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 4].SetCell("实际距离", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 5].SetCell("缩放比例", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 6].SetCell("区域最大房源数", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 7].SetCell("区域最小房源数", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 8].SetCell("区域最大最小房源数比", Color.FromArgb(196, 248, 170)); sheet.Cells[0, 9].SetCell("执行级别(1-3)", Color.FromArgb(196, 248, 170)); int row = 1;//第几行 list_reach.ForEach(s => { int i = 1; GetResult(s.SQID, out longitude, out latitude, out distance, out scale, out max_com_num, out min_com_num, out max_min_scale, ref i); sheet.Cells[row, 0].SetCell(s.C_ReachName, Color.White); sheet.Cells[row, 1].SetCell(s.SQName, Color.White); sheet.Cells[row, 2].SetCell(longitude, Color.White); sheet.Cells[row, 3].SetCell(latitude, Color.White); sheet.Cells[row, 4].SetCell(distance, Color.White); sheet.Cells[row, 5].SetCell(scale, Color.White); sheet.Cells[row, 6].SetCell(max_com_num, Color.White); sheet.Cells[row, 7].SetCell(min_com_num, Color.White); sheet.Cells[row, 8].SetCell(max_min_scale, Color.White); sheet.Cells[row, 9].SetCell(i - 1, Color.White); row++; }); sheet.setColumnWithAuto(); wb.Save(execl_path);
引用扩展类
static class Cells1 { /// <summary> /// 设置cell的Value和Style /// </summary> /// <param name="cell"></param> /// <param name="name"></param> /// <param name="bgColor"></param> public static void SetCell(this Cell cell, object name, Color bgColor) { cell.PutValue(name);//单元格值 Style style = new CellsFactory().CreateStyle(); style.ForegroundColor = bgColor; style.Pattern = BackgroundType.Solid;//背景颜色不起作用,加入该行代码 style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线 style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线 style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线 style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线 style.HorizontalAlignment = TextAlignmentType.Center; style.VerticalAlignment = TextAlignmentType.Center; cell.SetStyle(style); } /// <summary> /// 设置表页的列宽度自适应 /// </summary> /// <param name="sheet">worksheet对象</param> public static void setColumnWithAuto(this Worksheet sheet) { Cells cells = sheet.Cells; int columnCount = cells.MaxColumn; //获取表页的最大列数 int rowCount = cells.MaxRow; //获取表页的最大行数 for (int col = 0; col <= columnCount; col++) { sheet.AutoFitColumn(col, 0, rowCount); } for (int col = 0; col <= columnCount; col++) { cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30); } } }