C# 操作excel类

 public class CExcel
    {
        private Application m_App = null;
        private Worksheet m_current_sheet = null;
        private Workbook m_work_book = null;

        public Range Border(int top, int left, int bottom, int right)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return null;
                }
                Worksheet worksheet = this.m_current_sheet;
                Range range = worksheet.get_Range(worksheet.Cells[top, left], worksheet.Cells[bottom, right]);
                range.Borders.LineStyle = 1;
                return range;
            }
            catch
            {
                return null;
            }
        }

        public void Close()
        {
            try
            {
                this.m_App.Quit();
                this.m_App = null;
            }
            catch
            {
            }
        }

        public bool Create()
        {
            try
            {
                if (this.m_App != null)
                {
                    this.Close();
                }
                this.m_App = new ApplicationClass();
                if (this.m_App == null)
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Workbook CreateWorkBook()
        {
            Workbooks workbooks = this.m_App.Workbooks;
            if (workbooks == null)
            {
                this.m_App = null;
                return null;
            }
            Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            if (workbook == null)
            {
                this.m_App = null;
                return null;
            }
            this.m_work_book = workbook;
            this.m_current_sheet = (Worksheet)workbook.Worksheets.get_Item(1);
            return workbook;
        }

        public string GetCellText(object row, object column)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return "";
                }
                Range range = (Range)this.m_current_sheet.Cells.get_Item(row, column);
                if (range != null)
                {
                    return (string)range.Text;
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public Range GetRange(int top, int left, int bottom, int right)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return null;
                }
                Worksheet worksheet = this.m_current_sheet;
                return worksheet.Cells.get_Range(worksheet.Cells[top, left], worksheet.Cells[bottom, right]);
            }
            catch
            {
                return null;
            }
        }

        public Range Merge(int top, int left, int bottom, int right)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return null;
                }
                Worksheet worksheet = this.m_current_sheet;
                Range range = this.m_current_sheet.Cells.get_Range(worksheet.Cells[top, left], worksheet.Cells[bottom, right]);
                range.Merge(Missing.Value);
                return range;
            }
            catch
            {
                return null;
            }
        }

        public bool Open(string file)
        {
            try
            {
                if (this.m_App != null)
                {
                    this.Close();
                }
                this.m_App = new ApplicationClass();
                if (this.m_App == null)
                {
                    return false;
                }
                Workbooks workbooks = this.m_App.Workbooks;
                if (workbooks == null)
                {
                    this.m_App = null;
                    return false;
                }
                Workbook workbook = workbooks.Add(file);
                if (workbook == null)
                {
                    this.m_App = null;
                    return false;
                }
                this.m_work_book = workbook;
                this.m_current_sheet = (Worksheet)workbook.Worksheets.get_Item(1);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool SetCellData(int row, int column, object data)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return false;
                }
                this.m_current_sheet.Cells[row, column] = data;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool SetCellColor(int row, int column, object color)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return false;
                }
                this.m_current_sheet.get_Range(this.m_current_sheet.Cells[row, column], this.m_current_sheet.Cells[row, column]).Interior.Color = color;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Range SetCellText(int row, int column, string text)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return null;
                }
                Range range = (Range)this.m_current_sheet.Cells.get_Item(row, column);
                range.NumberFormatLocal = "@";
                this.m_current_sheet.Cells[row, column] = text;
                return range;
            }
            catch
            {
                return null;
            }
        }

        public Range SetColumnWidth(int column, double width)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return null;
                }
                Range range = (Range)this.m_current_sheet.Cells.get_Item(1, column);
                range.ColumnWidth = width;
                return range;
            }
            catch
            {
                return null;
            }
        }

        public void SetCurrentSheet(Worksheet sheet)
        {
            if (sheet != null)
            {
                this.m_current_sheet = sheet;
            }
        }

        public Range SetRowHeight(int row, double height)
        {
            try
            {
                if (this.m_current_sheet == null)
                {
                    return null;
                }
                Range range = (Range)this.m_current_sheet.Cells.get_Item(row, 1);
                range.RowHeight = height;
                range.Font.Name = "宋体";
                range.Font.Size = 9;
                return range;
            }
            catch
            {
                return null;
            }
        }

        public void Show()
        {
            try
            {
                if (this.m_App != null)
                {
                    this.m_App.Visible = true;
                }
            }
            catch
            {
            }
        }

        public Application app
        {
            get
            {
                return this.m_App;
            }
        }

        public Worksheet sheet
        {
            get
            {
                return this.m_current_sheet;
            }
        }

        public Workbook work_book
        {
            get
            {
                return this.m_work_book;
            }
        }
    }

 

posted @ 2013-07-22 12:35  StormeR  阅读(285)  评论(0编辑  收藏  举报