Hi mates,
I currently find a problem when I export values from my windows forms application to an excel-file. I've made an empty excel-file firstly and I want the excel to be able to get the values on my window. E.g. Cell[1,3] gets value of textBox1; Cell[2,4] gets value of the label1 etc.
It works well when the excel-file not to be opened during the "data writing"-process. But I noticed, when I open the excel-file and then control my windows forms, the values would not be writen in the excel-file. Instead of this they were writen in another excel-file(has the same name) under "My documents".
I thought that this happened because I used method Workbooks.Open(). I wondered if I should change the objects within the methods Workbooks.Open(), which in my prog all "Type::Missing" are. And I've tried a lot combinations. For example to set UpdateLinks to 3, set Editable as true and so on. But it didn't work still.
Refering msdn adress for Workbooks.Open():
http://msdn.microsoft.com/en-us/library/ms260757(v=office.11)?cs-save-lang=1&cs-lang=cpp#code-snippet-2[
^]
Here is my code, it shows how I "write" values to an excel-file. I hope you'll see it and tell me how to solve my problem:
#define Excel Microsoft::Office::Interop::Excel
using namespace Excel;
.
.
.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Excel::Application^ xlApp;
Excel::Workbook^ wb;
Excel::Sheets^ sheets;
Excel::Worksheet^ ws;
Excel::Range^ range;
xlApp =gcnew Excel::ApplicationClass();
try
{
wb = xlApp->Workbooks->Open("C:\\test.xlsx", Type::Missing, false,
Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing,
Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing,Type::Missing,Type::Missing);
sheets = wb->Sheets;
ws = (Excel::Worksheet^) sheets[1];
range = (Excel::Range^)ws->Range["A1:Z100", Type::Missing];
range->Cells[1,1] = "GiveToFirstCell";
range->Cells[2,10] = "I'm in cell I2!";
DateTime^ dt = System::DateTime::Now;
String^ time = dt->ToString("HH:mm:ss");
range->Cells[3,5]= time;
wb->Save();
}
catch (Exception^ ex)
{
MessageBox::Show("Error writing to excel:\r\n"+ex->Message, "Error",MessageBoxButtons::OK, MessageBoxIcon::Error);
}
finally
{
System::Runtime::InteropServices::Marshal::FinalReleaseComObject(range);
System::Runtime::InteropServices::Marshal::FinalReleaseComObject(ws);
System::Runtime::InteropServices::Marshal::FinalReleaseComObject(sheets);
wb->Close(true,false, Type::Missing);
System::Runtime::InteropServices::Marshal::FinalReleaseComObject(wb);
xlApp->Quit();
System::Runtime::InteropServices::Marshal::FinalReleaseComObject(xlApp);
}
}
If my problem wasn't clear explained, or you have questions to my code. Just tell me, I'm glad to answer.
Thank you in advance!
I freshly tried, when you open a being opened excel-file again, the system will tell you that it is read-only, and when you edit sth. in this read-only one, the system requires to save the file under another folder.
And this is also just what my code does.
Altough I set the read-only as false(third place within Workbooks.Open()).
It has no effects. So do I need another way instead of "OPEN" the excel-file???