当前位置:网站首页>Based on infragistics Document. Excel export table class

Based on infragistics Document. Excel export table class

2022-07-06 17:26:00 Heart blue 168

I used to use Infragistics Of Web Control , Where export Excel part , Have their own Excel Derived , For your convenience , So it was encapsulated again .

I don't know if there are any peers with this component , Hope to useful !

More complete source code , Please visit Download

https://download.csdn.net/download/hyjchina/80481314

#region  Namespace 
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using Infragistics.Documents.Excel;
#endregion

namespace XL.OFFIC.OfficeClass
{
    #region  Page setup 
    /// <summary>
    ///  Page setup ( Direction )
    /// </summary>
    public enum PageOrientation
    {
        /// <summary>
        ///  The transverse 
        /// </summary>
        Landscape,
        /// <summary>
        ///  The longitudinal 
        /// </summary>
        Portrait
    }

    /// <summary>
    ///  The paper size 
    /// </summary>
    public enum PaperSize
    {
        /// <summary>
        /// A2
        /// </summary>
        A2,
        /// <summary>
        /// A3
        /// </summary>
        A3,
        /// <summary>
        /// A4
        /// </summary>
        A4,
        /// <summary>
        /// A5
        /// </summary>
        A5,
        /// <summary>
        /// A6
        /// </summary>
        A6,
        /// <summary>
        /// B4
        /// </summary>
        B4JIS,
        /// <summary>
        /// B5
        /// </summary>
        B5JIS,
        /// <summary>
        /// JapanesePost
        /// </summary>
        JapanesePost
    }
    #endregion

    #region  Common constants 
    static class ExcelDataConst
    {
        public const int colWidth = 256;        // Width conversion ratio 
        public const int rowHeight = 20;        // Height conversion ratio 
        public const double pageMargin = 0.4;   // Margin conversion ratio 
    }
    #endregion

    #region  Alignment mode 
    /// <summary>
    ///  Alignment mode 
    /// </summary>
    public enum ExcelHAlign
    {
        /// <summary>
        ///  Align left 
        /// </summary>
        Left,
        /// <summary>
        ///  Align center 
        /// </summary>
        Center,
        /// <summary>
        ///  Right alignment 
        /// </summary>
        Right
    }
    #endregion

    /// <summary>
    /// UltraExcel Operation class 
    /// </summary>
    public class UltraExcelClass
    {
        #region  Private member 
        private Workbook wBook;                         // working copy 
        private Worksheet wSheet;                       // workbook 
        private string templateFile = string.Empty;     // Full name of temporary file 
        private int defaultColumnsWidth = 10 * ExcelDataConst.colWidth;     // Default column width 
        private int defaultZoom = 100;                                      // Default page setup ( The zoom )
        private int defaultRowsHeight = 10 * ExcelDataConst.rowHeight;      // Default line height 
        private PageOrientation pageOrientation = PageOrientation.Portrait; // Default page setup ( Direction )
        private PaperSize paperSize = PaperSize.A4;     // Default paper 
        private bool centerHorizontally = false;        // Default horizontal centering 
        private bool centerVertically = false;          // Default vertical centering 
        private double pageLeftMargin = 1.9 * ExcelDataConst.pageMargin;    // Default left margin 
        private double pageRightMargin = 1.9 * ExcelDataConst.pageMargin;   // Default right margin 
        private double pageTopMargin = 2.5 * ExcelDataConst.pageMargin;     // Default top margin 
        private double pageBottomMargin = 2.5 * ExcelDataConst.pageMargin;  // Default bottom margin 
        private string columnNameList = "ABCDEFGHIJKLMNOPQRSTUVWSYZ";
        #endregion

        #region [ attribute ]WorkBook
        /// <summary>
        /// WorkBook
        /// </summary>
        public Workbook WorkBook
        {
            get
            {
                return this.wBook;
            }
            set
            {
                wBook = value;
            }
        }
        #endregion

        #region [ attribute ]WorkSheet
        /// <summary>
        /// WorkSheet
        /// </summary>
        public Worksheet WorkSheet
        {
            get
            {
                return this.wSheet;
            }
            set
            {
                wSheet = value;
            }
        }
        #endregion

        #region [ attribute ]Excel Temporary file name 
        /// <summary>
        /// Excel Temporary file name ( For use only Excel Template export method )
        /// </summary>
        public string TemplateFile
        {
            get
            {
                return this.templateFile;
            }
        }
        #endregion

