首页 > 解决方案 > 我们可以开发一个 .exe 文件,在 c# 中用于宏,以便在之前不包含任何宏的新机器中设置 excel 文件

问题描述

每当我们单击它时,我都需要开发一个 .exe 文件,应该将 Personal.XLSB 和 Excel 自定义从一个文件夹复制到特定机器中的 XLSTART 文件夹(我已经完成了这个)。并选中开发人员选项卡的复选框并导入 Excel 自定义通过 c# 代码。有可能吗?请帮忙。

完成将文件从一个地方复制到另一个地方。现在我要做的是导入 Excel 自定义文件并选中 Excel 选项中的“开发人员”选项卡的复选框。这可以通过 c# 代码实现吗?

标签: c#excelvbawindows

解决方案


好吧,只要您构建项目,就会创建一个 EXE 文件。所以...BUILD->Batch Build 并激活发布配置中的“build”复选框。单击构建按钮时,将生成带有一些依赖项的 exe。您可以在项目的调试文件夹中找到此文件。

C:\Users\username\Documents\Visual Studio 2012\Projects\ProjectName\bin\Debug

这是代码。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {
                MessageBox.Show("Excel is not properly installed!!");
                return;
            }


            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Cells[1, 1] = "ID";
            xlWorkSheet.Cells[1, 2] = "Name";
            xlWorkSheet.Cells[2, 1] = "1";
            xlWorkSheet.Cells[2, 2] = "One";
            xlWorkSheet.Cells[3, 1] = "2";
            xlWorkSheet.Cells[3, 2] = "Two";



            xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
        }

    }
}

另外,请参阅下面的链接。

http://csharp.net-informations.com/excel/csharp-create-excel.htm


推荐阅读