C# Programming
04-07-2009, 11:00 PM
Hi to all.
First of all, my apologies to the moderators in case this is the wrong place to post my question.
I would like to know if it is possible to implement the following:
I have a winform that will, by clicking a button, launch a specific Excel File. The user will be able to edit it and by clicking another button, the Excel file will be saved (without any other kind of prompting from the user) and Excel will be closed.
Step1: launch Excel by clicking a button
Step2: user clicks another button on the winform and that will save the Excel file and close Excel.
Now, from what I've seen so far, I can achieve the first step. My problem seems to be in the second step. I have no idea on how I can save the work the user has done on the Excel File without having to Save first in Excel.
For instance, if my Excel File is this:
A1 cell = 1234
And after the launch I change it to this:
A1 cell = 12345
If I try to save to another file by clicking the other button on the winform that file will have:
A1 cell = 1234
How can I achieve what I want?
This is the code on the launch button:
private void button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
ApplicationClass excelApp = new ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook myWorkBook = excelApp.Workbooks.Open("D:\\Teste.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Sheets["Sheet1"];
excelApp.Visible = true;
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
This is the code for the save button:
private void button3_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook myWorkBook = excelApp.Workbooks.Open("D:\\Test.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Sheets["Sheet1"];
excelApp.Save("D:\\Test1.xls");
excelApp.Quit();
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
I have also tried this code:
private void button3_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Microsoft.Office.Interop.Excel.ApplicationClass excelApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.ApplicationClass;
Microsoft.Office.Interop.Excel.Workbook myWorkBook = excelApp.Workbooks[1];
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Sheets["Sheet1"];
Microsoft.Office.Interop.Excel.Range dataRange = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells["1", "A"];
excelApp.Save("D:\\Test2.xls");
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
However, when I try this code, I end up with this exception:
System.Runtime.InteropServices.COMException was unhandled
Message="Exception from HRESULT: 0x800A03EC"
Source="Microsoft.Office.Interop.Excel"
ErrorCode=-2146827284
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.Save(Object Filename)
Is it possible, what I want to achieve? Being able to save programatically from my winform an edited Excel file? If so, what am I doing wrong?
I would apreciate if anibody could point me to the right direction.
Thanks in advance
First of all, my apologies to the moderators in case this is the wrong place to post my question.
I would like to know if it is possible to implement the following:
I have a winform that will, by clicking a button, launch a specific Excel File. The user will be able to edit it and by clicking another button, the Excel file will be saved (without any other kind of prompting from the user) and Excel will be closed.
Step1: launch Excel by clicking a button
Step2: user clicks another button on the winform and that will save the Excel file and close Excel.
Now, from what I've seen so far, I can achieve the first step. My problem seems to be in the second step. I have no idea on how I can save the work the user has done on the Excel File without having to Save first in Excel.
For instance, if my Excel File is this:
A1 cell = 1234
And after the launch I change it to this:
A1 cell = 12345
If I try to save to another file by clicking the other button on the winform that file will have:
A1 cell = 1234
How can I achieve what I want?
This is the code on the launch button:
private void button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
ApplicationClass excelApp = new ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook myWorkBook = excelApp.Workbooks.Open("D:\\Teste.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Sheets["Sheet1"];
excelApp.Visible = true;
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
This is the code for the save button:
private void button3_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook myWorkBook = excelApp.Workbooks.Open("D:\\Test.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Sheets["Sheet1"];
excelApp.Save("D:\\Test1.xls");
excelApp.Quit();
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
I have also tried this code:
private void button3_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Microsoft.Office.Interop.Excel.ApplicationClass excelApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.ApplicationClass;
Microsoft.Office.Interop.Excel.Workbook myWorkBook = excelApp.Workbooks[1];
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Sheets["Sheet1"];
Microsoft.Office.Interop.Excel.Range dataRange = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells["1", "A"];
excelApp.Save("D:\\Test2.xls");
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
However, when I try this code, I end up with this exception:
System.Runtime.InteropServices.COMException was unhandled
Message="Exception from HRESULT: 0x800A03EC"
Source="Microsoft.Office.Interop.Excel"
ErrorCode=-2146827284
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.Save(Object Filename)
Is it possible, what I want to achieve? Being able to save programatically from my winform an edited Excel file? If so, what am I doing wrong?
I would apreciate if anibody could point me to the right direction.
Thanks in advance