c# - 我们可以开发一个 .exe 文件,在 c# 中用于宏,以便在之前不包含任何宏的新机器中设置 excel 文件
问题描述
每当我们单击它时,我都需要开发一个 .exe 文件,应该将 Personal.XLSB 和 Excel 自定义从一个文件夹复制到特定机器中的 XLSTART 文件夹(我已经完成了这个)。并选中开发人员选项卡的复选框并导入 Excel 自定义通过 c# 代码。有可能吗?请帮忙。
完成将文件从一个地方复制到另一个地方。现在我要做的是导入 Excel 自定义文件并选中 Excel 选项中的“开发人员”选项卡的复选框。这可以通过 c# 代码实现吗?
解决方案
好吧,只要您构建项目,就会创建一个 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
推荐阅读
- shiny - 如何调整框内verbatimTextOutput的高度?
- javascript - Photoshop 中的 Javascript:更改文本内容会增加字体大小
- date - Word 中未解析通配符日期格式
- java - Spring Boot Test 嵌入 MongoDB:结合 MockMVC 和 @DataMongoTest
- merge - 使用 SSIS-Lookup 引用外键
- c# - 自定义类型上的属性以期望用另一个属性标记的方法
- html - wkhtmltopdf --page-width 和 --page-height 不起作用
- google-cloud-platform - 在 Datafusion wrangler 中将 Schema 设置为运行时参数不起作用
- php - 我如何使用 php 将 json 文件导入到 firebase?
- python - 在 Python 中生成具有平行标记边/顶点的有向图