The Problem I am having is that each time a number from the comboBox is selected it is creating a new worksheet. I just want the variable to be added to the active sheet only, and not create a sheet each time. Any Help is greatly appreciated.
Would something like this work?
var xl = new Excel.Application();
xl.Visible = true;
var wb = (Excel._Workbook)xl.Active开发者_JAVA技巧Workbook; //(xl.Workbooks.Add(Missing.Value));
var sheet = (Excel._Worksheet)wb.ActiveSheet;
//Generate Linear Guide Support in Solidworks
if (comboBox1.Text == "0")
{
sheet.Cells[6, 4] = "0"; //Cell Location[y-axis, x-axis]
}
if (comboBox2.Text == "AH")
{
sheet.Cells[6, 5] = "AH";
}
if(comboBox3.Text == "2")
{
sheet.Cells[6, 6] = "2";
}
Factor out this code and don't invoke it for every selection. You can move this so that it is executed only once.
// Available at the class level. single instance - Singleton pattern may be employed
// Check the correct datatype for ExcelSheet
ExcelSheet sheet;
void UpdateExcelSheet(int row, int col, string value)
{
if (sheet == null)
{
var xl = new Excel.Application();
xl.Visible = true;
var wb = (Excel._Workbook)(xl.Workbooks.Add(Missing.Value));
sheet = (Excel._Worksheet)wb.ActiveSheet;
}
sheet.Cells[row, col] = value;
}
void OnComboSelection()
{
int row, col;
string value;
if(comboBox5.Text == "1")
{
row = 6; col = 6; value = "1";
}
if (comboBox3.Text == "2")
{
row = 6; col = 8; value = "2";
}
UpdateExcelSheet(row, col, value);
}
精彩评论