c# 操作 Excel

主函数文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; 
using System.Threading.Tasks;


namespace ConsoleApplication1
{

    class Program
    {
        static void Main( )
        {
            double[] arr=new double [5];
            for (int i=0;i<=4;i++){
                arr[i]=i;
            }
            ClassExcel ce = new ClassExcel();
            ce.openXlsx("D:\\Sheet1.xlsx");
            ce.outputAC(arr, 4, 2, 3);
            ce.setBackColor(221, 34, 54);
            ce.setWordColor(221, 34, 221);
            ce.saveXlsx();
            ce.closeXlsx();
            //ce.outputNumopen(7);
        }
    }
}

类文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Drawing;


namespace ConsoleApplication1
{
    class ClassExcel
    {
        public string Fname;
        public Microsoft.Office.Interop.Excel.Application xlApp;
        public Workbook wb;
        public Worksheet ws;
        public Range ra;
        public ClassExcel()
        {
            Fname = Environment.CurrentDirectory+"\\"+"workbook1.xlsx";
            xlApp = new Microsoft.Office.Interop.Excel.Application();
            ra = null;
        }

        public void setBackColor(int r,int g,int b){
            if (ra == null)
            {
                MessageBox.Show("Range不对", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            ra.Cells.Interior.Color = System.Drawing.Color.FromArgb(r, g, b).ToArgb();
        }

        public void setWordColor(int r, int g, int b)
        {
            if (ra == null)
            {
                MessageBox.Show("Range不对", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            ra.Cells.Font.Color = System.Drawing.Color.FromArgb(r, g, b).ToArgb();
        }

        public int newXlsx(){
            if (xlApp == null)
            {
                MessageBox.Show("Excel App没有建立", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            
            wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            ws = (Worksheet)wb.Worksheets[1];
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            return 0;
        }

        public int openXlsx(string n)
        {
            if (xlApp == null)
            {
                MessageBox.Show("Excel App没有建立", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            wb = xlApp.Workbooks.Add(n);
            Fname = n;
            ws = (Worksheet)wb.Worksheets[1];
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            return 0;
        }

        public int saveXlsx()
        {
            if (xlApp == null)
            {
                MessageBox.Show("Excel App没有建立", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            //设置禁止弹出保存和覆盖的询问提示框   
            xlApp.DisplayAlerts = false;
            xlApp.AlertBeforeOverwriting = false;
            //保存工作簿   
            wb.SaveAs(Fname);
            return 0;
        }

        public int saveAsXlsx(string n)
        {
            if (xlApp == null)
            {
                MessageBox.Show("Excel App没有建立", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            //设置禁止弹出保存和覆盖的询问提示框   
            xlApp.DisplayAlerts = false;
            xlApp.AlertBeforeOverwriting = false;
            //保存工作簿   
            wb.SaveAs(n);
            return 0;
        }

        public int outputNum(double dbl,int c,int r)
        {
            if (xlApp == null)
            {
                MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }

            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            ws.Cells[r,c]=dbl;
            // Select the Excel cells, in the range c1 to c7 in the worksheet.
            //Range aRange = ws.get_Range("A1", "E1");

            //if (aRange == null)
            //{
            //    MessageBox.Show("Range不对", "EXCEL输出",
            //    MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //    return -1;
            //}
            //aRange.Font.Size = 15;
            //aRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();
            return 0;
        }

        public int outputAC(double[] a,int n,int c,int r)
        {
            if (xlApp == null)
            {
                MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            ra = ws.get_Range(ws.Cells[r, c] as Range, ws.Cells[r, c + n]as Range);
            for (int i = 0; i <= n; i++)
            {
                ws.Cells[r, c+i] = a[i];
            }
            return 0;
        }

        public int outputAR(double[] a, int n, int c, int r)
        {
            if (xlApp == null)
            {
                MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            ra = ws.get_Range(ws.Cells[r, c]as Range, ws.Cells[r+n, c]as Range);
            for (int i = 0; i <= n; i++)
            {
                ws.Cells[r+ i, c ] = a[i];
            }
            return 0;
        }

        public int output2DA(double[][] a, int l, int m,int r,int c)
        {
            if (xlApp == null)
            {
                MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            ra = ws.get_Range(ws.Cells[r, c]as Range, ws.Cells[r + l, c+m]as Range);
            for (int i = 0; i <= l; i++)
            {
                for (int j = 0; j <= m; j++)
                { 
                    ws.Cells[r+i, c+j] = a[i][j];
                }
               
            }
            return 0;
        }

        public int output2DAr(double[][] a, int l, int m, int r, int c)
        {
            if (xlApp == null)
            {
                MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            if (ws == null)
            {
                MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return -1;
            }
            ra = ws.get_Range(ws.Cells[r, c]as Range, ws.Cells[r + l, c + m]as Range);
            for (int i = 0; i <= l; i++)
            {
                for (int j = m; j <= 0; j--)
                {
                    ws.Cells[r + i, c + j] = a[i][j];
                }
            }
            return 0;
        }

        public int closeXlsx()
        {
            wb.Close();
            xlApp.Quit();
            return 0;
        }

    }
}



转载于:https://my.oschina.net/u/929434/blog/98633

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值