本文介绍了写入已打开的excel文件(通过openfiledialog)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello MSDN。 我是C#.NET开发的新手,我正在试图弄清楚如何将数据写入已由opefiledialog方法打开的工作簿中的特定工作表和单元格。
有没有办法做到这一点?
$
或者写入打开的excel文件的方法在打开excel文件后启动。不知道最好的方法是什么。
这是代码。
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;
工作簿wbook;
工作表wsheet;
_Application excel = new _Excel.Application();
if(openFileDialog.ShowDialog()== DialogResult.OK)
{
excel = new _Excel.Application();
wbook = excel.Workbooks.Open(openFileDialog.FileName);
excel.Visible = true;
}
}
}
解决方案
Hello MSDN. I am new to C#.NET development and I am trying to figure out how to write data into specific sheets and cells in a workbook that is already opened by opefiledialog method. Is there a way to do this?
Alternatively the method that writes to the open excel file initiates after the excel file has been opened. Not sure what the best approach is here.
Here 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; } } }
解决方案
这篇关于写入已打开的excel文件(通过openfiledialog)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!