Click here to Skip to main content
15,896,154 members
Home / Discussions / C#
   

C#

 
AnswerRe: Making video call Pin
Christian Graus7-Apr-09 17:38
protectorChristian Graus7-Apr-09 17:38 
QuestionCrystal Reports Pin
CodingYoshi7-Apr-09 11:06
CodingYoshi7-Apr-09 11:06 
AnswerRe: Crystal Reports Pin
Mycroft Holmes7-Apr-09 12:28
professionalMycroft Holmes7-Apr-09 12:28 
QuestionIn search of a good PDF viewer control for .NET WinForms Pin
gongchengshi7-Apr-09 10:04
gongchengshi7-Apr-09 10:04 
AnswerRe: In search of a good PDF viewer control for .NET WinForms Pin
Christian Graus7-Apr-09 10:45
protectorChristian Graus7-Apr-09 10:45 
QuestionButton does not generate clicked events after validating textbox in a usercontrol Pin
hmvo247-Apr-09 9:44
hmvo247-Apr-09 9:44 
QuestionRTCP Packet in .net (c# or vb.net) Pin
Saurabh90907-Apr-09 9:11
Saurabh90907-Apr-09 9:11 
QuestionLaunching and Saving an Excel file from Winform Pin
DevWithDoutbs7-Apr-09 8:43
DevWithDoutbs7-Apr-09 8:43 
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
AnswerRe: Launching and Saving an Excel file from Winform Pin
Rolf Jaeger15-Jun-09 17:25
Rolf Jaeger15-Jun-09 17:25 
QuestionUsing StreamWriter...... Help Guys [modified] Pin
Rajdeep.NET is BACK7-Apr-09 8:32
Rajdeep.NET is BACK7-Apr-09 8:32 
AnswerRe: Using StreamWriter...... Help Guys Pin
behzadcp7-Apr-09 8:40
professionalbehzadcp7-Apr-09 8:40 
GeneralRe: Using StreamWriter...... Help Guys Pin
Rajdeep.NET is BACK7-Apr-09 8:44
Rajdeep.NET is BACK7-Apr-09 8:44 
AnswerRe: Using StreamWriter...... Help Guys Pin
Ashfield7-Apr-09 20:55
Ashfield7-Apr-09 20:55 
GeneralRe: Using StreamWriter...... Help Guys Pin
al3xutzu007-Apr-09 22:45
al3xutzu007-Apr-09 22:45 
Questionadd control to syncfusion gridcontrol Pin
behzadcp7-Apr-09 8:27
professionalbehzadcp7-Apr-09 8:27 
AnswerRe: add control to syncfusion gridcontrol Pin
Judah Gabriel Himango7-Apr-09 19:34
sponsorJudah Gabriel Himango7-Apr-09 19:34 
QuestionBusqueda en un Datagridview Pin
yaguis7-Apr-09 8:07
yaguis7-Apr-09 8:07 
AnswerRe: Busqueda en un Datagridview Pin
Christian Graus7-Apr-09 11:14
protectorChristian Graus7-Apr-09 11:14 
AnswerRe: Busqueda en un Datagridview Pin
Rajdeep.NET is BACK7-Apr-09 20:02
Rajdeep.NET is BACK7-Apr-09 20:02 
AnswerRe: Busqueda en un Datagridview Pin
Blue_Boy7-Apr-09 22:19
Blue_Boy7-Apr-09 22:19 
QuestionFlood Fill in C# Pin
k_crysa7-Apr-09 7:49
k_crysa7-Apr-09 7:49 
AnswerRe: Flood Fill in C# Pin
Thomas Stockwell7-Apr-09 9:22
professionalThomas Stockwell7-Apr-09 9:22 
AnswerRe: Flood Fill in C# Pin
Christian Graus7-Apr-09 11:15
protectorChristian Graus7-Apr-09 11:15 
AnswerRe: Flood Fill in C# Pin
dybs7-Apr-09 19:19
dybs7-Apr-09 19:19 
GeneralNeed Coding advice Pin
al3xutzu007-Apr-09 7:05
al3xutzu007-Apr-09 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.