如何在手动和编程中允许excel数据修改?

问题描述 投票:2回答:1

我正在使用C#windows应用程序进行Excel数据添加/更新。我添加了Microsoft.Office.Interop.Excel参考(参考 - >右键单击 - >添加参考 - > COM - >类型库 - > Microsoft Excel 1X.0对象库)。在我的表格上,我有一个面板控制panel1,一个列表框lstSamples和两个按钮btnAddSamplebtnFormatWorksheet

我的示例代码如下:

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;


public partial class Form1 : Form
{
    Microsoft.Office.Interop.Excel.Application excelApp;
    Workbook excelWorkBook;
    Worksheet excelWorkSheet;

    public Form1()
    {
        InitializeComponent();
        LoadExcelFile();
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    private void LoadExcelFile()
    {
        excelApp = new Microsoft.Office.Interop.Excel.Application();

        excelApp.Visible = true;
        excelApp.ScreenUpdating = true;
        excelApp.EnableAutoComplete = false;
        excelWorkBook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        IntPtr excelHwnd = new IntPtr(excelApp.Application.Hwnd);
        SetParent(excelHwnd, panel1.Handle);
    }

    private void btnAddSample_Click(object sender, EventArgs e)
    {
        excelWorkSheet = (Worksheet)excelWorkBook.Worksheets.get_Item(1);
        int lastUsedRow = excelWorkSheet.UsedRange.Rows.Count;
        excelWorkSheet.Cells[lastUsedRow + 1, 1] = lstSamples.SelectedItem.ToString();
        lstSamples.Items.Remove(lstSamples.SelectedItem);
    }

   private void btnFormatWorksheet_Click(object sender, EventArgs e)
   {
        Range chartRange;
        excelWorkSheet = (Worksheet)excelWorkBook.Worksheets.get_Item(1);
        chartRange = excelWorkSheet.get_Range("b2", "e9");
        chartRange.BorderAround(XlLineStyle.xlContinuous, 
        XlBorderWeight.xlMedium, XlColorIndex.xlColorIndexAutomatic, 
        XlColorIndex.xlColorIndexAutomatic);

    }
}    

请按照我提到的步骤操作1.运行应用程序并在“A1”单元格中添加数据(字符串类型)2。再次在“A2”单元格中添加一些数据,然后按回车3.从lstSamples列表框中选择一项点击btnAddSample(结果就像所选项目将被添加到“A3”单元格中4.尝试修改“A1”或“A2”单元格数据。(这里lstSample具有类似于Test1,Test2,Test3,...的字符串类型的项目。 ..)。如果你能够编辑单元格,然后点击btnFormatWorksheet然后尝试编辑任何单元格。

c# excel winapi com-interop excel-interop
1个回答
4
投票

为什么要引用COM DLL?您应该通过References窗口中的.Net选项卡引用.Net PIA - 此位置的主互操作程序集,并浏览到:

C:\ Program Files(x86)\ Microsoft Visual Studio [版本] \ Visual Studio工具用于Office \ PIA \ Office [版本] \ Microsoft.Office.Interop.Excel.dll

仅用于单元测试,您是否参考COM。 See my answer herehow I originally worked it out。它很容易混淆,因为在解决方案资源管理器中,它们都被称为同一个东西!

enter image description here


如果这不起作用,我最初把它作为一个答案,以挽救其他人浪费时间。

Ahmed和我都无法使用您提供的代码重现您描述的问题。

请参阅我输入的单元格A1和A2,然后我在列表中选择了一个项目并单击了该按钮。然后我选择单元格A2并键入Editable。

enter image description here

ps如果你能提供重现的步骤,我将很乐意再看看它。

UPDATE:

您重现问题的修订步骤不正确,它适用于我:

enter image description here

UPDATE 2:

检查消息泵过滤器是否导致焦点转到另一个单元/控件:

Excel CustomTaskPane with WebBrowser control - keyboard/focus issues

© www.soinside.com 2019 - 2024. All rights reserved.