C#对excel一些基本操作

代码一般可以直接copy使用,也许会在工程里面未引用,未引用的要自己添加引用(office com接口引用方式:右键工程→addreference→com页→Microsoft Excel xx.0 Object Library)。以下代码仅仅打开,给某个单元格设置颜色,然后关闭。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;

namespace TestCsConsole
{
    class Excel_Parser_Impl : IExcel_Parser
    {
        private Microsoft.Office.Interop.Excel.Application      app     = null;
        private Microsoft.Office.Interop.Excel.Workbook         wb      = null;
        private Microsoft.Office.Interop.Excel.Worksheet        ws      = null;
        private Microsoft.Office.Interop.Excel.Range            range   = null;
        private string                                          filePath;

        public  bool LoadFile(string FileName)
        {
            filePath = FileName;
            try
            {
                app = new Microsoft.Office.Interop.Excel.ApplicationClass();
                wb = app.Workbooks.Open(filePath, false, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                    System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                    System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
                app.Visible = false;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Excel操作出错:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            finally
            {
            }
            return true;
        }

        public  void CloseFile()
        {
            wb.Save();
            app.Quit();
            app = null;
            return;
        }

        public  void SetCellColor(string TabSheetName, System.Drawing.Color CellColor, int Col, int Row)
        {
            
            if (true)
            {
                try
                {
                    ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(TabSheetName);
                    range = (Range)ws.Cells[Row, Col];
                    range.Interior.Color = System.Drawing.ColorTranslator.ToOle(CellColor);
                    
                    app.DisplayAlerts = false;
                    app.AlertBeforeOverwriting = false;
                    
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Excel操作出错:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                }
            }
            return;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值