We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Write to an already opened excel file (through openfiledialog)

Hello SyncFusion. 

I am new to C#.NET development and I am trying to figure out how to write data into specific sheets and cells in an workbook. This workbook does not have a specific path, instead it is opened by another method and I´d like to write data to it by another method (seperate button on the winform menu).

Is there a way to do this? 
Alternatively a method during the same event click writes to the excel file once it opens. Not sure what approach is best here.

This is the code. 

private void selectHeatBalanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "xls files (*.xls)|*.xls|xlsx files (*.xlsx)|*.xlsx";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;
                Workbook wbook;
                Worksheet wsheet;
                _Application excel = new _Excel.Application();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    excel = new _Excel.Application();
                    wbook = excel.Workbooks.Open(openFileDialog.FileName);
                    excel.Visible = true;
                }
            }
           
        }

1 Reply

MC Mohan Chandran Syncfusion Team January 16, 2019 12:23 PM UTC

Hi Christian, 

Thank you for contacting Syncfusion support. 

We have shared a sample to open a Excel file using open File dialog and to manipulate it. This can be downloaded from the following link. The code that helps to achieve this requirement is shared below. 

Code snippet: 
ExcelEngine excelEngine = new ExcelEngine(); 
IApplication application = excelEngine.Excel; 
 
//The workbook is opened. 
IWorkbook workbook = null; 
 
OpenFileDialog openFileDialog = new OpenFileDialog(); 
openFileDialog.InitialDirectory = "c:\\"; 
openFileDialog.Filter = "xls files (*.xls)|*.xls|xlsx files (*.xlsx)|*.xlsx"; 
openFileDialog.FilterIndex = 2; 
openFileDialog.RestoreDirectory = true; 
 
if (openFileDialog.ShowDialog() == DialogResult.OK) 
    workbook = application.Workbooks.Open(openFileDialog.FileName, ExcelOpenType.Automatic); 
 
//The first worksheet object in the worksheets collection is accessed. 
IWorksheet worksheet = workbook.Worksheets[0]; 


Please refer the UG documentation from the following link to know about worksheet manipulation in XlsIO. 



Kindly try this and let us know whether it helps. 

Regards, 
Mohan. 


Loader.
Live Chat Icon For mobile
Up arrow icon