用c#写入电子表格活动单元格
writing into spreadsheet active cell with c#
我编写了一个 C# winforms 程序,它打开一个 excel 电子表格并将整数值 "total" 写入一个单元格(在本例中为 I9)。有什么方法可以让我在电子表格中选择一个单元格,然后在当前活动的单元格中写入我的整数值?
这是与我的问题相关的代码:
private static Excel.Workbook MyBook = null;
private static Excel.Application excelApp = null;
private static Excel.Worksheet MySheet = null;
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openb = new OpenFileDialog();
if (openb.ShowDialog() == DialogResult.OK)
{
var name = openb.FileName;
String filename = DialogResult.ToString();
excelApp = new Excel.Application();
excelApp.Visible = true;
MyBook = excelApp.Workbooks.Open(name);
MySheet = (Excel.Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
MySheet.Cells[9, 9] = total;
MyBook.Save();
}
}
您需要第二个按钮。
第一个打开(并保持打开)您需要的文件,因为您已经拥有它:
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openb = new OpenFileDialog();
if (openb.ShowDialog() == DialogResult.OK)
{
var name = openb.FileName;
String filename = DialogResult.ToString();
excelApp = new Excel.Application();
excelApp.Visible = true;
}
第二个按钮在用户选择的单元格中写入 total
的值:
private void button5_Click(object sender, EventArgs e)
{
if (excelApp != null && MyBook != null) //sanity check
{
MySheet = (Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
MySheet.Cells[excelApp.ActiveCell.Row, excelApp.ActiveCell.Column] = total;
MyBook.Save();
}
}
我编写了一个 C# winforms 程序,它打开一个 excel 电子表格并将整数值 "total" 写入一个单元格(在本例中为 I9)。有什么方法可以让我在电子表格中选择一个单元格,然后在当前活动的单元格中写入我的整数值?
这是与我的问题相关的代码:
private static Excel.Workbook MyBook = null;
private static Excel.Application excelApp = null;
private static Excel.Worksheet MySheet = null;
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openb = new OpenFileDialog();
if (openb.ShowDialog() == DialogResult.OK)
{
var name = openb.FileName;
String filename = DialogResult.ToString();
excelApp = new Excel.Application();
excelApp.Visible = true;
MyBook = excelApp.Workbooks.Open(name);
MySheet = (Excel.Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
MySheet.Cells[9, 9] = total;
MyBook.Save();
}
}
您需要第二个按钮。
第一个打开(并保持打开)您需要的文件,因为您已经拥有它:
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openb = new OpenFileDialog();
if (openb.ShowDialog() == DialogResult.OK)
{
var name = openb.FileName;
String filename = DialogResult.ToString();
excelApp = new Excel.Application();
excelApp.Visible = true;
}
第二个按钮在用户选择的单元格中写入 total
的值:
private void button5_Click(object sender, EventArgs e)
{
if (excelApp != null && MyBook != null) //sanity check
{
MySheet = (Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
MySheet.Cells[excelApp.ActiveCell.Row, excelApp.ActiveCell.Column] = total;
MyBook.Save();
}
}