用C#替换宏,实现动态创建Excel下拉框及批注

 //添加引用对应的Excel命名空间

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;

namespace windows1
{
    public partial class newtest : DevExpress.XtraEditors.XtraForm
    {
        public newtest()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            KillProcess("Excel");//首先杀死进程
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
            Microsoft.Office.Interop.Excel.Range range = worksheet.Cells;
                     
            Excel.Range ran = range.get_Range("A5", "A5");
            ran.AddComment("批注");
            Excel.Range ran1 = worksheet.Columns.get_Range("H:H", Type.Missing);//或者.get_Range("D1", "D1").get_Resize(50,1);//获取一整列D,行数为50  
            ran1.Validation.Delete();//有无都可以
            ran1.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Type.Missing, "1,2,3", Type.Missing);
            //填充值
            worksheet.Cells[1, 1] = "姓名";
            worksheet.Cells[1, 2] = "年龄";
            worksheet.Cells[1, 3] = "性别";
            worksheet.Cells[2, 1] = "张三";
            worksheet.Cells[2, 2] = "14";
            worksheet.Cells[2, 3] = "男";                   
            workbook.SaveCopyAs("E://tt.xls");
            workbook.Saved = true;
            xlApp.Visible = true;
            // ran1.Validation.Modify(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Type.Missing, "4,3,2", Type.Missing);//用此法方(Modify)可以对前面的下拉框进行修改,


        }

        //杀死进程
        private void KillProcess(string processName)
        {
            //获得进程对象,以用来操作  
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            //得到所有打开的进程   
            try
            {
                //获得需要杀死的进程名  
                foreach (Process thisproc in Process.GetProcessesByName(processName))
                {
                    //立即杀死进程  
                    thisproc.Kill();
                }
            }
            catch (Exception Exc)
            {
                throw new Exception("", Exc);
            }
        } 

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值