如何使用Interop C#隐藏Excel列和行(How to hide Excel Columns and Rows using Interop C#)
我创建了一个简单的库存界面,它将从我的界面上的数据网格视图中访问和显示数据,然后通过按钮点击将信息发送到Excel。 此部分根据需要工作,但我想在发送信息后删除未使用的列和行。 我目前正在使用VS 2015.我无法弄清楚要添加什么来实现这一目标。
//send to excel private void btnExport_Click(object sender, EventArgs e) { ActiveControl = txtSerial; // creating Excel Application Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Inventory Search"; // storing header part in Excel for (int i = 1; i < dataGridFB.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dataGridFB.Columns[i - 1].HeaderText; worksheet.Cells[1, i].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightBlue); worksheet.Cells[1, i].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); worksheet.Cells[1, i].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; worksheet.Cells[1, i].Font.Size = 14; } // storing Each row and column value to excel sheet for (int i = 0; i < dataGridFB.Rows.Count - 1; i++) { for (int j = 0; j < dataGridFB.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dataGridFB.Rows[i].Cells[j].Value.ToString(); worksheet.Cells[i + 2, j + 1].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; worksheet.Cells[i + 2, j + 1].Font.Size = 12; worksheet.Columns["A:G"].AutoFit(); } } }
I have made a simple inventory interface that will take data from access and show in datagrid view on my interface and then send the information to Excel via button click. This part works as needed but I would like to remove the unused columns and rows after the information is sent. I am currently using VS 2015. I cant figure out what to add to accomplish this.
//send to excel private void btnExport_Click(object sender, EventArgs e) { ActiveControl = txtSerial; // creating Excel Application Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Inventory Search"; // storing header part in Excel for (int i = 1; i < dataGridFB.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dataGridFB.Columns[i - 1].HeaderText; worksheet.Cells[1, i].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightBlue); worksheet.Cells[1, i].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); worksheet.Cells[1, i].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; worksheet.Cells[1, i].Font.Size = 14; } // storing Each row and column value to excel sheet for (int i = 0; i < dataGridFB.Rows.Count - 1; i++) { for (int j = 0; j < dataGridFB.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dataGridFB.Rows[i].Cells[j].Value.ToString(); worksheet.Cells[i + 2, j + 1].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; worksheet.Cells[i + 2, j + 1].Font.Size = 12; worksheet.Columns["A:G"].AutoFit(); } } }
原文:https://stackoverflow.com/questions/35974760
最满意答案
经过搜索和实验,这里是我如何解决问题,也许不是最好的方法1.在安装项目中保持相同的升级代码,将removePreviousVersions设置为true 2.在自定义Action中,在public override void Install(IDictionary stateSaver)我添加了代码将文件从旧文件夹移动到新文件夹
After search & experiment, here's how I solved the issue, maybe not the best way 1. keep the same upgradecode in setup project, set removePreviousVersions to true 2. in custom Action, in public override void Install(IDictionary stateSaver) I added code to move files from old folder to new folder
相关问答
更多-
sudo
/Library/uninstall-devtools --mode=all 其中 是Developer文件夹的路径(其中包含'Xcode 3.2.5')。这不会影响'Xcode 4.0.2'安装,因为它位于完全不同的文件夹中。 如何unistall Xcode sudo /Library/uninstall-devtools --mode=all where is the path to the Developer folder (th ... -
从源代码安装时,缺省情况下,安装位于/usr/local - 特别是可执行文件变为/usr/local/bin/pythonX.Y并带有一个名为/usr/local/python的符号链接。 Ubuntu自己的安装位于/usr/ (例如/usr/bin/python ),因此新安装不会覆盖它。 注意PATH环境变量在/usr/local/bin之前没有/usr/local/bin /usr/bin ,否则简单提到python 会执行新的,而不是旧的。 When you install from source ...
-
您应该验证升级是否正常运行。 例如,如果你安装了每台机器,那么每个用户“升级”就没有真正的升级,因为两个产品仍然会被安装。 如果您卸载了新的每用户设置,则会使每台计算机都安装完毕,然后您无法对其进行降级。 在反之亦然的情况下,存在具有交叉上下文“升级”的类似场景。 所以这些是猜测,不知道整个情况,所需的信息主要是初始安装的详细日志和你说的升级工作。 “机器上没有任何东西”可能只是使用ARPSYSTEMCOMPONENT = 1。 或者,如果您使用不同的凭据登录,则不会在其他帐户下看到安装的产品。 因为相同的 ...
-
您可以查看以下注册表项之一: HKEY_USERS\.DEFAULT\Software\Microsoft\Office\11.0\Excel HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Excel 您应该查找密钥的存在,其中包含“Excel”,因为您可以安装Office 2003,而将visio 2007放在一边,因此两个密钥都将存在,但只有一个密钥具有Excel子密钥: HKEY_CURRENT_USER\Software\Microsoft\O ...
-
从2010年到现在,任何版本的Visual Studio都应该能够为Excel 2010创建VSTO加载项。从VS 2012开始,也适用于Office 2013.您在更高版本中看到的哪些模板集依赖于1)版本已安装Office和2)创建项目时选择的.NET Framework版本(2010年为4.0;稍后为4.5)。 更多信息可以在这个页面上找到: https : //blogs.msdn.microsoft.com/vsto/2013/03/06/office-developer-tools-for-vis ...
-
过期期货套餐 :您无法更改旧版本的软件包以检测新版本。 您需要从一开始就为您的包构建保护。 Packages need to be pre-cognitive. It's an industry problem. 现代时报 :您在上面展示的WiX元素是“旧式”。 这里描述了一个新的“便利功能”: 如何在每次启动时检测已安装的产品版本? 它涉及“新的” MajorUpgrade元素 。 这个新的MajorUpgrade元素具有一些自动魔法,我相信它会添加您默认描述的保护(降级保护)。 因此,您可以切换到使用它 ...
-
经过搜索和实验,这里是我如何解决问题,也许不是最好的方法1.在安装项目中保持相同的升级代码,将removePreviousVersions设置为true 2.在自定义Action中,在public override void Install(IDictionary stateSaver)我添加了代码将文件从旧文件夹移动到新文件夹 After search & experiment, here's how I solved the issue, maybe not the best way 1. keep t ...
-
WIX安装程序:卸载以前的版本并安装新版本(WIX Installer : uninstall previous version and install new version)[2023-12-02]
正确的答案是在InstallInitialize之后,但如果这不起作用那么你就会遇到更大的问题。 例如,如果您尝试了它并且它只安装了具有更高版本的更新文件,则根据定义您的主要升级逻辑是不正确的,因为在安装较新的产品之前将卸载旧产品。 所以也许你没有检测到旧产品。 假设您的MSI文件中有适当的升级检测机制,这些是规则: 新的ProductCode和PackageCode。 相同的UpgradeCode。 在前三位数中增加ProductVersion。 并且每个用户安装不会升级每台机器,反之亦然,我认为您不能使 ... -
如果您尚未全局安装软件包(即使用'-g'),则只需删除node_modules中的socket.io文件夹,然后使用所需的socket.io版本再次运行npm install。 或者你可以这样做: npm uninstall socket.io@0.9.16 npm install socket.io@0.9.0 If you have not installed the package globally (i.e. with '-g'), you can just delete the socket.i ...
-
您需要安装以前的版本,如下所示: pip install django-excel==0.1 与运行pip freeze命令时的显示方式类似。 You will need to install a prior version like so: pip install django-excel==0.1 Similar to how it appears if you run the pip freeze command.