        #region [ attribute ] Default column width 
        /// <summary>
        ///  Default column width 
        /// </summary>
        public int DefaultColumnsWidth
        {
            set
            {
                this.defaultColumnsWidth = value * ExcelDataConst.colWidth;
                this.SetDefaultColWidth();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( The zoom )
        /// <summary>
        ///  Page setup ( The zoom )
        /// </summary>
        public int DefaultZoom
        {
            set
            {
                this.defaultZoom = value;
                this.SetPageZoom();
            }
        }
        #endregion

        #region [ attribute ] Default line number 
        /// <summary>
        ///  Default line number 
        /// </summary>
        public int DefaultRowsHeight
        {
            set
            {
                this.defaultRowsHeight = value * ExcelDataConst.rowHeight;
                this.SetDefaultRowHeight();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Direction )
        /// <summary>
        ///  Page setup ( Direction )
        /// </summary>
        public PageOrientation PageOrientation
        {
            set
            {
                this.pageOrientation = value;
                this.SetPageOrientation();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( The paper size )
        /// <summary>
        ///   Page setup ( The paper size )
        /// </summary>
        public PaperSize PaperSize
        {
            set
            {
                this.paperSize = value;
                this.SetPaperSize();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Horizontal center )
        /// <summary>
        ///  Page setup ( Horizontal center )
        /// </summary>
        public bool CenterHorizontally
        {
            set
            {
                this.centerHorizontally = value;
                this.SetCenterHorizontally();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Vertically centered )
        /// <summary>
        ///  Page setup ( Vertically centered )
        /// </summary>
        public bool CenterVertically
        {
            set
            {
                this.centerVertically = value;
                this.SetCenterVertically();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Page margins )
        /// <summary>
        ///  Page setup ( Page margins )
        /// </summary>
        public double PageLeftMargin
        {
            set
            {
                this.pageLeftMargin = value * ExcelDataConst.pageMargin;
                this.SetPageMargin();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Page margins )
        /// <summary>
        ///  Page setup ( Page margins )
        /// </summary>
        public double PageRightMargin
        {
            set
            {
                this.pageRightMargin = value * ExcelDataConst.pageMargin;
                this.SetPageMargin();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Page margins )
        /// <summary>
        ///  Page setup ( Page margins )
        /// </summary>
        public double PageTopMargin
        {
            set
            {
                this.pageTopMargin = value * ExcelDataConst.pageMargin;
                this.SetPageMargin();
            }
        }
        #endregion

        #region [ attribute ] Page setup ( Page margins )
        /// <summary>
        ///  Page setup ( Page margins )
        /// </summary>
        public double PageBottomMargin
        {
            set
            {
                this.pageBottomMargin = value * ExcelDataConst.pageMargin;
                this.SetPageMargin();
            }
        }
        #endregion

        #region [ Method ] String inversion 
        /// <summary>
        ///  String inversion ( Such as :ABC To CBA)
        /// </summary>
        /// <param name="str"> character string </param>
        /// <returns> Result string </returns>
        private string StringReverse(string str)
        {
            return new string(str.ToCharArray().Reverse<char>().ToArray<char>());
        }
        #endregion

        #region [ Method ] Constructors 
        /// <summary>
        ///  Constructors 
        /// </summary>
        /// <param name="tempFileFullName"> Temporary full name </param>
        public UltraExcelClass(string tempFileFullName)
        {
            try
            {
                this.templateFile = tempFileFullName;

                this.wBook = new Workbook();
                this.wBook = Workbook.Load(this.templateFile);
                this.wSheet = this.wBook.Worksheets[0];
                this.wBook.WindowOptions.SelectedWorksheet = this.wSheet;
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
        }
        #endregion

        #region [ Method ] Set the default column width 
        /// <summary>
        ///  Set the default column width 
        /// </summary>
        private void SetDefaultColWidth()
        {
            this.wSheet.DefaultColumnWidth = defaultColumnsWidth;
        }
        #endregion

        #region [ Method ] Set the default row height 
        /// <summary>
        ///  Set the default row height 
        /// </summary>
        private void SetDefaultRowHeight()
        {
            this.wSheet.DefaultRowHeight = defaultRowsHeight;
        }
        #endregion

        #region [ Method ] Page setup ( Direction )
        /// <summary>
        ///  Page setup ( Direction )
        /// </summary>
        private void SetPageOrientation()
        {
            if (pageOrientation == PageOrientation.Landscape)
            {
                this.wSheet.PrintOptions.Orientation = Infragistics.Documents.Excel.Orientation.Landscape;
            }
            else
            {
                this.wSheet.PrintOptions.Orientation = Infragistics.Documents.Excel.Orientation.Portrait;
            }
        }
        #endregion

        #region [ Method ] Page setup ( The paper size )
        /// <summary>
        ///  Page setup ( The paper size )
        /// </summary>
        private void SetPaperSize()
        {
            switch (paperSize)
            {
                case PaperSize.A2:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.A2;
                    break;
                case PaperSize.A3:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.A3;
                    break;
                case PaperSize.A4:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.A4;
                    break;
                case PaperSize.A5:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.A5;
                    break;
                case PaperSize.A6:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.A6;
                    break;
                case PaperSize.B4JIS:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.B4JIS;
                    break;
                case PaperSize.B5JIS:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.B5JIS;
                    break;
                case PaperSize.JapanesePost:
                    this.wSheet.PrintOptions.PaperSize = Infragistics.Documents.Excel.PaperSize.JapanesePostcard;
                    break;
            }
        }
        #endregion

        #region [ Method ] Page setup ( Horizontal center )
        /// <summary>
        ///  Page setup ( Horizontal center )
        /// </summary>
        private void SetCenterHorizontally()
        {
            this.wSheet.PrintOptions.CenterHorizontally = centerHorizontally;
        }
        #endregion

        #region [ Method ] Page setup ( Vertically centered )
        /// <summary>
        ///  Page setup ( Vertically centered )
        /// </summary>
        private void SetCenterVertically()
        {
            this.wSheet.PrintOptions.CenterVertically = centerVertically;
        }
        #endregion

        #region [ Method ] Page setup ( Page margins )
        /// <summary>
        ///  Page setup ( Page margins )
        /// </summary>
        private void SetPageMargin()
        {
            this.wSheet.PrintOptions.LeftMargin = this.pageLeftMargin;
            this.wSheet.PrintOptions.RightMargin = this.pageRightMargin;
            this.wSheet.PrintOptions.TopMargin = this.pageTopMargin;
            this.wSheet.PrintOptions.BottomMargin = this.pageBottomMargin;
        }
        #endregion

        #region [ Method ] Page setup ( The zoom )
        /// <summary>
        ///  Page setup ( The zoom )
        /// </summary>
        private void SetPageZoom()
        {
            this.wSheet.PrintOptions.ScalingType = ScalingType.UseScalingFactor;
            this.wSheet.PrintOptions.ScalingFactor = this.defaultZoom;
        }
        #endregion

        #region [ Method ] Add a workbook 
        /// <summary>
        ///  Add a workbook 
        /// </summary>
        public void AddSheet()
        {
            int num = this.wBook.Worksheets.Count + 1;
            string sheetName = "Sheet" + num.ToString();
            while (this.IsExistTheSheet(sheetName))
            {
                num++;
                sheetName = "Sheet" + num.ToString();
            }
            this.wBook.WindowOptions.SelectedWorksheet = this.wBook.Worksheets.Add(sheetName);
            this.wSheet = this.wBook.WindowOptions.SelectedWorksheet;
            this.SetDefaultColWidth();
        }

        /// <summary>
        ///  Add a workbook 
        /// </summary>
        /// <param name="sheetName">sheet name </param>
        public void AddSheet(string sheetName)
        {
            if (this.IsExistTheSheet(sheetName))
            {
                throw new Exception(" There is a with the same name Sheet page !");
            }
            this.wBook.WindowOptions.SelectedWorksheet = this.wBook.Worksheets.Add(sheetName);
            this.wSheet = this.wBook.WindowOptions.SelectedWorksheet;
            this.SetDefaultColWidth();
        }
        #endregion

        #region [ Method ] Whether the same workbook exists 
        /// <summary>
        ///  Whether the same workbook exists 
        /// </summary>
        /// <param name="sheetName">sheet name </param>
        /// <returns> There is :true   non-existent :false</returns>
        private bool IsExistTheSheet(string sheetName)
        {
            for (int i = 0; i < this.wBook.Worksheets.Count; i++)
            {
                if (this.wBook.Worksheets[i].Name == sheetName)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region [ Method ] Get column index 
        private int GetColumnIndex(string cellName)
        {
            double colIndex = 0;
            string colName = string.Empty;
            string colItem = string.Empty;

            for (int iLoop = 0; iLoop < cellName.Length; iLoop++)
            {
                colItem = cellName.Substring(iLoop, 1);
                int indexTxt = this.columnNameList.IndexOf(colItem);
                if (indexTxt == -1)
                {
                    break;
                }
                else
                {
                    colName = cellName.Substring(0, iLoop + 1);
                }
            }
            colName = this.StringReverse(colName);

            for (int iLoop = 0; iLoop < colName.Length; iLoop++)
            {
                int indexTxt = this.columnNameList.IndexOf(colName.Substring(iLoop, 1));
                if (indexTxt > -1)
                {
                    colIndex += Math.Pow(this.columnNameList.Length, iLoop) * (indexTxt + 1);
                }
                else
                {
                    break;
                }
            }
            return int.Parse(colIndex.ToString()) - 1;
        }
        #endregion

        #region [ Method ] Cell assignment 
        /// <summary>
        ///  Cell assignment 
        /// </summary>
        /// <param name="cellName"> Cell name ( Such as :A1)</param>
        /// <param name="cellValue"> Cell values </param>
        public void SetCellValueTxt(string cellName, string cellValue)
        {
            int rowIndex = this.GetRowIndex(cellName);
            int colIndex = this.GetColumnIndex(cellName);
            if (cellValue == null)
            {
                this.wSheet.Rows[rowIndex].Cells[colIndex].Value = string.Empty;
            }
            else
            {
                IWorksheetCellFormat formatObj = this.wSheet.Rows[rowIndex].Cells[colIndex].CellFormat;
                this.wSheet.Rows[rowIndex].Cells[colIndex].Value = cellValue;
                this.wSheet.Rows[rowIndex].Cells[colIndex].CellFormat.Alignment = formatObj.Alignment;
            }
        }

        /// <summary>
        ///  Cell assignment 
        /// </summary>
        /// <param name="cellName"> Cell name ( Such as :A1)</param>
        /// <param name="cellValue"> Cell values </param>
        public void SetCellValueNum(string cellName, string cellValue)
        {
            int rowIndex = this.GetRowIndex(cellName);
            int colIndex = this.GetColumnIndex(cellName);
            if (cellValue != null)
            {
                this.wSheet.Rows[rowIndex].Cells[colIndex].Value = double.Parse(cellValue);
            }
        }

        /// <summary>
        ///  Set cell style 
        /// </summary>
        /// <param name="startCellName"> Start cell ( Such as :A1)</param>
        /// <param name="endCellName"> End cell ( Such as :B1)</param>
        /// <param name="isBold"> In bold </param>
        public void SetCellStyle(string startCellName, string endCellName, bool isBold)
        {
            int firstRow = this.GetRowIndex(startCellName);
            int firstColumn = this.GetColumnIndex(startCellName);
            int lastRow = this.GetRowIndex(endCellName);
            int lastColumn = this.GetColumnIndex(endCellName);
            int iLoop = firstRow > lastRow ? -1 : 1;
            int jLoop = firstColumn > lastColumn ? -1 : 1;
            int RowIndex = firstRow;
            while (true)
            {
                int ColumnIndex = firstColumn;
                while (true)
                {
                    wSheet.Rows[RowIndex].Cells[ColumnIndex].CellFormat.Font.Bold = isBold ? ExcelDefaultableBoolean.True : ExcelDefaultableBoolean.False;
                    if (ColumnIndex == lastColumn) break;
                    ColumnIndex += jLoop;
                }
                if (RowIndex == lastRow) break;
                RowIndex += iLoop;
            }
        }

        /// <summary>
        ///  Set cell style 
        /// </summary>
        /// <param name="startCellName"> Start cell ( Such as :A1)</param>
        /// <param name="endCellName"> End cell ( Such as :B1)</param>
        /// <param name="vAlign"> Alignment mode </param>
        public void SetCellStyle(string startCellName, string endCellName, VerticalCellAlignment vAlign)
        {
            int firstRow = this.GetRowIndex(startCellName);
            int firstColumn = this.GetColumnIndex(startCellName);
            int lastRow = this.GetRowIndex(endCellName);
            int lastColumn = this.GetColumnIndex(endCellName);
            int iLoop = firstRow > lastRow ? -1 : 1;
            int jLoop = firstColumn > lastColumn ? -1 : 1;
            int RowIndex = firstRow;
            while (true)
            {
                int ColumnIndex = firstColumn;
                while (true)
                {
                    wSheet.Rows[RowIndex].Cells[ColumnIndex].CellFormat.VerticalAlignment = vAlign;
                    if (ColumnIndex == lastColumn) break;
                    ColumnIndex += jLoop;
                }
                if (RowIndex == lastRow) break;
                RowIndex += iLoop;
            }
        }

        /// <summary>
        ///  Set cell style 
        /// </summary>
        /// <param name="cellName"> Cell name ( Such as :A1)</param>
        /// <param name="fontName"> typeface , Such as : Song style </param>
        /// <param name="fontHeight"> Font size </param>
        public void SetCellStyle(string cellName, string fontName, int fontHeight)
        {
            int rowIndex = this.GetRowIndex(cellName);
            int colIndex = this.GetColumnIndex(cellName);
            wSheet.Rows[rowIndex].Cells[colIndex].CellFormat.Font.Name = fontName;
            wSheet.Rows[rowIndex].Cells[colIndex].CellFormat.Font.Height = fontHeight;
        }

        /// <summary>
        ///  Set cell style 
        /// </summary>
        /// <param name="cellName"> Cell name ( Such as :A1)</param>
        /// <param name="isBold"> In bold </param>
        public void SetCellStyle(string cellName, bool isBold)
        {
            int rowIndex = this.GetRowIndex(cellName);
            int colIndex = this.GetColumnIndex(cellName);
            wSheet.Rows[rowIndex].Cells[colIndex].CellFormat.Font.Bold = isBold ? ExcelDefaultableBoolean.True : ExcelDefaultableBoolean.False;
        }

        /// <summary>
        ///  Set cell style 
        /// </summary>
        /// <param name="cellName"> Cell name ( Such as :A1)</param>
        /// <param name="align"> Alignment mode </param>
        public void SetCellStyle(string cellName, HorizontalCellAlignment align)
        {
            int rowIndex = this.GetRowIndex(cellName);
            int colIndex = this.GetColumnIndex(cellName);
            wSheet.Rows[rowIndex].Cells[colIndex].CellFormat.Alignment = align;
        }

        /// <summary>
        ///  Set cell style 
        /// </summary>
        /// <param name="cellName"> Cell name ( Such as :A1)</param>
        /// <param name="vAlign"> Alignment mode </param>
        public void SetCellStyle(string cellName, VerticalCellAlignment vAlign)
        {
            int rowIndex = this.GetRowIndex(cellName);
            int colIndex = this.GetColumnIndex(cellName);
            wSheet.Rows[rowIndex].Cells[colIndex].CellFormat.VerticalAlignment = vAlign;
        }
        #endregion

        #region [ Method ] Set row cell style 
        /// <summary>
        ///  Set row cell style 
        /// </summary>
        /// <param name="rowIndex"> Line number </param>
        /// <param name="fontName"> typeface , Such as : Song style </param>
        /// <param name="fontHeight"> Font size </param>
        public void SetRowCellStyle(int rowIndex, string fontName, int fontHeight)
        {
            wSheet.Rows[rowIndex - 1].CellFormat.Font.Name = fontName;
            wSheet.Rows[rowIndex - 1].CellFormat.Font.Height = fontHeight;
        }

        /// <summary>
        ///  Set row cell style 
        /// </summary>
        /// <param name="rowIndex"> Line number </param>
        /// <param name="isBold"> In bold </param>
        public void SetRowCellStyle(int rowIndex, bool isBold)
        {
            wSheet.Rows[rowIndex - 1].CellFormat.Font.Bold = isBold ? ExcelDefaultableBoolean.True : ExcelDefaultableBoolean.False;
        }

        /// <summary>
        ///  Set row cell style 
        /// </summary>
        /// <param name="rowIndex"> Line number </param>
        /// <param name="align"> Alignment mode </param>
        public void SetRowCellStyle(int rowIndex, HorizontalCellAlignment align)
        {
            wSheet.Rows[rowIndex - 1].CellFormat.Alignment = align;
        }

        /// <summary>
        ///  Set row cell style 
        /// </summary>
        /// <param name="rowIndex"> Line number </param>
        /// <param name="vAlign"> Alignment mode </param>
        public void SetRowCellStyle(int rowIndex, VerticalCellAlignment vAlign)
        {
            wSheet.Rows[rowIndex - 1].CellFormat.VerticalAlignment = vAlign;
        }
        #endregion

        #region [ Method ] Set column cell style 
        /// <summary>
        ///  Set column cell style 
        /// </summary>
        /// <param name="colName"> Name </param>
        /// <param name="fontName"> typeface , Such as : Song style </param>
        /// <param name="fontHeight"> Font size </param>
        public void SetColumnCellStyle(string colName, string fontName, int fontHeight)
        {
            int colIndex = this.GetColumnIndex(colName);
            wSheet.Columns[colIndex].CellFormat.Font.Name = fontName;
            wSheet.Columns[colIndex].CellFormat.Font.Height = fontHeight;
        }

        /// <summary>
        ///  Set column cell style 
        /// </summary>
        /// <param name="colName"> Name </param>
        /// <param name="isBold"> In bold </param>
        public void SetColumnCellStyle(string colName, bool isBold)
        {
            int colIndex = this.GetColumnIndex(colName);
            wSheet.Columns[colIndex].CellFormat.Font.Bold = isBold ? ExcelDefaultableBoolean.True : ExcelDefaultableBoolean.False;
        }

        /// <summary>
        ///  Set column cell style 
        /// </summary>
        /// <param name="colName"> Name </param>
        /// <param name="align"> Alignment mode </param>
        public void SetColumnCellStyle(string colName, HorizontalCellAlignment align)
        {
            int colIndex = this.GetColumnIndex(colName);
            wSheet.Columns[colIndex].CellFormat.Alignment = align;
        }

        /// <summary>
        ///  Set column cell style 
        /// </summary>
        /// <param name="colName"> Name </param>
        /// <param name="vAlign"> Alignment mode </param>
        public void SetColumnCellStyle(string colName, VerticalCellAlignment vAlign)
        {
            int colIndex = this.GetColumnIndex(colName);
            wSheet.Columns[colIndex].CellFormat.VerticalAlignment = vAlign;
        }
        #endregion

        #region [ Method ] Set row height 
        /// <summary>
        ///  Set row height 
        /// </summary>
        /// <param name="rowIndex"> Line number </param>
        /// <param name="rowHeight"> Row height </param>
        public void SetRowHeight(int rowIndex, int rowHeight)
        {
            wSheet.Rows[rowIndex - 1].Height = rowHeight;
        }
        #endregion

        #region [ Method ] Set column width 
        /// <summary>
        ///  Set column width 
        /// </summary>
        /// <param name="colName"> Name </param>
        /// <param name="colWidth"> Width </param>
        public void SetColWidth(string colName, int colWidth)
        {
            int colIndex = this.GetColumnIndex(colName);
            wSheet.Columns[colIndex].Width = colWidth * ExcelDataConst.colWidth;
        }
        #endregion

        #region [ Method ] merge cell 
        /// <summary>
        ///  merge cell 
        /// </summary>
        /// <param name="startCellName"> Start cell ( Such as :A1)</param>
        /// <param name="endCellName"> End cell ( Such as :B1)</param>
        public void MergedCells(string startCellName, string endCellName)
        {
            int firstRow = this.GetRowIndex(startCellName);
            int firstColumn = this.GetColumnIndex(startCellName);
            int lastRow = this.GetRowIndex(endCellName);
            int lastColumn = this.GetColumnIndex(endCellName);
            this.wSheet.MergedCellsRegions.Add(firstRow, firstColumn, lastRow, lastColumn);
        }
        #endregion

        #region [ Method ] Protect WorkSheet
        /// <summary>
        ///  Protect WorkSheet
        /// </summary>
        /// <param name="isProtected"> Whether to protect </param>
        public void ProtectedWorkSheet(bool isProtected)
        {
            this.wSheet.Protected = isProtected;
        }
        #endregion

        #region [ Method ] Protect WorkBook
        /// <summary>
        ///  Protect WorkBook
        /// </summary>
        /// <param name="isProtected"> Whether to protect </param>
        public void ProtectedWorkBook(bool isProtected)
        {
            this.wBook.Protected = isProtected;
        }
        #endregion

        #region [ Method ] Save and close Excel file 
        /// <summary>
        ///  Save and close Excel file 
        /// </summary>
        public void SaveAndCloseFile()
        {
            this.wBook.Save(this.templateFile);
        }
        #endregion
    }
}

原网站

版权声明
本文为[Heart blue 168]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060928323155.html