如何将数据写入正在打开的excel文件的单元格中? [英] How to write data into cells for a being opened excel-file?

查看:123
本文介绍了如何将数据写入正在打开的excel文件的单元格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙伴,



当我从Windows窗体应用程序导出值时,我当前发现问题到一个excel文件。我首先创建了一个空的excel文件,我希望excel能够在窗口中获取值。例如。 Cell [1,3]获得textBox1的值; Cell [2,4]获得label1等的值。



当数据写入过程中不打开excel文件时,它运行良好。但是我注意到,当我打开excel文件然后控制我的窗体时,这些值将不会在excel文件中写入。而不是这个,他们写在另一个excel文件(具有相同的名称)在我的文档下。



我认为这是因为我使用方法工作簿。打开()。我想知道是否应该改变方法Workbooks.Open()中的对象,这在我的编程中所有Type :: Missing都是。我尝试了很多组合。例如,要将UpdateLinks设置为3,请将Editable设置为true,依此类推。但它还没有奏效。

参考msdn地址为Workbooks.Open():

http://msdn.microsoft.com/en-us/library/ms260757(v = office.11 )?cs-save-lang = 1& cs-lang = cpp#code-snippet-2 [ ^ ]



这是我的代码,它显示了我如何写值到excel文件。我希望你会看到并告诉我如何解决我的问题:

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; // I used DateTime, because it''s a chagngable value
                  

 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);
}

}





如果我的问题没有明确解释,或者你有问我的代码。告诉我,我很高兴回答。

提前谢谢!



我刚刚尝试过,当你再次打开一个正在打开的excel文件时,系统会告诉你那个它是只读的,当你编辑某事时。在这个只读的系统中,系统需要将文件保存在另一个文件夹下。

这也是我的代码所做的。

尽管我设置了读取仅作为false(Workbooks.Open()中的第三位)。

没有效果。那么我需要另一种方式而不是打开excel文件???



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???

推荐答案

问题似乎是在
wb->Close(true, false, Type::Missing);



第二个参数应该是Filename而不是布尔值。请尝试以下代码...


Second parameter is supposed to be "Filename" not a boolean. Try the following instead ...

wb->Close(true, Type::Missing, Type::Missing);



在这个实例中你不需要


You won''t need the

wb->Save();

行 - 或保留保存并更改关闭时的第一个参数错误。



我认为尽管有所有的FinalReleaseComObject调用你都得到了只读错误,因为仍然有对原始电子表格的引用 - 该版本应该是循环 - 请参阅 http://support.microsoft.com/kb/317109 [ ^ ]

line in this instance - or keep the save and change the first parameter on the close to false.

I think you got the read-only error despite all the FinalReleaseComObject calls because there was still a reference to the original spreadsheet - the release should probably be in loop - see http://support.microsoft.com/kb/317109[^]


这篇关于如何将数据写入正在打开的excel文件的单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