Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C# windows application for Excel data add/update. I had added Microsoft.Office.Interop.Excel reference. On my form, I have one panel control panel1, one ListBox lstSamples and two button btnAddSample, btnFormatWorksheet.
My sample code is as below:

C#
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);

    }

}



When the code block from btnAddSample_Click is executed then excel will get open in read-only mode but I want this file in read- write mode. Before btnAddSample_Click event everything is working as expected. I don't want to re-open that excel

What I have tried:

Trying to find properties of excel, Workbook, Worksheet those are changed after button click event executed.

Please follow the steps as I mentioned 1. Run the application and add data in "A1" cell (Which is of string type) 2. Again add some data in "A2" cell and Press enter 3. Select one item from lstSamples listbox and click on btnAddSample (Result is like selected item will get added into "A3" cell 4. Try to modify "A1" or "A2" cell data. (Here lstSample is having items of string type like Test1, Test2, Test3,....)
If you are able to edit cells then click on btnFormatWorksheet then try to edit any cell.
Posted
Updated 10-Apr-19 19:12pm
v7
Comments
CHill60 8-Apr-19 7:23am    
btnAddSample_Click is not opening Excel, that happens when you load your app. Try stepping through the code to follow what is happening - in particular what is the value of lastUsedRow and is there actually a selectedItem in your listbox
Maciej Los 8-Apr-19 14:53pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900