示例#1
0
        public static Workbook CreateExcel(ExcelFormat FormatType, bool Save = true)
        {
            //实例化workbook对象
            Workbook workbook = new Workbook();
            //获得工作簿中的表
            Worksheet sheet = (Worksheet)workbook.Worksheets[FormatType.SheetIndex];
            //获得指定Sheet中的所有单元格
            Cells cells = sheet.Cells;

            //有行标题
            if (FormatType.Columns.Count > 0)
            {
                //设置第一行行高
                cells.SetRowHeight(0, FormatType.RowsSize);
                for (int i = 0; i < FormatType.Columns.Count; i++)
                {
                    //设置列宽
                    cells.SetColumnWidth(i, FormatType.ColumnsSize);
                    //单元格赋值
                    cells[0, FormatType.Columns[i].Y].PutValue(FormatType.Columns[i].Txt_Obj);
                    //设置样式
                    cells[0, FormatType.Columns[i].Y].SetStyle(FormatType.Columns[i].CreateStyle());
                }
            }
            //有行标题
            if (FormatType.Rows.Count > 0)
            {
                //设置第一列列宽
                cells.SetColumnWidth(0, FormatType.ColumnsSize);
                for (int i = 0; i < FormatType.Rows.Count; i++)
                {
                    //设置行高
                    cells.SetRowHeight(i, FormatType.RowsSize);
                    //单元格赋值
                    cells[FormatType.Rows[i].X, 0].PutValue(FormatType.Rows[i].Txt_Obj);
                    //设置样式
                    cells[FormatType.Rows[i].X, 0].SetStyle(FormatType.Rows[i].CreateStyle());
                    //cells[i, 0].SetStyle(FormatType.Rows[i].CreateStyle());
                }
            }

            if (FormatType.SCells.Count > 0)
            {
                foreach (var cell in FormatType.SCells)
                {
                    //设置行高
                    cells.SetRowHeight(cell.X, FormatType.RowsSize);
                    //设置列宽
                    cells.SetColumnWidth(cell.Y, FormatType.ColumnsSize);
                    //设置文字换行
                    //单元格赋值
                    cells[cell.X, cell.Y].PutValue(cell.Txt_Obj);
                    //设置样式
                    cells[cell.X, cell.Y].SetStyle(cell.CreateStyle());
                }
            }

            if (Save)
            {
                try
                {
                    workbook.Save(FormatType.SavePath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("保存失败");
                    throw ex;
                }
            }

            return(workbook);
        }